POJ 3468--a simple problem with integers —————— "segment tree interval update, interval query"

Source: Internet
Author: User

A simple problem with integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 86780 Accepted: 26950
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 topic: give you n number, m times ask. Queries include the addition of C for each value in the interval and all values within the query interval. Problem-solving ideas: delay markers. Ensure that the current value is correct. Lazy cannot be emptied and should accumulate because the last lazy may not have been pushed down.
#include <stdio.h> #include <vector> #include <queue> #include <string.h> #include < Algorithm>using namespace Std;typedef long long ll;const int MAXN = 1e6+20;const int INF = 0x3f3f3f3f;const int mod = 1 e9+7; #define MID (L+r)/2#define Lson rt*2,l,mid#define rson rt*2+1,mid+1,rstruct segtree{LL val, lazy;} segs[maxn*4];void pushup (int rt) {Segs[rt].val = Segs[rt*2+1].val + segs[rt*2].val;}        void pushdown (int rt,int l,int R) {if (Segs[rt].lazy) {segs[rt*2].val + = segs[rt].lazy* (mid-l + 1);//Guaranteed Current Correctness        Segs[rt*2+1].val + = segs[rt].lazy* (R-mid);   Segs[rt*2].lazy + = Segs[rt].lazy;        Cumulative Segs[rt*2+1].lazy + = Segs[rt].lazy;    Segs[rt].lazy = 0;    }}void buildtree (int rt,int l,int R) {segs[rt].val = 0;    Segs[rt].lazy = 0;        if (L = = R) {scanf ("%lld", &segs[rt].val);    return;    } buildtree (Lson);    Buildtree (Rson); Pushup (RT);} void Update (int rt,int l,int r,int _idx,int _val) {//if (L = = R && L= = _idx) {//Segs[rt].val + = _val;//return;//}//if (_idx <= mid)//Update (Lson,_idx,_val); else//Update (rson,_idx,_val);//pushup (RT);//}ll query (int rt,int l,int r,int l_ran,int R_ran) {if (l_r    An <= l&&r <= R_ran) {return segs[rt].val;    } pushdown (Rt,l,r);    LL ret = 0;    if (L_ran <= mid) {ret + = query (Lson,l_ran,r_ran);    } if (R_ran > mid) {ret + = query (Rson,l_ran,r_ran);    } pushup (RT); return ret;} void Update (int rt,int l,int r,int l_ran,int r_ran,ll chg) {if (L_ran <= l&&r <= r_ran) {SEGS[RT].V        Al + = chg* (r-l+1);        Segs[rt].lazy + = Chg;    return;    } pushdown (Rt,l,r);    if (L_ran <= mid) Update (LSON,L_RAN,R_RAN,CHG);    if (R_ran > Mid) Update (RSON,L_RAN,R_RAN,CHG); Pushup (RT);}    int main () {int n,m;        while (scanf ("%d%d", &n,&m)!=eof) {buildtree (1,1,n);        int l,r;        LL C;        Char s[12];for (int i = 1; I <= m; i++) {scanf ("%s", s);                if (s[0]== ' Q ') {scanf ("%d%d", &l,&r);                LL ans = query (1,1,n,l,r);            printf ("%lld\n", ans);                }else{scanf ("%d%d%lld", &l,&r,&c);            Update (1,1,N,L,R,C); }}} return 0;}

  

POJ 3468--a simple problem with integers —————— "segment tree interval update, interval query"

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.