社区讨论
P3372 线段树 HELP!!!
学术版参与者 3已保存回复 2
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 2 条
- 当前快照
- 1 份
- 快照标识符
- @m38midqi
- 此快照首次捕获于
- 2024/11/08 18:58 去年
- 此快照最后确认于
- 2025/11/04 15:07 4 个月前
CPP
#include<bits/stdc++.h>
#define pl h<<1
#define pr h<<1|1
using namespace std;
const int N=5e5+1;
struct LM{
int l,r,sum,add;
}df[N*4];
int s[N],id,k,x,y,n,m;
void pushup(int h){
df[h].sum=df[pl].sum+df[pr].sum;
return;
}
void build(int h,int l,int r){
df[h]={l,r,s[l]};
if(l==r) return;
int cnt=l+r>>1;
build(pl,l,cnt);
build(pr,cnt+1,r);
pushup(h);
}
void updata(int h,int x,int k){
if(df[h].l==x&&df[h].r==x){
df[h].sum+=k;
return;
}
int cnt=df[h].l+df[h].r>>1;
if(x<=m) updata(pl,x,k);
if(x>m) updata(pr,x,k);
pushup(h);
}
int query(int h,int x,int y){
if(df[h].l>=x&&df[h].r<=y) return df[h].sum;
pushup(h);
int cnt=df[h].l+df[h].r>>1;
int sum=0;
if(x<=cnt) sum+=query(pl,x,y);
if(y>cnt) sum+=query(pr,x,y);
return sum;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>s[i];
build(1,1,n);
while(m--){
cin>>id;
if(id==1){
cin>>x>>k;
updata(1,x,k);
}
if(id==2){
cin>>x>>y;
cout<<query(1,x,y)<<endl;
}
}
return 0;
}
回复
共 2 条回复,欢迎继续交流。
正在加载回复...