[Luogu] 树状数组

https://www.luogu.org/problemnew/show/P3374

单点修改,区间查询

#include <iostream>
#include <cstdio>

using namespace std;
const int N = 5e5 + 10;

#define yxy getchar()

int T[N], n, Ty;

inline int read() {
    int x = 0, f = 1; char c = yxy;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = yxy;}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = yxy;
    return x * f;
}

int lowbit(int x) {return x & (- x);}

inline void Add(int id, int num) {
    while(id <= n) {
        T[id] += num;
        id += lowbit(id);
    }
}

inline int Sum(int x) {
    int ret = 0;
    while(x) {
        ret += T[x];
        x -= lowbit(x);
    } return ret;
}

int main() {
    n = read();
    Ty = read();
    for(int i = 1; i <= n; i ++) {
        int x = read();
        Add(i, x);
    }
    while(Ty --) {
        int opt = read(), x = read(), y = read();
        if(opt == 1) Add(x, y);
        else cout << Sum(y) - Sum(x - 1) << endl;
    }
    
    return 0;
}

https://www.luogu.org/problemnew/show/P3368

区间修改,单点查询

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;
const int N = 5e5 + 10;

#define yxy getchar()

int T[N], A[N];
int n, Ty;

inline int read() {
    int x = 0, f = 1;
    char c = yxy;
    while(c < '0' || c > '9') {
        if(c == '-') f = -1;
        c = yxy;
    }
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = yxy;
    return x * f;
}

inline int lowbit(int x) {
    return x & - x;
}

inline void Add(int x, int num) {
    while(x <= n) {
        T[x] += num;
        x += lowbit(x);
    }
}

inline int Sum(int x) {
    int ret = 0;
    while(x) {
        ret += T[x];
        x -= lowbit(x);
    } return ret;
}

int main() {
    n = read();
    Ty = read();
    for(int i = 1; i <= n; i ++) A[i] = read();
    while(Ty --) {
        int opt = read();
        if(opt == 1) {
            int x = read(), y = read(), k = read();
            Add(x, k); Add(y + 1, -k);
        } else {
            int x = read();
            cout << A[x] + Sum(x) << endl;
        }
    }
    return 0;
}

P2068 统计和

https://www.luogu.org/problemnew/show/P2068

#include <iostream>
#include <cstdio>

using namespace std;
const int N = 1e5 + 10;

#define yxy getchar()
#define R freopen("gg.in", "r", stdin)

int T[N], n, Ty;

inline int read() {
    int x = 0, f = 1; char c = yxy;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = yxy;}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = yxy;
    return x * f;
}

inline int lowbit(int x) {
    return x & (-x);
}

inline void Add(int x, int y) {
    while(x <= n) {
        T[x] += y;
        x += lowbit(x);
    }
}

inline int Sum(int x){
    int ret = 0;
    while(x) {
        ret += T[x];
        x -= lowbit(x);
    } return ret;
}

int main() {
    n = read();
    Ty = read();
    while(Ty --) {
        char c = yxy;
        int x = read(), y = read();
        if(c == 'x') Add(x, y);
        else cout << Sum(y) - Sum(x - 1) << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/shandongs1/p/8422295.html