poj3468 A simple problem with integers (segment tree template function: Interval increment, interval sum)

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/u012860063

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

field=source&key=poj+monthly--2007.11.25 "style=" Text-decoration:none ">poj Monthly--2007.11.25, Yang Yi


The code is as follows:

Segment Tree function: Update: Add or subtract query: interval sum//This is titled POJ 3468 Code # include <cstdio> #include <algorithm>using namespace std;# Define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT << 1 | 1//lson and Rson identify the node's left son and right son,//rt, which represents the root of the current subtree (root), which is the current node, # define LL long longconst int MAXN = 111111;//MAXN is the maximum interval given to the topic, and the number of nodes to open 4 times times, the exact number of nodes to open more than maxn of the minimum 2x twice times ll add[maxn<<2];//used to mark each node, 0 is not marked, otherwise marked. LL sum[maxn<<2];//sum void pushup (int rt)//Update the information of the current node to the parent node {SUM[RT] = sum[rt<<1] + sum[rt<<1|1];} void pushdown (int rt,int len)//Update the information of the current node to the son node, Len for the partition between the length of {//to a certain interval changes, assuming that it is marked, the query must be changed to the child node, because the query is not necessarily the current interval if ( ADD[RT])//has been marked. The interval has been changed {//due to the RT's son node may be marked by multiple delays. and the delay mark of the son node of RT does not move to the sun grandson node of RT, so with "+ =" add[rt<<1] + add[rt];add[rt<<1|1] + = add[rt];/* here with Add[rt] multiplied by interval length, Not add[rt<<1], since RT's son node assumes that it was marked repeatedly, it has been updated to sum[rt<<1].

*/SUM[RT&LT;&LT;1] + = add[rt] * (Len-(Len >> 1));//update left son's and sum[rt<<1|1] + + add[rt] * (len >> 1 );//Update Right son's and add[rt] = 0;//After the tag is moved to the son node. The parent node's delay token is removed}}void build (int l,int R,int RT) {Add[rt] = 0;//initialized to all nodes not labeled if (L = = r) {scanf ("%lld", &sum[rt]); return;} int mid = (L + R) >> 1;build (Lson); build (Rson); Pushup (RT);} void update (int l,int r,int c,int l,int r,int RT) {if (L <= l && R <= R) {Add[rt] + = C;sum[rt] + = (LL) c * (R -L + 1);//Update the node that represents an interval and that node is not necessarily a leaf node return;} /* When you want to update the son node of this interval marked by delay, first move the delay tag to the son node of course, assuming there has been no update to the son node of the segment, the delay tag does not need to move to the son node, so that the time complexity of the update operation is still O (Logn), is also the reason for using the delay token. */pushdown (RT, r-l + 1);//down pass int mid = (L + R) >> 1;if (L <= mid) Update (L, R, C, Lson);//Update left son if (mid &l T R) Update (L, R, C, Rson);//Update right son Pushup (RT);//Up-pass Update and}LL query (int l,int r,int l,int r,int RT) {if (L <= l && R <= R) {return SUM[RT];} The value of the RT child node to be taken. It is also first to move the RT delay tag down pushdown (RT, r-l + 1); int mid = (L + r) >> 1; LL ret = 0;if (L <= mid) ret + = query (l, R, Lson), if (Mid < R) RET + = query (l, R, Rson); return ret;} int main () {int N, q;scanf ("%d%d", &n,&q),//n is the number of nodes build (1, N, 1);//achievements while (q--)//q is the number of queries {char op[2];int A, B, C;SCANF ("%s", op), if (op[0] = = ' Q ') {scanf ("%d%d", &a,&b);p rintf ("%lld\n", query (A, B, 1, N, 1));} ELSE{SCANF ("%d%d%d", &a,&b,&c);//c added value for interval A to B update (A, B, C, 1, N, 1);}} return 0;}



poj3468 A simple problem with integers (segment tree template function: 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.