POJ 3468
Description
Given a sequence, you need to deal with the following two types of queries.
"C A B C" indicates that the values in the [a, b] interval are all increased by C ( -10000≤c≤10000).
"Q a B" asks for the and of all values in the [a, b] interval.
Input
The first line consists of two integers n, Q. 1≤n,q≤100000.
The second line contains n integers that represent the initial sequence a ( -1000000000≤ai≤1000000000).
Next Q Line asks, format as title description.
Output
For each Q start query, you need to output the corresponding answer, one row for each answer.
Sample Input
2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4
Sample Output
455915
Idea: Segment tree interval update.
The subject code is good for the segment tree interval update Template:
#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>#defineLL Long Longusing namespacestd;Const intmaxn=100005;structnode{LL sum,val;} node[4*MAXN];voidPushup (inti) {Node[i].sum=node[i<<1].sum+node[i<<1|1].sum;}voidPushdown (intIintm) { if(node[i].val) {node[i<<1].val+=Node[i].val; Node[i<<1|1].val+=Node[i].val; Node[i<<1].sum+= (LL) node[i].val* (M-(m>>1)); Node[i<<1|1].sum+= (LL) node[i].val* (m>>1); Node[i].val=0; }}voidBuildintLintRinti) {Node[i].val=0; if(l==r) {scanf ("%i64d",&node[i].sum); return ; } intMid= (L+R)/2; Build (L,mid,i<<1); Build (Mid+1,r,i<<1|1); Pushup (i);} LL Query (intLintRintLintRinti) { if(l<=l&&r<=R) {returnnode[i].sum; } intMid= (l+r) >>1; Pushdown (I,r-l+1); LL ans=0; if(L<=mid) Ans+=query (l,r,l,mid,i<<1); if(mid<r) Ans+=query (l,r,mid+1,r,i<<1|1); Pushup (i); returnans;}voidUpdateintLintRintAddintLintRinti) { if(l<=l&&r<=R) {node[i].sum+ = (LL) add* (r-l+1); Node[i].val+=add; return ; } pushdown (I,r-l+1); intMid= (l+r) >>1; if(l<=mid) Update (l,r,add,l,mid,i<<1); if(mid<r) Update (l,r,add,mid+1,r,i<<1|1); Pushup (i);}intMain () {intn,q,a,b; LL C; while(SCANF ("%d%d", &n,&q)! =EOF) {Build (1N1); Chars[3]; while(q--) {scanf ("%s", s); if(s[0]=='C') {scanf ("%d%d%i64d",&a,&b,&c); Update (A,B,C,1N1); } Else if(s[0]=='Q') {scanf ("%d%d",&a,&b); printf ("%i64d\n", query (A, B,1N1)); } } } return 0;}
Interval update of Segment tree---A simple problem with integers