求调
查看原帖
求调
794148
cyx20091026楼主2025/2/8 15:44
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m;
int a[100005];
int w[100005];
int lzy[100005];
void pushup(int u){
	w[u]=w[u*2]+w[u*2+1];
}
void build(int u,int L,int R){
	if(L==R){
		w[u]=a[L]; 
		return;
	}
	int mid=(L+R)/2;
	build(u*2,L,mid);build(u*2+1,mid+1,R);
	pushup(u);
}
bool in(int L,int R,int l,int r){
	return (L>=l)&&(R<=r); 
}
bool nocr(int L,int R,int l,int r){
	return (L>r)||(R<l);
}
void maketag(int u,int len,int x){
	w[u]+=len*x;
	lzy[u]+=x;
}
void putdown(int u,int L,int R){
	int mid=(L+R)/2;
	maketag(u*2,mid-L+1,lzy[u]);
	maketag(u*2+1,R-mid,lzy[u]);
	lzy[u]=0;
}
int ask(int u,int L,int R,int l,int r){
	if(in(L,R,l,r)){
		return w[u];
	}
	else if(!nocr(L,R,l,r)){
		int mid=(L+R)/2;
		putdown(u,L,R);
		return ask(u*2,L,mid,l,r)+ask(u*2+1,mid+1,R,l,r);
	}
	else return 0;
}
void update(int u,int L,int R,int r,int l,int x){
	if(in(L,R,l,r)){
		maketag(u,R-L+1,x);
	}
	else if(!nocr(L,R,l,r)){
		int mid=(L+R)/2;
		putdown(u,L,R);
		update(u*2,L,mid,l,r,x);
		update(u*2+1,mid+1,R,l,r,x);
		pushup(u);
	}
}
signed main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	build(1,1,n);
	while(m--){
		int work,x,y,k;
		cin>>work;
		if(work==1){
			cin>>x>>y>>k;
			update(1,1,n,x,y,k);
		}
		else if(work==2){
			cin>>x>>y;
			cout<<ask(1,1,n,x,y)<<endl;
		}
	}
	return 0;
}
2025/2/8 15:44
加载中...