https://www.luogu.com.cn/problem/P3374
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define lowbit(x) x&(-x)
using ll = long long;
using pii = pair<int, int>;
const double PI = acos(-1);
const int N = 5e5 + 10;
const int mod = 1e9 + 7;
ll a[N];
int n,m;
void add_dandian(int x,int k){
for(int i=x;i<=n;i+=lowbit(i)){
a[i]+=k;
}
}
ll query(int l,int r){//查询[l,r]的和
ll ans=0;
for(int i=r;i;i-=lowbit(i)){
ans+=a[i];
}
for(int i=l-1;i;i-=lowbit(i)){
ans-=a[i];
}
return ans;
}
void solve() {
cin>>n>>m;
for(int i=1;i<=n;i++){
int x;cin>>x;
add_dandian(i,x);
}
while(m--){
int t,x,y;cin>>t>>x>>y;
if(t==1){
add_dandian(x,y);
// for(int i=1;i<=n;i++)
// cout<<a[i]<<" ";
// cout<<endl;
}
else{
cout<<query(x,y)<<endl;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int T = 1;
//cin>>T;
while (T--) {
solve();
}
return 0;
}
标签:const,树状,int,ll,add,dandian,数组,using,模板
From: https://www.cnblogs.com/laileou/p/18667833