A simple problem with integers
Time Limit: 5000MS |
|
Memory Limit: 131072K |
Total Submissions: 72740 |
|
Accepted: 22453 |
Case Time Limit: 2000MS |
Description
You have N integers, a1, a2, ..., an. You need to deal with both kinds of operations. One type of operation is to add some given number to each number in a given interval. The other are to ask for the sum of numbers in a given interval.
Input
The first line contains the numbers N and Q. 1 ≤ N,Q ≤100000.
The second line contains N numbers, the initial values of a1, a2, ..., an. -1000000000≤ Ai ≤1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding C to each of AA, aa+1, ..., ab. -10000≤ C ≤10000.
"Q a b" means querying the sum of aa, aa+1, ..., Ab.
Output
You need to answer all Q commands in order. One answer in a line.
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
Template title.
#include <iostream>#include<cstdio>#include<string>#include<queue>#include<vector>#include<map>#include<algorithm>#include<cstring>#include<cctype>#include<cstdlib>#include<cmath>#include<ctime>#include<climits>using namespacestd;Const intSIZE =100005;Long LongTree[size *4],lazy[size *4],n,q;voidBuildint,int,int);voidUpdateint,int,int,int,int,int);voidPush_down (int,int,int);Long LongQueint,int,int,int,int);intMainvoid){ Charop; intA,b,c; while(~SCANF ("%d%d",&n,&Q) {Build (1,1, N); while(Q--) {scanf ("%c%d%d",&op,&a,&b); if(OP = ='Q') printf ("%lld\n", Que (a, B,1,1, N)); Else{scanf ("%d",&c); Update (A, B,1,1, N,c); } } } return 0;}voidBuildintNodeintLeftintRight ) {Lazy[node]=0; if(left = =Right ) scanf ("%lld",&Tree[node]); Else { intMid = (left + right) >>1; Build (Node*2, Left,mid); Build (Node*2+1, Mid +1, right); Tree[node]= Tree[node *2] + Tree[node *2+1]; }}voidUpdateintLintRintNodeintLeftintRightintadd) { if(left >= L && right <=R) {Tree[node]+ = Add * (Right-left +1); Lazy[node]+=add; return ; } if(Right < L | | left >R)return ; Push_down (Node,left,right); intMid = (left + right) >>1; Update (L,r,node*2, Left,mid,add); Update (L,r,node*2+1, Mid +1, Right,add); Tree[node]= Tree[node *2] + Tree[node *2+1];}Long LongQueintLintRintNodeintLeftintRight ) { if(left >= L && right <=R)returnTree[node]; if(Right < L | | left >R)return 0; Push_down (Node,left,right); intMid = (left + right) >>1; returnQue (L,r,node *2, left,mid) + que (l,r,node *2+1, Mid +1, right);}voidPush_down (intNodeintLeftintRight ) { if(Lazy[node]) {intMid = (left + right) >>1; Tree[node*2] + = Lazy[node] * (Mid-left +1); Lazy[node*2] +=Lazy[node]; Tree[node*2+1] + = Lazy[node] * (Right-mid); Lazy[node*2+1] +=Lazy[node]; Lazy[node]=0; }}
POJ 3468 A simple problem with integers (segment tree)