线段树1,实在找不出问题,求解
  • 板块学术版
  • 楼主hg20230121
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/5/8 14:50
  • 上次更新2024/5/8 18:50:59
查看原帖
线段树1,实在找不出问题,求解
1189120
hg20230121楼主2024/5/8 14:50
#include<bits/stdc++.h>
#define pl h<<1
#define pr h<<1|1
#define int long long
using namespace std;
const int N=500001;
int e,k,x,y,n,m,s[N];
struct jntm{
	int l,r,sum,add;
}df[N*4];
void pushup(int h){
	df[h].sum=df[pl].sum+df[pr].sum;
	return;
}
void bulid(int h,int l,int r){
	df[h]={l,r,s[l]};
	if(l==r) return;
	int cnt=l+r>>1;
	bulid(pl,l,cnt);
	bulid(pr,cnt+1,r);
	pushup(h);
}
void pushdown(int h){
	if(df[h].add){
		df[pl].sum+=df[h].add*(df[pl].r-df[pl].l+1);
		df[pr].sum+=df[h].add*(df[pr].r-df[pl].l+1);
		df[pl].add+=df[h].add;
		df[pr].add+=df[h].add;
		df[h].add=0;
	}
}
void updata(int h,int x,int y,int k){
	if(df[h].l>=x&&df[h].r<=y){
		df[h].sum+=(df[h].r-df[h].l+1)*k;
		df[h].add+=k;
		return;
	}
	pushdown(h);
	int cnt=df[h].l+df[h].r>>1;
	if(x<=cnt) updata(pl,x,y,k);
	if(y>cnt) updata(pr,x,y,k);
	pushup(h);
}
int query(int h,int x,int y){
	if(df[h].l>=x&&df[h].r<=y) return df[h].sum;
	pushdown(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;
}
signed main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>s[i];
	bulid(1,1,n);
	while(m--){
		cin>>e>>x>>y;
		if(e==1){
			cin>>k;
			updata(1,x,y,k);
		}
		if(e==2){
			cout<<query(1,x,y)<<endl;
		}
	}
	return 0;
}
2024/5/8 14:50
加载中...