Tunnel warfare
Time Limit: 4000/2000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 2396 accepted submission (s): 886
Problem descriptionduring the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of North China Plain. generally speaking, ages connected by tunnels lay in a line. cannot the two at the ends, every village was directly connected with two neighboring ones.
Frequently the invaders launched attack on some of the versions and destroyed the parts of tunnels in them. the Eighth Route Army commanders requested the latest connection state of the tunnels and ages. if some versions are severely isolated, restoration of connection must be done immediately!
Inputthe first line of the input contains two positive integers n and M (n, m ≤ 50,000) indicating the number of versions and events. Each of the next M lines describes an event.
There are 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 versions that X-th village was directly or indirectly connected with including itself.
R: The village destroyed last was rebuilt.
Outputoutput the answer to each of the army commanders 'request in order on a separate line.
Sample input7 9 d 3 D 6 d 5 q 4 Q 5 r Q 4 r q 4
Sample output1 0 2 4
Sourcepoj monthly
The recommendll is an online point. d x destroys this point. q X indicates the maximum number of consecutive points where X is located, R is the point where the last damage is restored. The line segment Tree node sets the maximum number of consecutive records starting from the left endpoint of the ll record interval, and the maximum number of consecutive records starting from the right endpoint of the RL record interval. ml indicates the maximum number of consecutive points in the interval. It mainly involves two operations: update and query.
/* HDU 1540 tunnel warfare refers to a segment. d X indicates that the X point is damaged, and r indicates that the last point is damaged, q X indicates the longest continuous interval of the two ends centered on X points. */ # Include <Stdio. h> # Include <String . H> # Include <Iostream> # Include <Algorithm> Using Namespace STD; Const Int Maxn = 50050 ; Struct Node { Int L, R; Int Ll, rl, ml; // The maximum start length on the left and the maximum start length on the right // And the maximum continuous length of this interval } Segtree [maxn * 3 ]; Void Build ( Int I, Int L, Int R) {segtree [I]. L = L; segtree [I]. r = R; segtree [I]. ll = Segtree [I]. RL = segtree [I]. ML = r-L + 1 ; If (L = r) Return ; Int Mid = (L + r)> 1 ; Build (I < 1 , L, mid); Build (I < 1 ) | 1 , Mid + 1 , R );} Void Update ( Int I, Int T, Int Val ){ If (Segtree [I]. L = Segtree [I]. R ){ If (Val = 1 ) Segtree [I]. LL = segtree [I]. RL = segtree [I]. ML = 1 ; Else Segtree [I]. LL = segtree [I]. RL = segtree [I]. ML = 0 ; Return ;} Int Mid = (segtree [I]. L + segtree [I]. R)> 1 ; If (T <= mid) Update (I < 1 , T, Val ); Else Update (I < 1 ) | 1 , T, Val); segtree [I]. ll = Segtree [I < 1 ]. Ll; segtree [I]. RL = Segtree [(I < 1 ) | 1 ]. RL; segtree [I]. ml = Max (segtree [I < 1 ]. Ml, segtree [(I < 1 ) | 1 ]. Ml); segtree [I]. ml = Max (segtree [I]. ml, segtree [I < 1 ]. RL + segtree [(I < 1 ) | 1 ]. Ll ); If (Segtree [I < 1 ]. LL = segtree [I < 1 ]. R-segtree [I < 1 ]. L + 1 ) Segtree [I]. LL + = segtree [(I < 1 ) | 1 ]. Ll; If (Segtree [(I < 1 ) | 1 ]. RL = segtree [(I < 1 ) | 1 ]. R-segtree [(I <1 ) | 1 ]. L + 1 ) Segtree [I]. RL + = Segtree [I < 1 ]. RL ;} Int Query ( Int I, Int T ){ If (Segtree [I]. L = segtree [I]. r | segtree [I]. ML = 0 | Segtree [I]. ML = segtree [I]. R-segtree [I]. L + 1 ){ Return Segtree [I]. ml ;} Int Mid = (segtree [I]. L + segtree [I]. R)> 1 ; If (T <= Mid ){ If (T> = segtree [I < 1 ]. R-segtree [I < 1 ]. RL + 1 ) Return Query (I < 1 , T) + query (I < 1 ) | 1 , Mid + 1 ); Else Return Query (I < 1 , T );} Else { If (T <= segtree [(I < 1 ) | 1 ]. L + segtree [(I <1 ) | 1 ]. Ll- 1 ) Return Query (I < 1 ) | 1 , T) + query (I < 1 , Mid ); Else Return Query (I < 1 ) | 1 , T );}} Int Que [maxn]; Int Top; Int Main (){ // Freopen ("in.txt", "r", stdin ); // Freopen ("out.txt", "W", stdout ); Int N, m; Char STR [ 10 ]; Int X; While (Scanf ( " % D " , & N, & M )! = EOF) {build ( 1 , 1 , N); top = 0 ; While (M -- ) {Scanf ( " % S " ,&Str ); If (STR [ 0 ] = ' D ' ) {Scanf ( " % D " ,& X); que [Top ++] = X; Update ( 1 , X, 0 );} Else If (STR [ 0 ] = ' Q ' ) {Scanf ( " % D " ,& X); printf ( " % D \ n " , Query (1 , X ));} Else { If (X> 0 ) {X = Que [-- Top]; Update ( 1 , X, 1 );}}}} Return 0 ;}