其他分享
首页 > 其他分享> > LibreOJ 130 树状数组1:单点修改,区间查询

LibreOJ 130 树状数组1:单点修改,区间查询

作者:互联网

题目链接:LibreOJ 130 树状数组1:单点修改,区间查询

题目大意:

题解:
树状数组模板,注意要开\(long\) \(long\)。

#include <iostream>
#include <cstring>
using namespace std;

long long bit[1000010];
int n, q, act, l, r;

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

void update(int x, int k) {
    while (x <= n) {
        bit[x] += (long long)k;
        x += lowbit(x);
    }
}

long long getSum(int x) {
    long long ans = 0;
    while (x > 0) {
        ans += bit[x];
        x -= lowbit(x);
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(false);
    while (cin >> n >> q) {
        memset(bit, 0, sizeof(bit));
        for (int i = 1, x; i <= n; ++i) {
            cin >> x;
            update(i, x);
        }
        while (q--) {
            cin >> act >> l >> r;
            if (act == 2) {
                cout << getSum(r) - getSum(l - 1) << endl;
            } else {
                update(l, r);
            }
        }
    }
    return 0;
}

标签:单点,LibreOJ,树状,int,long,while,130,act,bit
来源: https://www.cnblogs.com/IzumiSagiri/p/15860601.html