Sample Input
1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
ADD 3 6
Query 2 7
Sub 10 2
ADD 6 3
Query 3 10
End
Sample Output
Case 1:
6
33
59
1# include <iostream>2# include <cstdio>3# include <cstring>4# include <algorithm>5# include <cmath>6# include <queue>7# define LLLong Long8 using namespacestd;9 Ten Const intMAXN =50010; One A intsum[maxn<<2] ;//4 times times the knot. - - voidPushup (intRt//Update to parent node the { -SUM[RT] = Sum[rt *2] + Sum[rt *2+1] ;//RT is the current node. - } - + voidBuildintLintRintRt//Building a segment tree - { + if(L = =R) A { atscanf"%d", &Sum[rt]); - return ; - } - intm = (L + r)/2 ; -Build (L, M, RT *2) ; -Build (M +1, R, RT *2+1) ; in pushup (RT); - } to + voidUpdata (intPintAdd,intLintRintRt//single point increase or decrease - { the if(L = =R) * { $SUM[RT] + =add;Panax Notoginseng return ; - } the intm = (L + r)/2 ; + if(P <=m) AUpdata (P, add, L, M, RT *2) ; the Else +Updata (P, add, M +1, R, RT *2+1) ; - pushup (RT); $ } $ - intQueryintLintRintLintRintRt//Interval summation - { the if(l <= l && R <=R) - returnSum[rt];Wuyi intm = (L + r)/2 ; the intRET =0 ; - if(L <=m) WuRET + = query (l, R, L, M, RT *2) ; - if(R >m) AboutRET + = query (L, R, M +1, R, RT *2+1) ; $ returnret; - } - - intMain () A { + //freopen ("In.txt", "R", stdin); the intT, N; - intCase =0 ; $scanf"%d", &T); the while(t--) the { thescanf"%d", &n); theBuild1N1) ; - Charop[Ten] ; incase++ ; theprintf"Case %d:\n", case); the while(SCANF ("%s", op)) About { the if(op[0] =='E')//End the Break ; the intA, B; +scanf"%d%d", &a, &b); - if(op[0] =='Q')//the and of the interval of a, b theprintf"%d\n", query (A, B,1N1)) ;Bayi Else if(op[0] =='S')//reduce the first place to B theUpdata (A,-B,1N1) ; the Else if(op[0] =='A')//Add the first place to B -Updata (A, B,1N1) ; - } the } the the the return 0 ; -}View Code
HDU 1166 Segment Tree (single point increment/decrement interval sum)