题目链接: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;
}