P3372 线段树 HELP!!!
  • 板块学术版
  • 楼主hg20230121
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/11/8 18:57
  • 上次更新2024/11/8 21:08:12
查看原帖
P3372 线段树 HELP!!!
1189120
hg20230121楼主2024/11/8 18:57
#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; 
} 
2024/11/8 18:57
加载中...