luogu_P3368【模板】树状数组 2

树状数组记录修改,差分

#include<iostream>
#include<cstdio>

#define ri register int
#define u int

namespace opt {

    inline u in() {
        u x(0),f(1);
        char s(getchar());
        while(s<'0'||s>'9') {
            if(s=='-') f=-1;
            s=getchar();
        }
        while(s>='0'&&s<='9') {
            x=(x<<1)+(x<<3)+s-'0';
            s=getchar();
        }
        return x*f;
    }

}

using opt::in;

#define NN 500005

namespace mainstay {

    u N,K;

    u c[NN],a[NN];

    inline u ask(const u &x) {
        u _re(0);
        for(ri i(x); i; i-=i&-i) _re+=c[i];
        return _re;
    }

    inline void add(const u &x,const u &y) {
        for(ri i(x); i<=N; i+=i&-i) c[i]+=y;
    }

    inline void solve() {
        N=in(),K=in();
        for(ri i(1); i<=N; ++i) a[i]=in();
        for(ri i(1); i<=K; ++i) {
            u _k(in());
            if(_k==1) {
                u _a(in()),_b(in()),_c(in());
                add(_a,_c),add(_b+1,-_c);
            } else {
                u _a(in());
                printf("%d
",ask(_a)+a[_a]);
            }
        }
    }

}

int main() {

    //freopen("x.txt","r",stdin);
    std::ios::sync_with_stdio(false);
    mainstay::solve();

}
原文地址:https://www.cnblogs.com/ling-zhi/p/11782526.html