题目传送门
#include <bits/stdc++.h>
using namespace std;
const int N = 5 * 1e5 + 10;
int n, m;
int a[N];
// t[i]表示树状数组i结点覆盖的范围和
int t[N];
//返回非负整数x在二进制表示下最低位1及其后面的0构成的数值
int lowbit(int x) {
return x & -x;
}
//将序列中第x个数加上k
void add(int x, int k) {
for (int i = x; i <= n; i += lowbit(i)) t[i] += k;
}
//查询序列前x个数的和
int sum(int x) {
int sum = 0;
for (int i = x; i; i -= lowbit(i)) sum += t[i];
return sum;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i], add(i, a[i]); //初始化树状数组
while (m--) {
int op, x, y;
cin >> op >> x >> y;
if (op == 1)
add(x, y); //在x位置上加上y
else
//计算[x,y]之间的数字和
cout << sum(y) - sum(x - 1) << endl;
}
return 0;
}