求调
查看原帖
求调
772828
lliou楼主2024/8/9 07:53
#include<bits/stdc++.h>
#define N 100050
#define int long long
using namespace std;
int st[N*4],add[N*4],a[N],n,m,q,x,y,val;
void pushup(int p){
	st[p]=st[p*2]+st[p*2+1];
}
void pushdown(int p,int l,int r){
	if(add[p]){
		add[p*2]+=add[p];
		add[p*2+1]+=add[p];
		st[p*2]+=add[p]*l;
		st[p*2+1]+=add[p]*r;
		add[p]=0;
	}
}
void build(int p,int l,int r){
	if(l==r){
		st[p]=a[l];
		return;
	}
	int mid=l+r>>1;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
	pushup(p);
}
void update(int p,int l,int r,int x,int y,int val){
	if(x<=l&&y>=r){
		st[p]+=val*(r-l+1);
		add[p]+=val;
		return;
	}
	int mid=l+r>>1;
	pushdown(p,mid-l+1,r-mid);
	if(r<=mid){
		update(p*2,l,mid,x,y,val);
	}
	if(l>mid){
		update(p*2+1,mid+1,r,x,y,val);
	}
	pushup(p);
}
int query(int p,int l,int r,int x,int y){
	if(x<=l&&y>=r){
		return st[p];
	}
	int ans=0;
	int mid=l+r>>1;
	pushdown(p,mid-l+1,r-mid);
	if(x<=mid){
		ans+=query(p*2,l,mid,x,y);
	}
	if(y>mid){
		ans+=query(p*2+1,mid+1,r,x,y);
	}
	return ans;
}
signed main(){
	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
	}
	build(1,1,n);
	for(int i=1;i<=m;i++){
		scanf("%lld",&q);
		if(q==1){
			scanf("%lld%lld%lld",&x,&y,&val);
			update(1,1,n,x,y,val);
		}
		if(q==2){
			scanf("%lld%lld",&x,&y);
			printf("%lld\n",query(1,1,n,x,y));
		}
	}
}

qwq找不出错的点

2024/8/9 07:53
加载中...