POJ 2892 Tunnel Warfare(线段树区间合并)

Tunnel Warfare
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 10964   Accepted: 4538

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
 1 #include<iostream>
 2 #include<cstdio>
 3 #define ls (x<<1)
 4 #define rs (x<<1|1)
 5 using namespace std;
 6 const int N=5e4+5;
 7 int st[N],lsum[N<<2],rsum[N<<2],top,n,m;
 8 
 9 void update(int l,int r,int x)
10 {
11     int mid=(l+r)>>1;
12     lsum[x]=lsum[ls];
13     if(lsum[ls]==mid-l+1)lsum[x]+=lsum[rs];
14     rsum[x]=rsum[rs];
15     if(rsum[rs]==r-mid)rsum[x]+=rsum[ls];
16 }
17 void build(int l,int r,int x)
18 {
19     if(l==r)
20     {
21         lsum[x]=rsum[x]=1;
22         return ;
23     }
24     int mid=(l+r)>>1;
25     build(l,mid,ls);
26     build(mid+1,r,rs);
27     update(l,r,x);
28 }
29 void modify(int A,int B,int l,int r,int x,int v)
30 {
31     if(A<=l&&B>=r)
32     {
33         lsum[x]=rsum[x]=v;
34         return;
35     }
36     int mid=(l+r)>>1;
37     if(A<=mid)modify(A,B,l,mid,ls,v);
38     if(B>mid)modify(A,B,mid+1,r,rs,v);
39     update(l,r,x);
40 }
41 int query(int l,int r,int x,int v)
42 {
43     if(l==r)return lsum[x];
44     int mid=(l+r)>>1;
45     if(((v>=mid-rsum[ls]+1)&&(v<=mid))||(v>mid&&v<=(mid+lsum[rs])))return rsum[ls]+lsum[rs];
46     if(v<=mid) return query(l,mid,ls,v);
47     else return query(mid+1,r,rs,v);
48 }
49 int main()
50 {
51     scanf("%d%d",&n,&m);
52     build(1,n,1);
53     char ch;
54     int v;
55     for(int i=1;i<=m;i++)
56     {
57         cin>>ch;
58         if(ch=='D')
59         {
60             scanf("%d",&v);
61             modify(v,v,1,n,1,0);
62             st[++top]=v;
63         }
64         else if(ch=='Q')
65         {
66             scanf("%d",&v);
67             cout<<query(1,n,1,v)<<endl;
68         }
69         else
70         {
71             if(top)
72             {
73                 modify(st[top],st[top],1,n,1,1);
74                 top--;
75             }
76         }
77     }
78     return 0;
79 }

 

posted @ 2022-01-28 20:27  matt-11  阅读(33)  评论(0)    收藏  举报