P3374 【模板】树状数组 1 单点修改 + 区间查询
作者:
多米尼克領主的致意
,
2024-05-13 12:57:06
,
所有人可见
,
阅读 2
#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) x&(-x)
// typedef long long ll;
const int N = 5e5 + 10;
int n, m;
int a[N], tree[N];
void add(int x, int d){
while(x < N){
tree[x] += d;
x += lowbit(x);
}
}
int sum(int x){
int ans = 0;
while(x > 0){
ans += tree[x];
x -= lowbit(x);
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 1;i <= n;i++){
cin >> a[i];
add(i, a[i]);
}
while(m--){
int op, x, k;
cin >> op;
if(op == 1){
cin >> x >> k;
add(x, k);
}
if(op == 2){
int l, r;
cin >> l >> r;
cout << sum(r) - sum(l - 1) << endl;
}
}
return 0;
}