POJ 3468 A simple problem with integers

Source: Internet
Author: User

A simple problem with integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 92522 Accepted: 28778
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 Parse: Segment tree, Interval update.
1#include <cstdio>2 3 structnode{4     intL, R;5     Long LongVal, add;6 };7Node node[100005<<2];8 9 voidBuildintVintLintR)Ten { OneNODE[V].L =l; ANODE[V].R =R; -Node[v].add =0; -     if(NODE[V].L = =NODE[V].R) { thescanf"%i64d", &node[v].val); -         return; -     } -     intMid = (l+r) >>1; +Build (v<<1, L, mid); -Build (v<<1|1, mid+1, R); +Node[v].val = node[v<<1].val+node[v<<1|1].val; A } at  - voidPushdown (intv) - { -     if(node[v].add) { -node[v<<1].add + =Node[v].add; -node[v<<1|1].add + =Node[v].add; in         intm = node[v].r-node[v].l+1; -node[v<<1].val + = (m (m>>1))*Node[v].add; tonode[v<<1|1].val + = (m>>1)*Node[v].add; +Node[v].add =0; -     } the } *  $ Long LongQueryintVintLintR)Panax Notoginseng { -     if(NODE[V].L = = L && NODE[V].R = =R) the         returnNode[v].val; +Pushdown (v);//Update sub-range A     intMid = (NODE[V].L+NODE[V].R) >>1; the     if(R <=mid) +         returnQuery (v<<1, L, R); -     Else if(L >mid) $         returnQuery (v<<1|1, L, R); $     Else -         returnQuery (v<<1, L, Mid) +query (v<<1|1, mid+1, R); - } the  - voidUpdateintVintLintRintc)Wuyi { the     if(NODE[V].L = = L && NODE[V].R = =R) { -Node[v].val + = (r-l+1)*C; WuNode[v].add + =C; -         return; About     } $Pushdown (v);//Update sub-range -     intMid = (NODE[V].L+NODE[V].R) >>1; -     if(R <=mid) -Update (v<<1, L, R, c); A     Else if(L >mid) +Update (v<<1|1, L, R, c); the     Else{ -Update (v<<1, L, Mid, c); $Update (v<<1|1, mid+1, R, c); the     } theNode[v].val = node[v<<1].val+node[v<<1|1].val; the } the  - intMain () in { the     intN, Q; thescanf"%d%d", &n, &Q); AboutBuild1,1, N); the     Charorder[5]; the     intA, B, C; the      while(q--){ +scanf"%s", order); -         if(order[0] =='Q'){ thescanf"%d%d", &a, &b);Bayiprintf"%i64d\n", Query (1, A, b)); the         } the         Else{ -scanf"%d%d%d", &a, &b, &c); -Update1, A, b, c); the         } the     } the     return 0; the}

POJ 3468 A simple problem with integers

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.