1-dimensional array of tree: a single point of modification, the query interval

Title Description

Thinking


update is update from (x, y) at the down, sum is seeking (x, y) to (0, 0), and

Code

#include <cstdio>
#include <cstring>

int n, m;
long long arr[5005][5005];
int lowbit[5005];
void update(int x, int y, int z) {
    while (x <= n) {
        int j = y;
        while (j <= m) {
            arr[x][j] += z;
            j += lowbit[j];
        }
        x += lowbit[x];
    }
}
long long sum(int x, int y) {
    long long res = 0;
    while (x) {
        int j = y;
        while (j) {
            res += arr[x][j];
            j -= lowbit[j];
        }
        x -= lowbit[x];
    }
    return res;
}
int main() {
    scanf("%d %d", &n, &m);
    for (int i = 1; i < 5005; ++i) lowbit[i] = i & (-i);
    int a, b, c, d, e;
    while (scanf("%d", &a) != EOF) {
        if (a == 1) {
            scanf("%d %d %d", &b, &c, &d);
            update(b, c, d);
        } else {
            scanf("%d %d %d %d", &b, &c, &d, &e);
            printf("%lld\n", sum(d, e) - sum(b - 1, e) - sum(d, c - 1) + sum(b - 1, c - 1));
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/liuzz-20180701/p/11489755.html