ProgramAlgTrain/20240919/CSP常考算法模板/树状数组/树状数组( 单点修改 区间查询).cpp
2024-09-19 10:22:41 +08:00

47 lines
615 B
C++

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 1e6 + 10;
ll c[MAXN];
int n, q, k, a, b;
int lowbit(int x) {
return x&(-x);
}
void update(int x, int v) {
for(int i=x;i<=n;i+=lowbit(i))
c[i]+=v;
}
ll getSum(int x) {
ll ans=0;
for(int i=x; i>0; i-=lowbit(i))
ans+=c[i];
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int v;
cin>>n>>q;
for(int i=1; i<=n; i++) {
cin>>v;
update(i, v);
}
for(int i=1; i<=q; i++) {
cin>>k>>a>>b;
if(k==1) {
update(a, b);
} else {
cout<<getSum(b)-getSum(a-1)<<endl;
}
}
return 0;
}