• 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
    ",query(a,b,1,n,1));
                }
                else {
                    scanf("%d%d%d",&a,&b,&c);
                    update(a,b,c,1,n,1);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    Enum, Generic and Templates
    Writing A Threadpool in Rust
    A First Look at Rust Language
    Closures in OOC
    Complexity Behind Closure
    Introduction to OOC Programming Language
    OOC,泛型,糟糕的设计。
    Enlightenment笔记
    Machine Learning/Random Projection
    Machine Learning/Introducing Logistic Function
  • 原文地址:https://www.cnblogs.com/zsben991126/p/9910105.html
Copyright © 2020-2023  润新知