Segment Tree interval Merge single point modification interval query. Another 1 seconds of conception, the error has been checked for a long time ... The definition of an int was found to be a char type, with a punch face.
#include <stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<stack>#include<Set>#include<map>#include<string>#include<math.h>#include<stdlib.h>#include<time.h>using namespacestd;Const intmaxn=50000+Ten;intn,q;Charop[5];intNum;stack<int>S;BOOLFLAG[MAXN];structsegtree{intLsum,rsum,msum;} segtree[4*MAXN];intFail,ans1,ans2;voidPushup (intRtintLen) { if(segtree[2*rt].lsum==len-len/2) Segtree[rt].lsum=segtree[2*rt].lsum+segtree[2*rt+1].lsum; Elsesegtree[rt].lsum=segtree[2*rt].lsum; if(segtree[2*rt+1].rsum==len/2) Segtree[rt].rsum=segtree[2*rt].rsum+segtree[2*rt+1].rsum; Elsesegtree[rt].rsum=segtree[2*rt+1].rsum; Segtree[rt].msum=max (segtree[2*rt].msum,segtree[2*rt+1].msum); Segtree[rt].msum=max (segtree[rt].msum,segtree[2*rt+1].lsum+segtree[2*rt].rsum);}voidBuildintLintRintRT) { if(l==r) {segtree[rt].lsum=segtree[rt].rsum=segtree[rt].msum=1; return; } intM= (L+R)/2; Build (L,m,2*RT); Build (M+1R2*rt+1); Pushup (Rt,r-l+1);}voidUpdateintInfointNodeintLintRintRT) { if(l==r&&node==l) {segtree[rt].lsum=segtree[rt].rsum=segtree[rt].msum=info; return; } intM= (L+R)/2; if(node<=m) Update (INFO,NODE,L,M,2*RT); ElseUpdate (info,node,m+1R2*rt+1); Pushup (Rt,r-l+1);}voidQuary1 (intLintRintLintRintRT) { if(l<=l&&r<=R) {ans1=ans1+segtree[rt].rsum; if(segtree[rt].rsum!=r-l+1) fail=1; return; } intM= (L+R)/2; if(r>m) Quary1 (l,r,m+1R2*rt+1); if(fail)return; if(l<=m) Quary1 (L,r,l,m,2*RT); if(fail)return;}voidQuary2 (intLintRintLintRintRT) { if(l<=l&&r<=R) {ans2=ans2+segtree[rt].lsum; if(segtree[rt].lsum!=r-l+1) fail=1; return; } intM= (L+R)/2; if(l<=m) Quary2 (L,r,l,m,2*RT); if(fail)return; if(r>m) Quary2 (l,r,m+1R2*rt+1); if(fail)return;}intMain () {//freopen ("In.txt", "R", stdin); while(~SCANF ("%d%d",&n,&Q)) { while(!S.empty ()) S.pop (); Build (1N1); for(intI=0; i<=n;i++) flag[i]=1; for(intI=1; i<=q;i++) {scanf ("%s", op); if(op[0]=='D') {scanf ("%d",&num); S.push (num); Flag[num]=0; Update (0Num1N1); } Else if(op[0]=='Q') {scanf ("%d",&num); if(!flag[num]) printf ("%d\n",0); Else{ans1=0, ans2=0; Fail=0; if(num-1>=1) Quary1 (1, num-1,1N1); Fail=0; if(num+1<=n) Quary2 (num+1N1N1); intans=ans1+ans2+1; printf ("%d\n", ans); } } Else if(op[0]=='R') { if(S.empty ())Continue; Num=S.top (); S.pop (); Flag[num]=1; Update (1Num1N1); } } } return 0;}
HDU 1540 POJ 2892 tunnel Warfare