_加零_ @ 2023-11-16 20:15:04
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=5e6;
int n,m,tree[maxn];
int lowbit(int k){
return k & -k;
}
void add(int x,int k){
while(x<=n){
tree[x]+=k;
x+=lowbit(x);
}
}
int sum(int x){
int ans=0;
while(x!=0){
ans+=tree[x];
x-=lowbit(x);
}
return ans;
}
signed main(){
cin >> n >> m;
for(int i=1;i<=n;i++){
int a;
cin >> a;
add(i,a);
}
for(int i=1;i<=m;i++){
int op;
cin >> op;
if(op==1){
int x,y,z;
cin >> x >> y >> z;
add(x,z);
add(y+1,-z);
}else if(op==2){
int x,y;
cin >> x >> y;
cout << sum(y)-sum(x-1) << endl;
}
}
return 0;
}
by fangzichang @ 2023-11-16 20:20:51
?你是怎么认为一次单点加一次单点减就等于一次区间加了?
写这个,请
by P1nk_Patr1ck @ 2023-11-17 10:06:55
树状数组的区间修改不是这样打的吧