#include<bits/stdc++.h>
#define lowbit(x) x&(-x)
using namespace std;
const int N = 5e5 + 5;
int n, m, C[N];
void add(int pos, int num) {
while (pos <= n) {
C[pos] += num;
pos += lowbit(pos);
}
}
int sum(int pos) {
int sum = 0;
while (pos != 0) {
sum += C[pos];
pos -= lowbit(pos);
}
return sum;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int num; cin >> num;
add(i, num);
}
while (m--) {
int a, b, c; cin >> a >> b >> c;
if (a == 1) {
add(b, c);
}
else {
cout << sum(c) - sum(b-1) << endl;
}
}
return 0;
}
【例题记录】树状数组模板题1
最新推荐文章于 2024-08-18 21:07:18 发布