A simple problem with integers |
Time limit:3000 MS |
Memory limit:32768 K |
Total submit:178 (users) |
Total accepted:51 (users) |
Rating: |
Special Judge:no |
|
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 |
Input contain multiple test cases,for each case: 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 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. |
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 |
Source |
POJ monthly--2007.11.25, Yang Yi |
Recommend |
' Wind @Hrbust |
Segment Tree interval Update, if really all update each node, it will be time-consuming, so on a viable node, so that the next query when the query to this node, and then update the
#include <cstdio>#include<cmath>#include<queue>#include<iostream>using namespacestd;Const intm=100000; typedefLong LongLL;structnode{LL Inc; LL sum;} A[m<<2]; LL b[m+1];voidBuildintLintRintRT) {A[rt]. INC=0; if(l==R) {//scanf ("%lld", &b[l]);a[rt].sum=B[l]; return ; } intlr=rt<<1,rr=rt<<1|1; intMid= (l+r) >>1; Build (L,MID,LR); Build (Mid+1, R,RR); A[rt].sum=a[lr].sum+a[rr].sum;}voidUpdateintLintRintLintRintrt,ll x) { if(l==l&&r==R) {A[rt]. INC+=x; return ; } a[rt].sum+ = (r-l+1)*x; intM= (l+r) >>1; intlr=rt<<1,rr=rt<<1|1; if(r<=m) {update (L,R,L,M,LR,X); } Else if(l>m) {Update (L,r,m+1, r,rr,x); } Else{update (L,M,L,M,LR,X); Update (M+1, r,m+1, r,rr,x); }}ll Query (intLintRintLintRintRT) { if(l<=l&&r>=R) {returnA[RT].SUM+A[RT]. inc* (r-l+1); } a[rt].sum+ = (r-l+1)*A[rt]. INC; intM= (l+r) >>1; intlr=rt<<1,rr=rt<<1|1; if(A[rt]. INC) {//Update (L,M,L,M,LR,A[RT). INC); //Update (M+1,R,M+1,R,RR,A[RT). INC);A[LR]. inc+=A[rt]. INC; A[RR]. INC+=A[rt]. INC; } A[rt]. INC=0; if(r<=m) {returnquery (L,R,L,M,LR); } Else if(l>m) {returnQuery (l,r,m+1, R,RR); } LL Left=query (L,M,L,M,LR); LL Right=query (m+1, r,m+1, R,RR); returnleft+Right ;}intMain () {intn,q; while(~SCANF ("%d%d",&n,&Q)) {inti; for(i=1; i<=n;i++) {scanf ("%lld",&B[i]); } Build (1N1); CharC; intL,r; LL x; while(q--) {cin>>C; if(c=='Q') {scanf ("%d%d",&l,&R); printf ("%lld\n", Query (L,r,1N1)); } Else if(c=='C') { //cout<< "DSFDSF" <<endl;scanf"%d%d%lld",&l,&r,&x); Update (L,R,1N1, x); } } } return 0;}
A simple problem with integers (interval update)