zoukankan      html  css  js  c++  java
  • hdu 1540 Tunnel Warfare

    Tunnel Warfare

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2273    Accepted Submission(s): 837


    Problem Description
    During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was directly connected with two neighboring ones.

    Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages are severely isolated, restoration of connection must be done immediately!
     
    Input
    The first line of the input contains two positive integers n and m (n, m ≤ 50,000) indicating the number of villages and events. Each of the next m lines describes an event.

    There are three different events described in different format shown below:

    D x: The x-th village was destroyed.

    Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.

    R: The village destroyed last was rebuilt.
     
    Output
    Output the answer to each of the Army commanders’ request in order on a separate line.
     
    Sample Input
    7 9
    D 3
    D 6
    D 5
    Q 4
    Q 5
    R
    Q 4
    R
    Q 4
     
    Sample Output
    1
    0
    2
    4
     
    Source
     
    Recommend
    LL
    //求x所在最长的连续可用序列
    //query函数写的我蛋疼,判断各种情况
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <queue>
    #include <stack>
    #define N 50000
    #define lson l,m,k<<1
    #define rson m+1,r,k<<1|1
    using namespace std;
    struct node
    {
        int lm,rm,m;
        int len;
    };
    node st[N<<2];
    void up(int &k)
    {
        int ls=k<<1,rs=k<<1|1;
    st[k].lm=st[ls].lm==st[ls].len?st[ls].len+st[rs].lm:st[ls].lm;
    st[k].rm=st[rs].rm==st[rs].len?st[rs].len+st[ls].rm:st[rs].rm;
    
    st[k].m=max(max(st[ls].m,st[rs].m),st[ls].rm+st[rs].lm);
    //st[k].m=max(max(st[k].lm,st[k].rm),st[k].m);
    }
    void build(int l,int r,int k)
    {
        st[k].len=r-l+1;
        if(l==r)
        {
            st[k].lm=st[k].rm=st[k].m=1;
            return ;
        }
        int m=(l+r)>>1;
        build(lson);
        build(rson);
        up(k);
    }
    int flag;
    void update(int &index,int l,int r,int k)
    {
        if(l==r)
         {
             st[k].lm=st[k].rm=st[k].m=flag;
             return;
         }
         int m=(l+r)>>1;
         if(index<=m) update(index,lson);
         else update(index,rson);
         up(k);
    }
    int query(int &index,int l,int r,int k)
    {
       if(st[k].m==0) return 0;
       if(st[k].m==st[k].len) return st[k].len;
       int m=(l+r)>>1;
       if(st[k].lm&&st[k].lm+l-1>=index) return st[k].lm;
    
       if(st[k].rm&&r-st[k].rm+1<=index) return st[k].rm;
      // printf("%d %d %d\n",l,r,m-st[k<<1].rm+1);
       if(st[k<<1].rm&&index<=m&&m-st[k<<1].rm+1<=index)
         return st[k<<1].rm+st[k<<1|1].lm;
    
    if(st[k<<1|1].lm&&index>m&&m+st[k<<1|1].lm>=index)
          return st[k<<1].rm+st[k<<1|1].lm;
       if(index<=m)
       return   query(index,lson);
       else
       return   query(index,rson);
    }
    int main()
    {
        int n,m;
        char op;
        int data;
       while(scanf("%d%d",&n,&m)!=EOF)
       {
           stack<int> s;
           build(1,n,1);
           while(m--)
           {
               getchar();
               scanf("%c",&op);
               switch(op)
               {
                   case 'Q':scanf("%d",&data);
                      printf("%d\n",query(data,1,n,1));break;
                   case 'D':scanf("%d",&data);s.push(data);
                      flag=0;update(data,1,n,1);break;
                   case 'R':data=s.top();s.pop();
                      flag=1;update(data,1,n,1);break;
               }
           }
       }
        return 0;
    }
  • 相关阅读:
    最小生成树示例程序_Prim算法
    邻接表示例程序
    拓扑排序示例程序
    hdu1754 I Hate It && hdu1166 敌兵布阵 ——线段树复习
    2013年4月26日 晴
    Snakes & Ladders ——BFS入门题
    zoj1203 Swordfish ——最小生成树入门题_Kruscal算法
    poj3087 Shuffle'm Up ——水题
    poj1002 4873279 ——水题
    2013年4月21日 阴
  • 原文地址:https://www.cnblogs.com/372465774y/p/2603793.html
Copyright © 2011-2022 走看看