Tunnel Warfare
Time limit:4000/2000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 7703 Accepted Submission (s): 2981
Problem descriptionduring the War of Resistance against Japan, tunnel warfare was carried off extensively in the vast area S of North China Plain. 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!
Inputthe first line of the input contains, 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 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.
Outputoutput the answer to all of the Army commanders ' request in order on a separate line.
Sample Input7 9D 3D 6D 5Q 4Q 5RQ 4RQ 4
Sample Output1024 topic: There are N villages, each operation: D x means to destroy XQ X to ask how many R to connect with X to restore a city that was destroyed before the problem is the interval merge of the segment tree. First, 0 indicates the village that has been destroyed, 1 indicates that no destroyed or repaired village records information has two: 1.L indicates that the interval from the left number of the longest 1 strings of 2.R indicates that the interval from the right number of the longest 1 string pushup when the length does not cover the entire left or right interval is done directly
TR[X].L=TR[LS].L;
TR[X].R=TR[RS].R;
If the left or right interval is completely covered, then special treatment is done
if (tr[x].l==mid-l+1) tr[x].l+=tr[rs].l;
if (Tr[x].r==r-mid) TR[X].R+=TR[LS].R;
So for the destruction and restore direct single-point modification, the tr[x].l=tr[x].r=z (z is 0 or 1)
For inquiries, you can query A to the left of the longest 1 strings and a to the right up to 1 strings-1
In particular, if a has been destroyed, then Ans=-1
At this time to do a special sentence.
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;#defineLS x<<1#defineRS X<<1|1Const intn=50005;structx{intL,r;} Tr[n<<2];intSt[n];voidBuintLintRintx) { if(l==r) tr[x].l=tr[x].r=1; Else { intmid=l+ (r-l)/2; Bu (l,mid,ls); Bu (Mid+1, R,rs); TR[X].L=tr[x].r=r-l+1; }}voidChanintLintRintXintWintz) { if(l==r) tr[x].l=tr[x].r=Z; Else { intmid=l+ (r-l)/2; if(w<=mid) Chan (l,mid,ls,w,z); ElseChan (mid+1, r,rs,w,z); if(tr[x].l==mid-l+1) tr[x].l+=TR[RS].L; if(Tr[x].r==r-mid) tr[x].r+=TR[LS].R; }}intAsk1 (intLintRintXintW) { if(W==R)returnTR[X].R; Else { intmid=l+ (r-l)/2, re; if(w>mid) {re=ask1 (mid+1, r,rs,w); if(w-mid==re) re+=TR[LS].R; } ElseRe=Ask1 (L,MID,LS,W); returnre; }}intAsk2 (intLintRintXintW) { if(w==l)returnTR[X].L; Else { intmid=l+ (r-l)/2, re; if(w<=mid) {re=Ask2 (L,MID,LS,W); if(mid-w+1==re) re+=TR[RS].L; } ElseRe=ask2 (mid+1, r,rs,w); returnre; }}intMain () {intn,m; while(SCANF ("%d%d", &n,&m)! =EOF) { ints=0; memset (St,0,sizeof(ST)); memset (TR,0,sizeof(tr)); Bu (1N1); while(m--) { CharC; scanf ("\n%c",&c); if(c=='D') {scanf ("%d", &st[++S]); Chan (1N1, St[s],0); } Else if(c=='R') Chan (1N1, st[s--],1); Else { intA,ans; scanf ("%d",&a); Ans=ask1 (1N1, a) +ask2 (1N1, a)-1; if(ans>0) printf ("%d\n", ans); Elseprintf"0\n"); } } } return 0;}
hdu1540 Tunnel Warfare