POJ 3468 Segment Tree Interval summation

Source: Internet
Author: User

segment tree interval sum tree nodes cannot be stored and only saved and will cause each addend to be updated to the leaf node, which is too slow (O (NLOGN)). So we have to save two quantities, one for the original and the Nsum, and one for the cumulative Delta Inc.

In addition, if the interval to be added just covers a node, then increase its node's Inc value, no longer go down, or to update the nsum (plus this increment), and then the increment down, so that the complexity of the update is O (log (n)).

at query time, if the unknown origin interval does not cover exactly one node, bring the node's Inc down, then the Inc 0, and then search down. The process that the INC goes down is also the process of interval decomposition, and the complexity is also O (log (n)).   A simple problem with integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 79334 Accepted: 24455
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
1#include <iostream>2#include <cstdio>3 using namespacestd;4 structCNode5 {6     intL, R;7CNode * Pleft, *Pright;8     Long LongNSum;//the original and9     Long LongINC;//accumulation of increment CTen }; OneCNode tree[200010];//twice times the number of leaf nodes is enough A intNcount =0; - intMid (CNode *proot) - { the     return(Proot->l + proot->r)/2; - } - voidBuildtree (CNode * proot,intLintR) - { +Proot->l =L; -Proot->r =R; +Proot->nsum =0; AProot->inc =0; at     if(L = =R) -         return; -Ncount + +; -Proot->pleft = Tree +ncount; -Ncount + +; -Proot->pright = Tree +ncount; inBuildtree (Proot->pleft,l, (l+r)/2); -Buildtree (Proot->pright, (l+r)/2+1, R); to } + voidInsert (CNode * proot,intIintv) - { the     if(Proot->l = = I && proot->r = =i) *     { $Proot->nsum =v;Panax Notoginseng         return ; -     } theProot->nsum + =v; +     if(I <=Mid (proot)) AInsert (proot->pleft,i,v); the     Else +Insert (proot->pright,i,v); - } $ voidADD (CNode * proot,intAintBLong Longc) $ { -     if(Proot->l = = a && Proot->r = =b) -     { theProot->inc + =C; -         return ;Wuyi     } theProot->nsum + = c * (b-a +1) ; -     if(b <= (proot->l + proot->r)/2) WuADD (proot->pleft,a,b,c); -     Else if(A >= (proot->l + proot->r)/2+1) AboutADD (proot->pright,a,b,c); $     Else -     { -ADD (proot->Pleft,a, -(Proot->l + proot->r)/2, c); AADD (proot->Pright, +(Proot->l + proot->r)/2+1, b,c); the     } - } $ Long LongQuerynsum (CNode * proot,intAintb) the { the     if(Proot->l = = a && Proot->r = =b) the         returnProot->nsum + the(Proot->r-proot->l +1) * proot->Inc; -Proot->nsum + = (proot->r-proot->l +1) * proot->Inc; inADD (Proot->pleft,proot->l,mid (proot),proot->Inc); theADD (Proot->pright,mid (proot) +1,proot->r,proot->Inc); theProot->inc =0; About     if(b <=Mid (proot)) the         returnQuerynsum (proot->pleft,a,b); the     Else if(A >= Mid (proot) +1) the         returnQuerynsum (proot->pright,a,b); +     Else -     { the         returnQuerynsum (Proot->pleft,a,mid (proot)) +BayiQuerynsum (Proot->pright,mid (proot) +1, b); the     } the } - intMain () - { the     intn,q,a,b,c; the     Charcmd[Ten]; thescanf"%d%d",&n,&q); the     inti,j,k; -Ncount =0; theBuildtree (Tree,1, n); the      for(i =1; I <= N; i + + ) the     {94scanf"%d",&a); the Insert (tree,i,a); the     } the      for(i =0; i < Q; i + + )98     { Aboutscanf"%s", cmd); -         if(cmd[0] =='C' )101         {102scanf"%d%d%d",&a,&b,&c);103 Add (tree,a,b,c);104         } the         Else106         {107scanf"%d%d",&a,&b);108printf"%i64d\n", Querynsum (tree,a,b));109         } the     }111     return 0; the}
View Code

POJ 3468 segment tree 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.