---restore content starts---
Tunnel Warfare
Time Limit: 2000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64u
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 9D 3D 6D 5Q 4Q 5RQ 4RQ 4
Sample Output
1024 template title, explaining the code
#include"Cstdio"#include"algorithm"using namespacestd;Const intmaxn=50005;structnode{intL,r; intll,rl,ml;} A[MAXN*3];voidBuildintRtintLintR) {A[RT].L=l; A[RT].R=R; A[rt].ll=a[rt].rl=a[rt].ml=r-l+1; if(L==R)return ; intMid= (l+r) >>1; Build (Rt<<1, L,mid); Build (Rt<<1)|1, mid+1, R);}voidMergeintRT) { //When the Joz tree is full, it should be merged with the left interval of the right sub-treea[rt].ll=a[rt<<1].ll; if(a[rt<<1].ll== (a[rt<<1].r-a[rt<<1].l+1) ) {A[rt].ll+=a[(rt<<1)|1].ll; } //If the right subtree is full, it should be merged with the Zuozia[rt].rl=a[(rt<<1)|1].rl; if(A[(rt<<1)|1].rl== (a[(rt<<1)|1].r-a[(rt<<1)|1].l+1) ) {A[rt].rl+=a[rt<<1].rl; } //The maximum continuous length of the subtree is the maximum of the continuous length of the left or right subtree or the length of the Zuozi right interval and the left interval of the right sub -tree.A[rt].ml=max (Max (a[rt<<1].ml,a[(rt<<1)|1].ML),a[rt<<1].rl+a[(rt<<1)|1].ll);}voidUpdateintRtintPosintval) { if(a[rt].l==A[RT].R) { if(Val) a[rt].ll=a[rt].rl=a[rt].ml=1; ElseA[rt].ll=a[rt].rl=a[rt].ml=0; return ; } intMid= (A[RT].L+A[RT].R) >>1; if(pos<=mid) Update (rt<<1, Pos,val); ElseUpdate ((rt<<1)|1, Pos,val); Merge (RT);}intQueryintRtintPOS) { if(a[rt].l==a[rt].r| | a[rt].ml==0|| a[rt].ml==a[rt].r-a[rt].l+1)//reach the leaf node or the node is full or empty, then you don't have to go down { returna[rt].ml; } intMid= (A[RT].L+A[RT].R) >>1; if(Pos<=mid)//in Record { if(pos>=a[rt<<1].r-a[rt<<1].rl+1)//if the right interval of the left subtree { returnQuery (rt<<1, POS) +query ((rt<<1)|1, mid+1);//you need to query the right subtree } Else { returnQuery (rt<<1, POS); } } Else { if(Pos<=a[(rt<<1)|1].l+a[(rt<<1)|1].ll-1)//if the left interval of the right subtree { returnQuery ((rt<<1)|1, POS) +query (rt<<1, mid);//you need to query the left subtree } Else { returnQuery ((rt<<1)|1, POS); } } }intSTACK[MAXN];inttop;intMain () {intn,m; while(SCANF ("%d%d", &n,&m)! =EOF) { intPOS; Top=0; Build (1,1, N); while(m--) {scanf ("%*c"); CharOP;SCANF ("%c",&op); if(op=='D') {scanf ("%d",&POS); Stack[top++]=POS; Update (1Pos0); } Else if(op=='Q') {scanf ("%d",&POS); printf ("%d\n", Query (1, POS)); } Else{pos=stack[--top]; Update (1Pos1); } } }}
---restore content ends---
HDU1540 (segment tree statistics continuous length)