HDU 1166 Segment Tree (single point increment/decrement interval sum)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.