Topic Connection: http://acm.hdu.edu.cn/showproblem.php?pid=1166
Segment Tree function: Update: Single point of increase or decrease query: interval sum
#pragmaComment (linker, "/stack:102400000,102400000")#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<iostream>#include<algorithm>#include<queue>#include<cstdlib>#include<stack>#include<vector>#include<Set>#include<map>#defineLL Long Long#defineMoD 1000000007#defineINF 0x3f3f3f3f#defineN 50010#defineFILL (A, B) (Memset (A,b,sizeof (a)))#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1using namespacestd;intsum[n<<2];intA[n];voidPushup (intRT) {Sum[rt]=sum[rt<<1]+sum[rt<<1|1];}voidBuildintLintRintRT) { if(l==R) {Sum[rt]=A[l]; return; } intM= (l+r) >>1; Build (Lson); Build (Rson); Pushup (RT);}voidUpdateintPosintNumintLintRintRT) { if(l==R) {Sum[rt]+=num; return; } intM= (l+r) >>1; if(pos<=m) update (Pos,num,lson); ElseUpdate (Pos,num,rson); Pushup (RT);}intQueryintLintRintLintRintRT) { if(l<=l&&r<=R) {returnSum[rt]; } intM= (l+r) >>1; intres=0; if(l<=m) res+=query (L,r,lson); if(M<r) res+=query (L,r,rson); returnRes;}intMain () {intt,n,cas=1; Charop[Ten]; scanf ("%d",&t); while(t--) {scanf ("%d",&N); for(intI=1; i<=n;i++) scanf ("%d",&A[i]); Build (1N1);//printf ("%d", sum[20]);printf"Case %d:\n", cas++); while(SCANF ("%s", op) >0) { if(op[0]=='E') Break; intb; scanf ("%d%d",&a,&b); if(op[0]=='Q') printf ("%d\n", query (A, B,1N1)); Else if(op[0]=='A') Update (A, B,1N1); ElseUpdate (A,-b,1N1); } }}
View Code
hdu1166 (segment tree)