First, the topic description
During the War of Resistance against Japan, tunnel warfare was carried off extensively in the vast areas of the North China Pl Ain. Generally speaking, villages connected by tunnels lay in a line. Except the ends, every village was directly connected with the 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 is severely isolated, restoration of connection must is done immediately!
Input
The first line of the input contains the positive integers n and m (n, m≤50,000) indicating the number of villages and E Vents. Each of the next m lines describes an event.
There is 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 is directly or indirectly connected with includ ING itself.
R:the village destroyed 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
By the way attach the original title link →_→ Problem-1540
Second, the problem analysis
and the normal line tree is not much different, except that each interval adds two variables--lsum and rsum, respectively, the length of this interval to the right continuous "1" string and the left continuous "1" string length. In the maintenance tree backtracking, an interval of lsum equals the lsum,rsum of its left interval equal to the rsum of its right interval. In particular, when the lsum of an interval is equal to its left interval length, that is, when the interval is a continuous "1" string, the interval lsum need to add the lsum of its right interval;
Third, the code implementation
This problem has a sinkhole, the topic is not mentioned at all, but this problem has a number of data _ (: З"∠) _
1#include <cstdio>2#include <cstring>3 Const intmaxn=5e4+Ten;4 intn,m;5 structnode6 {7 intL,r;8 intlsum,rsum;9}tr[maxn<<2];Ten intStack[maxn],top; One voidBuildintXintYinti) A { -Tr[i].l=x,tr[i].r=y; - if(x==y) the { -tr[i].lsum=1; -tr[i].rsum=1; - return; + } - intMid= (TR[I].L+TR[I].R) >>1; +Build (x,mid,i<<1); ABuild (mid+1,y,i<<1|1); attr[i].lsum=y-x+1, tr[i].rsum=y-x+1; - return; - } - voidUpdateintXintIintval) - { - if(tr[i].l==x&&tr[i].r==x) in { -tr[i].lsum=Val; totr[i].rsum=Val; + return; - } the intMid= (TR[I].L+TR[I].R) >>1; * if(x<=mid) $ {Panax NotoginsengUpdate (x,i<<1, Val); - the } + Else A { theUpdate (x,i<<1|1, Val); + } -tr[i].lsum=tr[i<<1].lsum; $tr[i].rsum=tr[i<<1|1].rsum; $ if(tr[i<<1].lsum==tr[i<<1].r-tr[i<<1].l+1) tr[i].lsum+=tr[i<<1|1].lsum; - if(tr[i<<1|1].rsum==tr[i<<1|1].r-tr[i<<1|1].l+1) tr[i].rsum+=tr[i<<1].rsum; - } the intQuery_left (intXinti) - {Wuyi if(tr[i].r==x) the { - if(tr[i].rsum==tr[i].r-tr[i].l+1&&tr[i].l!=1) Wu { - returnTr[i].rsum+query_left (tr[i].l-1,1); About } $ returntr[i].rsum; - } - intMid= (TR[I].L+TR[I].R) >>1; - if(x<=mid) A { + returnQuery_left (x,i<<1); the } - Else $ { the returnQuery_left (x,i<<1|1); the } the } the intQuery_right (intXinti) - { in the if(tr[i].l==x) the { About if(tr[i].lsum==tr[i].r-tr[i].l+1&&tr[i].r!=N) the { the returnTr[i].lsum+query_right (tr[i].r+1,1); the } + returntr[i].lsum; - } the intMid= (TR[I].L+TR[I].R) >>1;Bayi if(x<=mid) the { the returnQuery_right (x,i<<1); - } - Else the { the returnQuery_right (x,i<<1|1); the } the } - intMain () the { the while(SCANF ("%d%d", &n,&m)! =EOF) the {94memset (TR,0,sizeof(tr)); thememset (Stack,0,sizeof(stack)); thetop=0; theBuild1N1);98 for(intI=1; i<=m;++i) About { - CharC;101 intx;102scanf"\n%c",&c);103 if(c=='D')104 { thescanf"%d",&x);106stack[++top]=x;107Update (x,1,0);108 }109 Else if(c=='R') the {111Update (stack[top--],1,1); the }113 Else the { thescanf"%d",&x); the intAns=query_left (x,1) +query_right (x,1)-1;117 if(ans<=0) printf ("0\n");118 Elseprintf"%d\n", ans);119 } - }121 }122 return 0;123}
Hdu1540-tunnel Warfare
Weakly weak to say, the Konjac Konjac code word is not easy, reproduced please indicate the source http://www.cnblogs.com/Maki-Nishikino/p/6230606.html
"Segment tree interval merging" Hdu1540-tunnel Warfare