求调
查看原帖
求调
1349423
jms23002楼主2025/1/19 13:58
#include<bits/stdc++.h>
using namespace std;
const long long N=1e5+5;
long long n,m,a[N],q;
struct node{
	int l;
	int r;
	long long sum;
	long long lazy;
	long long c;
}tr[4*N];
void pushup(int u){
	tr[u].sum=tr[u*2].sum+tr[u*2+1].sum;
}
void pushdown(int u){
	tr[u*2].c*=tr[u].c;
	tr[u*2].lazy=tr[u*2].lazy*tr[u].c+tr[u].lazy; 
	tr[u*2].sum=(tr[u].c*tr[u*2].sum+(tr[u*2].r-tr[u*2].l+1)*tr[u].lazy);
	
	tr[u*2+1].c*=tr[u].c;
	tr[u*2+1].lazy=tr[u*2+1].lazy*tr[u].c+tr[u].lazy; 
	tr[u*2+1].sum=(tr[u].c*tr[u*2+1].sum+(tr[u*2+1].r-tr[u*2+1].l+1)*tr[u].lazy);
		 
	tr[u].lazy=0;
	tr[u].c=1;
}
void build(int u,int l,int r){
	tr[u].l=l;
	tr[u].r=r;
	if(tr[u].l==tr[u].r){
	    tr[u].sum=a[l];
	    tr[u].lazy=0;
	    tr[u].c=1;
	    return;
	}
	long long mid=(tr[u].l+tr[u].r)/2;
	build(u*2,l,mid);build(u*2+1,mid+1,r);
	pushup(u);
}
void mofity(int u,int l,int r,long long v){
	if(l<=tr[u].l&&tr[u].r<=r){
		tr[u].lazy+=v;
		tr[u].sum+=(tr[u].r-tr[u].l+1)*v;
		return;
	} 
	pushdown(u);
	long long mid=(tr[u].r+tr[u].l)/2;
	if(l<=mid){
		mofity(u*2,l,r,v);
	} 
	if(mid<r){
		mofity(u*2+1,l,r,v);
	} 
	pushup(u);
}
void czc(int u,int l,int r,int v){
	if(l<=tr[u].l&&tr[u].r<=r){
		tr[u].c*=v;
		tr[u].lazy=tr[u].lazy*v;
		return;
	}
	pushdown(u);
	long long mid=(tr[u].r+tr[u].l)/2;
	if(l<=mid){
		czc(u*2,l,r,v);
	} 
	if(mid<r){
		czc(u*2+1,l,r,v);
	} 
	pushup(u);
}
long long query(int u,int l,int r){
	if(l<=tr[u].l&&tr[u].r<=r){
		return tr[u].sum;
	}
	pushdown(u);
	long long mid=(tr[u].r+tr[u].l)/2;
	long long ans=0;  
	if(l<=mid) ans=query(u*2,l,r);
	if(mid<r) ans+=query(u*2+1,l,r);
	return ans;
}
int main(){ 
	cin>>n>>q>>m;
	for(long long i=1;i<=n;i++){
		scanf("%lld",&a[i]);
	}
	build(1,1,n);
	while(q--){
		long long op,x,y,k;
		scanf("%lld",&op);
		if(op==1){
			scanf("%lld%lld%lld",&x,&y,&k);
			czc(1,x,y,k);
		}
		else if(op==3){
			scanf("%lld%lld",&x,&y);
			long long ans;
			ans=query(1,x,y);
			printf("%lld\n",ans);
		}
		else{
			scanf("%lld%lld%lld",&x,&y,&k);
			mofity(1,x,y,k);
		}
	}
	return 0;
}
2025/1/19 13:58
加载中...