全WA求调QAQ
查看原帖
全WA求调QAQ
794148
cyx20091026楼主2024/12/22 10:48
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
int a[100005];
int w[400005],lzy[400005];
int n,m;
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);
}
void maketag(int u,int len,int x){
	lzy[u]+=x;
	w[u]+=x*len;
}
void putdown(int L,int R,int u){
	int mid=(L+R)/2;
	maketag(u*2,mid-L+1,lzy[u]);
	maketag(u*2+1,R-mid,lzy[u]);
	lzy[u]=0;
}
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 (r<L)||(R<l);
}
int ask(int L,int R,int l,int r,int u){
	if(in(L,R,l,r)){
		return w[u];
	}
	else if(!nocr(L,R,l,r)){
		putdown(L,R,u);
		int mid=(L+R)/2;
		return ask(L,mid,l,r,u*2)+ask(mid+1,R,l,r,u*2+1);
	}
	else return 0;
}
void update(int L,int R,int l,int r,int u,int x){
	if(in(L,R,l,r)){
		maketag(u,L-R+1,x); 
	}
	else if(!nocr(L,R,l,r)){
		int mid=(L+R)/2;
		putdown(L,R,u);
		update(L,mid,l,r,u*2,x);
		update(mid+1,R,l,r,u*2+1,x);
		pushup(u);
	}
}
signed main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	build(1,1,n);
	int work,x,y,k;
	for(int i=1;i<=m;i++){
		cin>>work;
		if(work==1){
			cin>>x>>y>>k;
			update(1,n,x,y,1,k);
		}
		else{
			cin>>x>>y;
			cout<<ask(1,n,x,y,1)<<endl;
		}
	}
	return 0;
} 
2024/12/22 10:48
加载中...