POJ 3468 A simple problem with integers (segment tree interval update interval summation)

Source: Internet
Author: User

A simple problem with integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 67511 Accepted: 20818
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

Hint

The sums may exceed the range of 32-bit integers.
#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<vector>#include<queue>#include<map>#include<Set>#include<stack>#include<algorithm>using namespacestd;#defineRoot 1,n,1#defineLson l,mid,rt<<1#defineRson mid+1,r,rt<<1|1#defineLR rt<<1#defineRR rt<<1|1typedefLong LongLL;Const intOO = 1e9+7;Const DoublePI = ACOs (-1.0);Const DoubleEPS = 1e-6 ;Const intN =100010;Const intMoD =2333333;intN, M; LL Sum[n<<2], lazy[n<<2], e[n], tot;voidUp (intRT) {Sum[rt]= SUM[LR] +SUM[RR];}voidDown (intLintRintRT) {    if(Lazy[rt]! =0 ) {        intMid = (l+r) >>1; SUM[LR]+ = lazy[rt]* (mid-l+1), SUM[RR] + = lazy[rt]* (rmid); LAZY[LR]+ = Lazy[rt], LAZY[RR] + =Lazy[rt]; LAZY[RT]=0 ; }}voidBuildintLintRintRT) {Lazy[rt]=0 ; if(L = =R) {Sum[rt]= e[tot++]; return ; }    intMid = (l+r) >>1;    Build (Lson), build (Rson); Up (RT);}voidUpdateintLintRintRtintLintR, LL val) {    if(L = = L && r = =R) {Sum[rt]+ = val* (r-l+1) ; LAZY[RT]+=Val; return ;    } down (l, R, RT); intMid = (l+r) >>1; if(R <=mid) Update (LSON,L,R,VAL); Else if(L >mid) Update (RSON,L,R,VAL); ElseUpdate (Lson,l,mid,val), update (rson,mid+1, R,val); Up (RT);} LL Query (intLintRintRtintLintR) {if(L = = L && r = =R) {returnSum[rt];    } down (L,R,RT); intMid = (l+r) >>1; if(R <= mid)returnquery (LSON,L,R); Else if(L > Mid)returnquery (RSON,L,R); Else returnQuery (LSON,L,MID) + query (rson,mid+1, R);}intMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);//freopen ("OUT.txt", "w", stdout);    #endif //LOCAL    Chars[Ten];intx, y;    LL C;  while(~SCANF ("%d%d",&n,&m)) {tot=0 ;  for(inti =0; I < n; ++i) {scanf ("%i64d",&E[i]);        } build (root);  while(m--) {scanf ("%s", s); if(s[0] =='Q') {scanf ("%d%d",&x,&y); printf ("%i64d\n", Query (root,x,y)); }            Else{scanf ("%d%d%i64d",&x,&y,&c);            Update (ROOT,X,Y,C); }        }    }}
View Code

POJ 3468 A simple problem with integers (segment tree interval update interval summation)

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.