【模板】树状数组
具体原理:
传送门:C++树状数组详解,DALAO解释的非常详细
代码
#include <bits/stdc++.h>
#define MAX 2000005
#define ll long long
using namespace std;
int n, m;
int a[MAX];
int lowbit(int x){
return x & -x;
}
void update(int x, int k) { //单点修改
for (int i = x; i <= n; i += lowbit(i)) {
a[i] += k;
}
}
int __getSum(int x) { //前缀和
int res = 0;
for (int i = x; i; i -= lowbit(i)) {
res += a[i];
}
return res;
}
int getSum(int x, int y) { //区间和
return __getSum(y) - __getSum(x-1);
}
int main() {
int x;
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
scanf("%d", &x);
update(i,x);
}
for (int i = 1; i <= m; ++i) {
int y, z;
scanf("%d %d %d", &x, &y, &z);
if (x == 1) {
update(y, z);
} else {
printf("%d\n", getSum(y, z));
}
}
return 0;
}