求助
查看原帖
求助
275090
Albet楼主2020/11/22 11:30
#include<iostream>
using namespace std;
const int N=105000;
const int M=280000;
long long tree[M];
int tl[M],tr[M],tmid[M];
long long lazy[M];
int n,m;
long long a[N];
void replace(int x){
	tree[x]=tree[x*2]+tree[x*2+1];
}
void maketree(int x,int l,int r){
	tl[x]=l;
	tr[x]=r;
	lazy[x]=0;
	int mid=(l+r)/2;
	tmid[x]=mid;
	if(l==r){
		tree[x]=a[l];
	}else{
		maketree(x*2,l,mid);
		maketree(x*2+1,mid+1,r);
		replace(x);
	}
}
void pushdown(int x){
	if(tl[x]<tr[x]){
		tree[x*2]+=lazy[x]*(tr[x*2]-tl[x*2]+1);
		lazy[x*2]+=lazy[x];
		tree[x*2+1]+=lazy[x]*(tr[x*2+1]-tl[x*2+1]+1);
		lazy[x*2+1]+=lazy[x];
		lazy[x]=0;	
	}
}
void add(int x,int l,int r,long long c){
	if(tl[x]==l && tr[x]==r){
		tree[x]+=c*(r-l+1);
		lazy[x]+=c;
		return;
	}
	pushdown(x);
	if(r<=tmid[x]){
		add(x*2,l,r,c);
	}else if(l>tmid[x]){
		add(x*2+1,l,r,c);
	}else{
		add(x*2,l,tmid[x],c);
		add(x*2+1,tmid[x]+1,r,c);
	}
	replace(x);
}
long long find(int x,int l,int r){
	if(tl[x]==l &&tr[x]==r){
		return tree[x];
	}
	pushdown(x);
	if(r<=tmid[x]){
		return find(x*2,l,r);
	}else if(l>tmid[x]){
		return (x*2+1,l,r);
	}else{
		long long res=find(x*2,l,tmid[x])+find(x*2+1,tmid[x]+1,r);
		return res;
	}
}
int main(){
	cin>>n>>m;
	int i,j;
	for(i=1;i<=n;i++){
		cin>>a[i];
	}
	maketree(1,1,n);
	for(int i=1;i<=m;i++){
		int opt;
		cin>>opt;
		if(opt==1){
			int a,b;
			long long c;
			cin>>a>>b>>c;
			add(1,a,b,c);
		}else{
			int a,b;
			cin>>a>>b;
			long long res=find(1,a,b);
			cout<<res<<endl;
		}
	}
	return 0;
}
2020/11/22 11:30
加载中...