Tunnel Warfare 线段树区间合并

H - Tunnel Warfare

 

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

线段树区间合并模板

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int num=50005;
const int inf=0x3f3f3f3f;
char ss[2];
struct point
{
    int l,r,ls,rs,ms;
}tree[num*4];  //ls,rs,ms分别维护左边最大连续区间,右边最大连续区间,区间中最大连续区间
int LL,RR,point,ans;
void build(int k,int LL,int RR)
{
    tree[k].l=LL,tree[k].r=RR;
    tree[k].rs=tree[k].ls=tree[k].ms=RR-LL+1; // 新建的树区间是连续的
    if(tree[k].l==tree[k].r){
        return;
    }
    int bet=(tree[k].l+tree[k].r)/2;
    build(k*2,LL,bet);
    build(k*2+1,bet+1,RR);
}
void change(int k,int ch,int point)
{
    if(tree[k].l==tree[k].r){
        if(ch==1){
            tree[k].ls=tree[k].rs=tree[k].ms=1;//ch==1 恢复该点
        }
        else{
            tree[k].ls=tree[k].rs=tree[k].ms=0; //ch==0 破坏该点
        }
        return;
    }
    int bet=(tree[k].r+tree[k].l)/2;
    if(point<=bet) change(k*2,ch,point);
    else change(k*2+1,ch,point);
    tree[k].ls=tree[k*2].ls; //父亲区间的左边最大连续区间为左子区间的左边最大连续区间
    tree[k].rs=tree[k*2+1].rs; //同理
    tree[k].ms=max(tree[k*2].rs+tree[k*2+1].ls,max(tree[k*2].ms,tree[k*2+1].ms)); //父亲区间的最大连续区间的变化只有三种情况:左子区间最大连续区间,右子区间最大连续区间,左子区间右边最大连续区间+右子区间左边最大连续区间,取最大的就好
    if(tree[k*2].ls==tree[k*2].r-tree[k*2].l+1)
        tree[k].ls+=tree[k*2+1].ls; // 如果左子区间的左边最大连续区间为左子区间的整个区间,那么父亲区间的左边最大区间还要加上右子区间的左边最大连续区间,因为左子区间的右边界并不是父亲区间的左边最大连续区间的边界
    if(tree[k*2+1].rs==tree[k*2+1].r-tree[k*2+1].l+1)
        tree[k].rs+=tree[k*2].rs; // 同理
}
void ask(int k,int point)
{
    if(tree[k].l==tree[k].r||tree[k].ms==0||tree[k].r-tree[k].l+1==tree[k].ms){  //这里的三个判断条件分别是:节点区间为叶子区间,节点区间中点全为0,节点区间中点全为1
        ans+=tree[k].ms; // 注意这里是+=不是=,因为目标区间包含该节点区间,目标区间的长度应为包含的所有节点区间的长度的和
        return;
    }
    int bet=(tree[k].r+tree[k].l)/2;
    if(point<=bet){ // 注意<=
        if(point>=tree[k*2].r-tree[k*2].rs+1){ //注意>=   tree[k*2].r-tree[k*2].rs+1为左子区间右边最大连续区间的边界
            ask(k*2,point);//这里判断条件是目标节点在左子区间且在左子区间右边最大连续区间边界的右边,这时还得查询右子区间,因为左子区间的右边界并不是目标区间的右边界,目标区间的右边界在右子区间且在右子区间左边最大连续区间边界的左边
            ask(k*2+1,bet+1);
        }
        else{
            ask(k*2,point);
        }
    }
    else{
        if(point<=tree[k*2+1].l+tree[k*2+1].ls-1){  //注意<=  
            ask(k*2+1,point); //同理
            ask(k*2,bet);
        }
        else{
            ask(k*2+1,point);
        }
    }
}
int main()
{
    int n,m;
    while(scanf("%d %d",&n,&m)!=-1){
        build(1,1,n);
        int a;
        stack sta;
        for(int i=1;i<=m;i++){
            scanf("%s",ss);
            if(ss[0]=='D'){
                scanf("%d",&a);
                change(1,0,a);
                sta.push(a);
            }
            if(ss[0]=='R'){
                a=sta.top();
                sta.pop();
                change(1,1,a);
            }
            if(ss[0]=='Q'){
                scanf("%d",&a);
                ans=0; //每次查询前记得清空ans
                ask(1,a);
                printf("%d\n",ans);
            }
        }
    }
    return 0;
}

 

你可能感兴趣的:(Tunnel Warfare 线段树区间合并)