Tunnel Warfare
Test instructions: D for destroying the village, R for repairing the last destroyed village, Q for asking what is the maximum continuous interval, including x?
Idea: The maximal continuous interval of a node is determined by the maximum continuous interval of the left son, the maximal continuous interval of the right son, the largest continuous right interval of the left son, and the largest contiguous left interval of the right son.
So the nodes of the segment tree should maintain the maximal continuous left interval, the maximal continuous right interval, and the maximal continuous interval of the current node.
When updating, if the left son is full, the left continuous interval of the father's node should be added to the left of the right son. Vice versa.
When the query, you can prune, if it is a leaf, or empty, or full, then do not go down query.
When querying, also note that the current query point in the left son's maximum right continuous range, the maximum continuous interval plus the right son's maximum continuous interval. Vice versa;
1 //#pragma COMMENT (linker, "/stack:1024000000,1024000000")2#include <iostream>3#include <cstdio>4#include <cstring>5#include <sstream>6#include <string>7#include <algorithm>8#include <list>9#include <map>Ten#include <vector> One#include <queue> A#include <stack> -#include <cmath> -#include <cstdlib> the //#include <conio.h> - using namespacestd; - #defineCLC (A, B) memset (A,b,sizeof (a)) - #defineINF 0x3f3f3f3f + #defineLson l,mid,rt<<1 - #defineRson mid+1,r,rt<<1|1 + Const intN =50010; A Const intMOD = 1e9+7; at #defineLL Long Long - #defineMi () (l+r) >>1 - Double ConstPI = ACOs (-1); - voidfre () { -Freopen ("In.txt","R", stdin); - } in //Inline int R () { - //int X=0,f=1;char Ch=getchar (); to //While (ch> ' 9 ' | | ch< ' 0 ') {if (ch== '-') F=-1;ch=getchar ();} + //while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f; - // } the intS[n]; * structNode { $ intL,r;Panax Notoginseng intLs,rs,ms; -} e[n<<2]; the + voidPushup (intRT) { A the } + voidBuildintLintRintRT) { -E[rt].l=l; $E[rt].r=R; $e[rt].ls=e[rt].rs=e[rt].ms=r-l+1; - if(l!=r) { - intMid=mi (); the build (Lson); - build (Rson);Wuyi } the } - Wu voidUpdateintRtintCintx) { - if(e[rt].l==E[RT].R) { About if(x>0) { $e[rt].ls=e[rt].rs=e[rt].ms=1; -}Else -e[rt].ls=e[rt].rs=e[rt].ms=0; - return; A } + intMid= (E[RT].L+E[RT].R) >>1; the if(c<=mid) { -Update (rt<<1, c,x); $}Else theUpdate (rt<<1|1, c,x); thee[rt].ls=e[rt<<1].ls; thee[rt].rs=e[rt<<1|1].rs; theE[rt].ms=max (Max (e[rt<<1].ms,e[rt<<1|1].ms),e[rt<<1].rs+e[rt<<1|1].ls); - if(e[rt<<1].ls==e[rt<<1].r-e[rt<<1].l+1) { ine[rt].ls+=e[rt<<1|1].ls; the } the if(e[rt<<1|1].rs==e[rt<<1|1].r-e[rt<<1|1].l+1) { Aboute[rt].rs+=e[rt<<1].rs; the } the } the + intQueryintRtintc) { - if(e[rt].l==e[rt].r| | e[rt].ms==0|| e[rt].ms==e[rt].r-e[rt].l+1) { the returne[rt].ms;Bayi } the intMid= (E[RT].L+E[RT].R) >>1; the if(c<=mid) { - if(c>=e[rt<<1].r-e[rt<<1].rs+1) { - returnQuery (rt<<1, c) +query (rt<<1|1, mid+1); the}Else the returnQuery (rt<<1, c); the}Else { the if(c<=e[rt<<1|1].l+e[rt<<1|1].ls-1) { - returnQuery (rt<<1|1, c) +query (rt<<1, mid); the}Else the returnQuery (rt<<1|1, c); the }94 } the intMain () { the //fre (); the intN,q,top;98 while(~SCANF ("%d%d",&n,&q)) { AboutBuild1N1); -top=0;101 while(q--) {102 CharC;103 intx;104Cin>>C; the if(c=='D') {106Cin>>x;107s[top++]=x;108Update1X0);109}Else if(c=='Q') { theCin>>x;111printf"%d\n", Query (1, x)); the}Else {113x=s[--top]; theUpdate1X1); the } the }117 }118 return 0;119}
HDU 1540 Tunnel Warfare segment tree interval merging