POJ 2892 Tunnel Warfare (线段树)

Tunnel Warfare
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 5985   Accepted: 2440

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 (nm  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:

  1. D x: The x-th village was destroyed.
  2. Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
  3. 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

Hint

An illustration of the sample input:

      OOOOOOO


D 3 OOXOOOO
D 6 OOXOOXO
D 5 OOXOXXO
R OOXOOXO
R OOXOOOO

Source

 
 
题意:抗日时期,八路军擅用地道把村落连起来,现指挥官要知道一些信息:对地道的操作如下
D x   表示销毁x这个地道
Q x 表示查询有多少个地道与x相连
   修复最后被摧毁的地道

思路:线段路,记录每个结点左边 中间,右边 连续的地道数量
 
 
#include<iostream>

#include<cstdio>

#include<cstring>



using namespace std;



#define L(rt) (rt<<1)

#define R(rt) (rt<<1|1)



const int N=50010;



struct node{

    int l,r,state;  //state -1表示空,1表示全被destroy

    int lma,mma,rma;     //左中右连续的数量

}tree[N*3];



int D[N];



void build(int l,int r,int rt){

    tree[rt].l=l;

    tree[rt].r=r;

    if(l==r)

        return ;

    int mid=(l+r)>>1;

    build(l,mid,L(rt));

    build(mid+1,r,R(rt));

}



void PushDown(int op,int rt){

    tree[rt].state=0;

    if(op==-1){

        tree[L(rt)].state=-1;

        tree[L(rt)].lma=tree[L(rt)].mma=tree[L(rt)].rma=tree[L(rt)].r-tree[L(rt)].l+1;

        tree[R(rt)].state=-1;

        tree[R(rt)].lma=tree[R(rt)].mma=tree[R(rt)].rma=tree[R(rt)].r-tree[R(rt)].l+1;

    }else if(op==1){

        tree[L(rt)].state=1;

        tree[L(rt)].lma=tree[L(rt)].mma=tree[L(rt)].rma=0;

        tree[R(rt)].state=1;

        tree[R(rt)].lma=tree[R(rt)].mma=tree[R(rt)].rma=0;

    }

}



void PushUp(int rt){

    if(tree[L(rt)].state==-1)

        tree[rt].lma=(tree[L(rt)].r-tree[L(rt)].l+1)+tree[R(rt)].lma;

    else

        tree[rt].lma=tree[L(rt)].lma;

    if(tree[R(rt)].state==-1)

        tree[rt].rma=(tree[R(rt)].r-tree[R(rt)].l+1)+tree[L(rt)].rma;

    else

        tree[rt].rma=tree[R(rt)].rma;

    tree[rt].mma=tree[L(rt)].rma+tree[R(rt)].lma;

}



void destroy(int num,int rt){   //销毁操作

    if(tree[rt].state==1)

        return ;

    if(tree[rt].l==num && tree[rt].r==num){ //找到点修改它的值

        tree[rt].state=1;

        tree[rt].lma=tree[rt].mma=tree[rt].rma=0;

        return ;

    }

    if(tree[rt].state==-1)  //延迟覆盖  

        PushDown(-1,rt);

    if(num<=tree[L(rt)].r)

        destroy(num,L(rt));

    if(num>=tree[R(rt)].l)

        destroy(num,R(rt));

    PushUp(rt);  //根据左右结点,修改父结点

    if(tree[L(rt)].state==tree[R(rt)].state)

        tree[rt].state=tree[L(rt)].state;

}



int query(int num,int rt){   //查询操作

    if(tree[rt].state==-1)

        return tree[rt].r-tree[rt].l+1;

    if(tree[rt].l==num && tree[rt].r==num)  //如果它的状态不为-1 则一定是被销毁了,返回0

        return tree[rt].lma;

    if(num>=tree[rt].l && num<=tree[rt].l+tree[rt].lma-1)   //如果在左连续的段内

        return tree[rt].lma;

    if(num<=tree[rt].r && num>=tree[rt].r-tree[rt].rma+1)   //在右

        return tree[rt].rma;

    if(num>=tree[L(rt)].r-tree[L(rt)].rma+1 && num<=tree[R(rt)].l+tree[R(rt)].lma-1)    //在中间

        return tree[rt].mma;

    if(num<=tree[L(rt)].r)

        query(num,L(rt));

    else if(num>=tree[R(rt)].l)

        query(num,R(rt));

    return 0;

}



void rebuild(int num,int rt){

    if(tree[rt].l==num && tree[rt].r==num){

        tree[rt].state=-1;

        tree[rt].lma=tree[rt].mma=tree[rt].rma=1;

        return ;

    }

    if(tree[rt].state==1)

        PushDown(1,rt);

    if(num<=tree[L(rt)].r)

        rebuild(num,L(rt));

    if(num>=tree[R(rt)].l)

        rebuild(num,R(rt));

    PushUp(rt);

    if(tree[L(rt)].state==tree[R(rt)].state)

        tree[rt].state=tree[L(rt)].state;

}



int main(){



    freopen("input.txt","r",stdin);



    int n,m;

    while(~scanf("%d%d",&n,&m)){

        build(1,n,1);

        tree[1].state=-1;

        tree[1].lma=tree[1].mma=tree[1].rma=tree[1].r-tree[1].l+1;

        int cnt=0,num;

        char op;

        while(m--){

            getchar();

            scanf("%c",&op);

            if(op=='D'){

                scanf("%d",&num);

                D[cnt++]=num;

                destroy(num,1);

            }else if(op=='Q'){

                scanf("%d",&num);

                int len=query(num,1);

                printf("%d\n",len);

            }else{

                num=D[--cnt];

                rebuild(num,1);

            }

        }

    }

    return 0;

}

 

你可能感兴趣的:(poj)