全WA
查看原帖
全WA
638274
_YQY楼主2023/1/30 15:49
#include<bits/stdc++.h>
using namespace std;
#define ll long long 
const int maxn=1e5+2;
int w[maxn*4],a[maxn];
void pushup(const int u){
	w[u]=w[u*2]+w[u*2+1];
}
void build(const int u,int l,int r){//建树 
	if(l==r){
		w[u]=a[l];
		return ;
	}
	int m=(l+r)/2;
	build(u*2,l,m);
	build(u*2+1,m+1,r);
	pushup(u);
}
bool inrange(int L,int R,int l,int r){
	//判断[L,R]是否被[l,r]包含
	return (l<=L)&&(R<=r); 
}
bool outrange(int L,int R,int l,int r){
	//判断[L,R]是否和[l,r]完全无交
	return (L>r)||(R<l);
}
ll lzy[maxn*4];
void maketag(int u,int len,ll x){
	lzy[u]+=x;
	w[u]+=len*x;
}
void pushdown(int u,int l,int r){
	int m=(l+r)/2;
	maketag(u*2,m-l+1,lzy[u]);
	maketag(u*2+1,r-m,lzy[u]);
	lzy[u]=0;
}
ll query(int u,int L,int R,int l,int r){
	if(inrange(L,R,l,r)){
		return w[u];
	}
	else if(!outrange(L,R,l,r)){
		int m=(L+R)/2;
		pushdown(u,L,R);
		return query(u*2,L,m,l,r)+query(u*2+1,m+1,R,l,r);
	}
	else return 0;
}
void update(int u,int L,int R,int l,int r,ll x){
	if(inrange(L,R,l,r)){
		maketag(u,R-L+1,x);
	}
	else if(!outrange(L,R,l,r)){
		int m=(L+R)/2;
		pushdown(u,L,R);
		update(u*2,L,m,l,r,x);
		update(u*2+1,m+1,R,l,r,x);
		pushup(u);	
	}
} 
int main(){
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	build(1,1,n);
	for(int i=1;i<=m;i++){
		int op,x,y;
		ll k;
		if(op==1){
			cin>>x>>y>>k;
			update(1,1,n,x,y,k);
		}
		else {
			cin>>x>>y;
			cout<<query(1,1,n,x,y)<<endl; 
		}
	}
	return 0;
}


2023/1/30 15:49
加载中...