Topic Link: Portal
The main topic: There are N villages, m operations. There are three types of operations. 1. ' D X ' destroys village x,2. ' R ' rebuilt the last destroyed village, 3. ' Q x ' Ask how many villages are connected to X
Topic Idea: Segment tree interval merging
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<cstring>#include<stack>#include<cctype>#include<queue>#include<string>#include<vector>#include<Set>#include<map>#include<climits>#defineLson Root<<1,l,mid#defineRson Root<<1|1,mid+1,r#defineFi first#defineSe Second#definePing (x, y) ((x-y) * (x-y))#defineMST (x, y) memset (x,y,sizeof (x))#defineMCP (x, y) memcpy (x,y,sizeof (y))using namespacestd;#defineGamma 0.5772156649015328606065120#defineMOD 1000000007#defineINF 0x3f3f3f3f#defineN 500005#defineMAXN 50005typedef pair<int,int>Pii;typedefLong LongLL;Const DoublePi=acos (-1.0);Const DoubleE=2.718281828459; Stack<int>SK;intn,m,x;Charstr[Ten];intnode[maxn<<2],mark[maxn<<2];intlsum[maxn<<2],rsum[maxn<<2];inlinevoidPushup (intRootintLintRintmid) {Lsum[root]=lsum[root<<1]; Rsum[root]=rsum[root<<1|1]; Node[root]=max (Max (node[root<<1],node[root<<1|1]),rsum[root<<1]+lsum[root<<1|1]); if(lsum[root]==mid-l+1) lsum[root]+=lsum[root<<1|1]; if(Rsum[root]==r-mid) rsum[root]+=rsum[root<<1];}voidBuildintRootintLintR) { if(l==R) {Node[root]=lsum[root]=rsum[root]=1; return; } intMid=l+r>>1; Build (Lson); build (Rson); Pushup (Root,l,r,mid);}voidAddintRootintLintRintflag) { if(l==R) { if(flag==1) lsum[root]=rsum[root]=node[root]=1; Elselsum[root]=rsum[root]=node[root]=0; return; } intMid=l+r>>1; if(x<=mid) Add (Lson,flag); ElseAdd (Rson,flag); Pushup (Root,l,r,mid);}intQueryintRootintLintRintx) { if(l==r| | node[root]== (r-l+1)|| node[root]==0){ returnNode[root]; } intMid=l+r>>1; if(x<=mid) { if(x>mid-rsum[root<<1])returnQuery (lson,x) +query (rson,mid+1); Else returnquery (LSON,X); } Else{ if(x<lsum[root<<1|1]+mid+1)returnQuery (LSON,MID) +query (RSON,X); Else returnquery (RSON,X); }}intMain () {inti,j; while(SCANF ("%d%d", &n,&m)! =EOF) { while(!Sk.empty ()) {Sk.pop ();} Build (1,1, N); while(m--) {scanf ("%s", str); if(str[0]=='D') {scanf ("%d",&x); Sk.push (x); Add (1,1, n,-1); } Else if(str[0]=='Q') {scanf ("%d",&x); printf ("%d\n", Query (1,1, n,x)); } Else{ if(!Sk.empty ()) {x=Sk.top (); Sk.pop (); Add (1,1N1); } } } } return 0;}
HDU1540 (tunnel Warfare)