POJ3468 (segment tree interval increment, interval sum)

Source: Internet
Author: User

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

Source

POJ monthly--2007.11.25, Yang Yi Note the update for the lazy tag
/** @Author: link* @Date: 2015-10-31 18:34:32* @Last Modified by:link* @Last Modified time:2015-10-31 23:11:18*/#include<map>#include<Set>#include<cmath>#include<stack>#include<queue>#include<vector>#include<cstdio>#include<string>#include<utility>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;#defineEPS 1e-8#defineRandin Srand ((unsigned int) time (NULL))#defineInput freopen ("Input.txt", "R", stdin)#defineDebug (s) cout << "s =" << s << endl;#defineOutstars cout << "*************" << Endl;Const DoublePI = ACOs (-1.0);Const intINF =0x3f3f3f3f;Const intINF =0x7fffffff; typedefLong Longll;Const intMAXN =100010;structNode {ll sum, add;} tree[maxn<<2];intN, Q;ll NUM[MAXN];voidBuildintRtintLintr) {if(L = =r) {tree[rt].sum=Num[l]; Tree[rt].add=0; return; }    intMid = (L + r) >>1; Build (Rt<<1, L, mid); Build (Rt<<1|1, Mid +1, R); Tree[rt].sum= Tree[rt <<1].sum + tree[rt <<1|1].sum; Tree[rt].add=0;}voidUpdateintRtintLintRintLintR, LL Val) {    if(l <= l && R >=r) {Tree[rt].add+=Val; return; } tree[rt].sum+ = val * (min (R, R)-Max (L, L) +1); intMid = (L + r) >>1; if(R <= mid) update (RT <<1, L, Mid, L, R, Val); Else if(L > Mid) update (RT <<1|1, Mid +1, R, L, R, Val); Else{update (RT<<1, L, Mid, L, R, Val); Update (RT<<1|1, Mid +1, R, L, R, Val); }}ll Query (intRtintLintRintLintR) {if(l <= l && R >=r) {returnTree[rt].sum + Tree[rt].add * (R-l +1); }    intMid = (L + r) >>1; if(tree[rt].add) {tree[rt].sum+ = (r-l +1) *Tree[rt].add; Tree[rt<<1].add + =Tree[rt].add; Tree[rt<<1|1].add + =Tree[rt].add; Tree[rt].add=0; }    if(R <= mid)returnQuery (RT <<1, L, Mid, L, R); if(L > Mid)returnQuery (RT <<1|1, Mid +1, R, L, R); returnQuery (RT <<1, L, Mid, L, R) + query (RT <<1|1, Mid +1, R, L, r);}intMain () {Charop; intA, B;    ll C;  while(~SCANF ("%d%d", &n, &q)) { for(inti =1; I <= N; i + +) scanf ("%lld", &Num[i]); Build (1,1, N);  while(Q--) {scanf ("%c%d%d", &op, &a, &b); if(OP = ='Q') printf ("%lld\n", Query (1,1, N, a, b)); Else{scanf ("%lld", &c); Update (1,1, N, a, b, c); }         }    }    return 0;}

POJ3468 (segment tree interval increment, interval 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.