Tunnel Warfare
Time Limit: 1000MS |
|
Memory Limit: 131072K |
Total Submissions: 7499 |
|
Accepted: 3096 |
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 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 con Nected with including 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
Hint
An illustration of the sample input:
Ooooooo
D 3 ooxoooo
D 6 Ooxooxo
D 5 Ooxoxxo
R Ooxooxo
R ooxoooo
Test instructions
A row of villages, need to operate: D (x) destroys the village X,r back to the last broken village, Q (x) the longest number of villages connected to X.
Ideas
Segment tree.
01 Indicates whether the village exists. Maintenance of LS RS ms three information.
Query:
If x is in left dial hand, if X is in RS left dial hand you need to query the longest segment of the right child that is connected to m+1, that is, query (u<<1,x) +query (u<<1|1,m+1), and if you are not in RS, return to query directly (u<< 1,X), X is the same as the right child.
Code
1#include <cstdio>2#include <iostream>3 #definefor (A,B,C) for (int a= (b); a<= (c); a++)4 using namespacestd;5 6 Const intN = 1e5+Ten;7 8 structtrie{intL,r,ls,rs,ms; }t[n<<1];9 Ten voidMaintain (intu) { One intlc=u<<1, rc=lc|1; At[u].ls=t[lc].ls,t[u].rs=t[rc].rs; - if(t[lc].ls==t[lc].r-t[lc].l+1) t[u].ls+=T[rc].ls; - if(t[rc].rs==t[rc].r-t[rc].l+1) t[u].rs+=t[lc].rs; thet[u].ms=Max (t[lc].ms,t[rc].ms); -T[u].ms=max (t[u].ms,t[lc].rs+t[rc].ls); - } - voidBuildintUintLintR) { +T[u].l=l,t[u].r=R; - if(l==R) { +t[u].ls=t[u].rs=t[u].ms=r-l+1; A return ; at } - intM= (l+r) >>1; -Build (u<<1, l,m); Build (u<<1|1, m+1, R); - Maintain (u); - } - voidUpdateintUintRintx) { in if(t[u].l==T[U].R) -t[u].ms=t[u].ls=t[u].rs=x; to Else { + intM= (T[U].L+T[U].R) >>1; - if(r<=m) Update (u<<1, r,x); the ElseUpdate (u<<1|1, r,x); * Maintain (u); $ }Panax Notoginseng } - intQueryintUintx) { the if(T[U].L==T[U].R | | t[u].ms==0|| t[u].ms==t[u].r-t[u].l+1) + returnt[u].ms; A intM= (T[U].L+T[U].R) >>1,lc=u<<1, rc=lc|1; the if(x<=M) { + if(x>=t[lc].r-t[lc].rs+1) - returnQuery (lc,x) +query (rc,m+1); $ Else returnquery (lc,x); $}Else { - if(x<=t[rc].l+t[rc].ls-1) - returnQuery (RC,X) +query (lc,m); the Else returnquery (rc,x); - }Wuyi } the - intN,m,s[n],top,f[n]; Wu Charop[5]; - voidReadint&x) { About CharC=getchar ();intf=1; x=0; $ while(!isdigit (c)) {if(c=='-') f=-1; c=GetChar ();} - while(IsDigit (c)) x=x*Ten+c-'0', c=GetChar (); -x*=F; - } A intMain () { + read (n), read (m); theBuild1,1, n); - intx; $ while(m--) { thescanf"%s", op); the Switch(op[0]) { the Case 'D': theRead (x); Update (1X0); s[++top]=x;f[x]=1; - Break; in Case 'R': the if(top) theUpdate1, S[top],1), f[s[top]]=0,--top; About Break; the Case 'Q': the read (x); the if(F[x]) puts ("0"); + Elseprintf"%d\n", Query (1, x)); - Break; the }Bayi } the return 0; the}
POJ 2892 tunnel Warfare (segment tree)