poj3468

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define maxn 100005
typedef long long ll;
ll sum[maxn<<2];
ll lazy[maxn<<2];
inline void pushup(int rt){
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
inline void pushdown(int rt,int m){
    if(lazy[rt]){
        lazy[rt<<1]+=lazy[rt];
        lazy[rt<<1|1]+=lazy[rt];
        sum[rt<<1]+=(m-(m>>1))*lazy[rt];
        sum[rt<<1|1]+=(m>>1)*lazy[rt];
        lazy[rt]=0;
    }
}
void build(int l,int r,int rt){
    lazy[rt]=0;
    if(l==r){
        scanf("%lld",&sum[rt]);
        return;
    }
    int m=l+r>>1;
    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){
        lazy[rt]+=val;
        sum[rt]+=(r-l+1)*val;
        return;
    }
    pushdown(rt,r-l+1);
    int m=l+r>>1;
    if(L<=m) update(L,R,val,lson);
    if (R>m) 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>>1;
    ll ret=0;
    if(L<=m) ret+=query(L,R,lson);
    if(R>m) ret+=query(L,R,rson);
    return ret;
}
int main(){
    int n,q,a,b,c;
    while(scanf("%d%d",&n,&q)==2){
        build(1,n,1);
        char op[5];
        while(q--){
            scanf("%s",op);
            if(op[0]=='Q'){
                scanf("%d%d",&a,&b);
                printf("%lld\n",query(a,b,1,n,1));
            }
            else {
                scanf("%d%d%d",&a,&b,&c);
                update(a,b,c,1,n,1);
            }
        }
    }
    return 0;
}

 

posted on 2018-11-05 17:01  zsben  阅读(101)  评论(0编辑  收藏  举报

导航