Segment Tree interval update && summation poj3486

Source: Internet
Author: User

given a sequence, you need to deal with the following two types of queries. "c A B c"Indicates that the values in the [a, b] interval are all increased by C (-10000≤c≤10000). "Q a B"inquire about the and of all values in the [a, b] interval. The first line of input contains two integers n, Q. 1≤n,q≤100000the second row contains n integers that represent the initial sequence a (-1000000000≤ai≤1000000000). The next Q line asks, Sample InputTen 51 2 3 4 5 6 7 8 9 TenQ4 4Q1 TenQ2 4C3 6 3Q2 4Sample Output4 -9 the
#include <iostream>#include<stdio.h>#include<cstring>using namespacestd;#defineMAXSIZE 100005Long LongVal[maxsize];Long Longadd[100010<<2];Long Longsum[100010<<2];Long Longsum1;structnode{Long LongTotal ; intLeft ; intRight ; intMark//delay Mark} tree[maxsize*3];//the following two types of Create are available, select one toLong LongCreateintRootintLeftintRight ) {Add[root]=0; Sum[root]=1; Tree[root].left=Left ; Tree[root].right=Right ; if(left==Right )returnTree[root].total=Val[left]; intMiddle= (left+right) >>1; returnTree[root].total=create (root<<1, Left,middle) +create (root<<1|1, middle+1, right);}//parameter: Ask interval left endpoint, ask interval right end, each position need increment value, current node ordinalvoidUpdateintLintRintXintroot) {    if(L<=tree[root].left && tree[root].right<=R) {//The current interval is included and the corresponding additional information is processedAdd[root] + = x;//Update delay Flagtree[root].total+= x * (tree[root].right-tree[root].left+1); return;//You don't have to go down recursively .    }    intMid = (tree[root].left+tree[root].right) >>1; if(Add[root])//The delay token is not 0, indicating that there are outstanding updates, updated{add[root<<1] +=Add[root]; Add[root<<1|1] +=Add[root]; Tree[root<<1].total + = add[root] * (mid-tree[root].left+1); Tree[root<<1|1].total + = add[root] * (tree[root].right-mid); Add[root]=0;//don't forget to remove the delay tag    }    if(L <= mid)//The section of the left dial hand interval that contains the update interval needs to be updatedUpdate (L, R, X, root<<1); if(R > Mid)//The right sub-range contains the part of the update interval that needs to be updatedUpdate (L, R, X, root<<1|1); Tree[root].total= tree[root<<1].total + tree[root<<1|1].total;//Update from leaf node up}/*void Find (tree *r,int a,int b) {if (r->left==a&&r->right==b) {sum+=r->data;   Return   } int mid= (r->left+r->right) >>1;   if (B<=mid) find (R->LCHILD,A,B);   else if (a>mid) find (R->RCHILD,A,B);    else{find (R->lchild,a,mid);   Find (R->RCHILD,MID+1,B); }}*///long long sum1;Long LongCalintRootintAintb) {    if(a<=tree[root].left&&b>=tree[root].right) {       returnTree[root].total; }     intMid= (tree[root].left+tree[root].right) >>1; if(Add[root]) {add[root<<1] +=Add[root]; Add[root<<1|1] +=Add[root]; Tree[root<<1].total + = add[root] * (mid-tree[root].left+1); Tree[root<<1|1].total + = add[root] * (tree[root].right-mid); Add[root]=0;//don't forget to remove the delay tag        }       if(b<=mid)returnCal (root<<1, A, b); Else if(a>mid)returnCal (root<<1|1, A, b); Else{          returnCal (root<<1, A,mid) +cal (root<<1|1, mid+1, B); }}intMain () {intn,q;  while(SCANF ("%d%d", &n,&q)! =EOF) {         for(intI=1; i<=n;i++) {scanf ("%lld",&Val[i]); } memset (Sum,0,sizeof(sum)); memset (Add,0,sizeof(add)); Long LongTmp=create (1,1, N); CharC;        GetChar (); inta,b,d;  for(intI=1; i<=q;i++) {scanf ("%c",&c); if(c=='C') {scanf (" %d%d%d",&a,&b,&d); Update (A,B,D,1); }            Else{scanf ("%d%d",&a,&b); Sum1=cal (1, A, b); printf ("%lld\n", SUM1);        } getchar (); }    }    return 0;}

Segment Tree interval update && summation poj3486

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.