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.
Test instructions is well understood, is the standard line-segment tree interval update problem, can be said to be a template.
However, because the writing is not skilled, was the pit two WA, the wrong place in the code is marked out ...
The code is as follows:
#include <iostream>#include<cstdio>#defineLson l,m,po*2#defineRson m+1,r,po*2+1using namespacestd;Long Longbit[100005*4];Long Longcol[100005*4];voidPushup (intPO) {Bit[po]=bit[po*2]+bit[po*2+1];}voidPushdown (intPointLen) { if(Col[po]) {Bit[po*2]+=(Long Long) col[po]* (len-(len/2)); Bit[po*2+1]+=(Long Long) col[po]* (len/2); Col[po*2]+=COL[PO];//Note is + =, not =!!! col[po*2+1]+=Col[po]; COL[PO]=0; }}voidBuild_tree (intLintRintPO) { if(l==R) {Col[po]=0; CIN>>Bit[po]; return; } intM= (L+R)/2; Build_tree (Lson); Build_tree (Rson); Pushup (PO);}Long LongQueryintQlintQrintLintRintPO) { if(ql<=l&&qr>=R)returnBit[po]; Pushdown (PO, (R-l+1)); intM= (L+R)/2; if(qr<=M)returnquery (Ql,qr,lson); if(ql>M)returnquery (Ql,qr,rson); returnQuery (Ql,qr,rson) +query (Ql,qr,lson);}voidUpdateintUlinturintAddintLintRintPO) { if(ul<=l&&ur>=R) {Bit[po]+=(Long Long) add* (r-l+1); COL[PO]+=(Long Long) add; return; } pushdown (Po,r-l+1); intM= (L+R)/2; if(ul<=M) Update (Ul,ur,add,lson); if(ur>M) Update (Ul,ur,add,rson); Pushup (PO);}intMain () {intn,q; CharC; intA,b,c; while(~SCANF ("%d%d",&n,&Q) {Build_tree (1N1); for(intI=0; i<q;++i) {cin>>b; if(c=='Q') {scanf ("%d%d",&a,&b); cout<<query (A, B,1N1) <<Endl; } Else{scanf (" %d%d%d",&a,&b,&c); Update (A,B,C,1N1); } } } return 0;}
View Code
(simple) POJ 3468 A easy problem with integers, segment tree + interval update.