poj 3468A Simple Problem with Integers(成段更新)

来源:互联网 发布:哪个软件可以升级win10 编辑:程序博客网 时间:2024/06/06 20:35

题目链接:http://poj.org/problem?id=3468


#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>using namespace std;#define maxn 100010#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define LL long long#define inf 0x3f3f3f3fLL sum[maxn<<2];LL col[maxn<<2];void PushUp(int rt){    sum[rt] = sum[rt<<1] + sum[rt<<1|1];}void PushDown(int rt,int len){    if(col[rt]){        sum[rt<<1]   += col[rt] * (len - (len>>1));        sum[rt<<1|1] += col[rt] * (len>>1);        col[rt<<1|1] += col[rt];        col[rt<<1]   += col[rt];        col[rt] = 0;    }}void build(int l,int r,int rt){    if(l == r){        scanf("%I64d",&sum[rt]);        col[rt] = 0;        return ;    }    int m = (l + r) /2;    build(lson);    build(rson);    PushUp(rt);}void update(int L,int R,int val,int l,int r,int rt){    if(L <= l && r <= R){        col[rt] += val;        sum[rt] += (LL)(r - l + 1) * val;        return ;    }    PushDown(rt , (r - l + 1));    int m = (l + r) /2;    if(L <= m) update(L,R,val,lson);    if(m <  R) update(L,R,val,rson);    PushUp(rt);}LL query(int L,int R,int l,int r,int rt){    if(L <= l && r <= R) {        return sum[rt];    }    PushDown(rt , r - l + 1);    int m = (l + r)/2;    LL ret = 0;    if(L <= m) ret += query(L,R,lson);    if(m <  R) ret += query(L,R,rson);    return ret;}int main(){    int n,m,a,b,c;    char op[5];    while(~scanf("%d %d",&n,&m)){        build(1,n,1);        while(m--){            scanf("%s",op);            if(op[0] == 'C'){                scanf("%d %d %d",&a,&b,&c);                if(a>b) swap(a,b);                update(a,b,c,1,n,1);            }            else{                scanf("%d %d",&a,&b);                if(a>b) swap(a,b);                printf("%I64d\n",query(a,b,1,n,1));            }        }    }    return 0;}


原创粉丝点击