[POJ] 3468 A simple problem with integers [segment tree interval update sum]

Source: Internet
Author: User

A simple problem with integers

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.

Source

POJ monthly--2007.11.25, Yang Yi

Segment Tree function : Update: segment Add or subtract query: interval sum

1#include <cstdio>2#include <algorithm>3 4 #defineCLR (x, y) memset (x,y,sizeof (×))5 #defineLL Long Long6 #defineLson l,m,rt<<17 #defineRson m+1,r,rt<<1|18 9 Const intmaxn=1e5+3511;Ten using namespacestd; One  ALL sum[maxn<<2],lazy[maxn<<2]; -  - voidPushup (intRT) the { -sum[rt]=sum[rt<<1]+sum[rt<<1|1]; - } -  + voidPushdown (intRtintm) - { +     if(Lazy[rt]) { Alazy[rt<<1]+=Lazy[rt]; atlazy[rt<<1|1]+=Lazy[rt]; -sum[rt<<1]+= (M-(m>>1))*Lazy[rt]; -sum[rt<<1|1]+= (m>>1)*Lazy[rt]; -lazy[rt]=0; -     }     - } in  - voidBuildintLintRintRT) to { +     intm; -lazy[rt]=0; the     if(l==r) { *scanf"%lld",&Sum[rt]); $         return;Panax Notoginseng     } -      theM= (l+r) >>1; + build (Lson); A build (Rson); the pushup (RT); + } -  $ voidUpdata (intLintRintCintLintRintRT) $ { -     intm; -     if(L<=l && r<=R) { thelazy[rt]+=C; -sum[rt]+= (LL) c* (r-l+1);Wuyi         return; the     } -      WuPushdown (rt,r-l+1); -M= (l+r) >>1; About     if(l<=m) updata (L,r,c,lson); $     if(r>m) updata (L,r,c,rson); - pushup (RT); -      - } A  +  theLL Query (intLintRintLintRintRT) - { $     intm; theLL ret=0; the     if(L<=l && r<=R) { the         returnSum[rt]; the     } -      inPushdown (rt,r-l+1); theM= (l+r) >>1; the     if(l<=m) ret+=query (L,r,lson); About     if(r>m) ret+=query (L,r,rson); the      the     returnret; the } +  -  the Bayi intMain () the { the     intq,n,a,b,c; -     Charst[Ten]; -      thescanf"%d%d",&n,&Q); theBuild1N1); the      the      while(q--) { -scanf"%s", ST); the         if(st[0]=='C') { thescanf"%d%d%d",&a,&b,&c); theUpdata (A,b,c,1N1);94}Else { thescanf"%d%d",&a,&b); theprintf"%lld\n", query (A, B,1N1)); the         }98          About     } -     101     return 0;102}

[POJ] 3468 A simple problem with integers [segment tree interval update sum]

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.