POJ 3468 A Simple Problem with Integers(线段树)

来源:互联网 发布:淘宝店铺免费一键复制 编辑:程序博客网 时间:2024/06/05 15:37
A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072KTotal Submissions: 107448 Accepted: 33516Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 51 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


线段树区间更新裸题


#include <stdio.h>#include <string.h>using namespace std;const int N = 100000 + 10;long long n, m;struct xx{    long long l, r, v, add;}T[N*4];void Pushup(long long k){    T[k].v = T[k<<1].v + T[k<<1|1].v;}void Pushdown(long long k){    T[k<<1].add += T[k].add;    T[k<<1|1].add += T[k].add;    T[k<<1].v += T[k].add*(T[k<<1].r-T[k<<1].l+1);    T[k<<1|1].v += T[k].add*(T[k<<1|1].r-T[k<<1|1].l+1);    T[k].add = 0;}void Build(long long l, long long r, long long k){    T[k].l = l, T[k].r = r, T[k].add = 0;    if(l == r){        scanf("%lld", &T[k].v);        return;    }    long long mid = (l+r)>>1;    Build(l, mid, k<<1);    Build(mid+1, r, k<<1|1);    Pushup(k);}void Update(long long l, long long r, long long c, long long k){    if(l > T[k].r || r < T[k].l){        return;    }    if(l <= T[k].l && r >= T[k].r){        T[k].add += c;        T[k].v += c*(T[k].r-T[k].l+1);        return;    }    if(T[k].add) Pushdown(k);    Update(l, r, c, k<<1);    Update(l, r, c, k<<1|1);    Pushup(k);}long long ans;void Query(long long l, long long r, long long k){    if(l > T[k].r || r < T[k].l){        return;    }    if(l <= T[k].l && r >= T[k].r){        ans += T[k].v;        return;    }    if(T[k].add) Pushdown(k);    long long mid = (T[k].l+T[k].r)>>1;    if(r <= mid) Query(l, r, k<<1);    else if(l > mid) Query(l, r, k<<1|1);    else{        Query(l, mid, k<<1);        Query(mid+1, r, k<<1|1);    }}int main(){    while(scanf("%lld%lld", &n, &m) == 2){        Build(1, n, 1);        while(m--){            char op[5];            long long u, v, w;            scanf("%s", op);            if(op[0] == 'Q'){                scanf("%lld%lld", &u, &v);                ans = 0;                Query(u, v, 1);                printf("%lld\n", ans);            }            else{                scanf("%lld%lld%lld", &u, &v, &w);                Update(u, v, w, 1);            }        }    }}



0 0
原创粉丝点击