LAZY_TAG

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn=100000+10;
typedef long long ll;
ll a[maxn];
ll tree[maxn<<2];
ll lazy[maxn<<2];
void build(ll h,ll l,ll r){
	if(l==r){
		tree[h]=a[l];
		return ;
	}
	ll mid=(l+r)>>1;
	build(h<<1,l,mid);
	build(h<<1|1,mid+1,r);
	tree[h]=tree[h<<1]+tree[h<<1|1];
}
void updata(ll h,ll l,ll r,ll q,ll w,ll e){
	if(l==q&&r==w){	
		lazy[h]+=e;
		return ;
	}
	ll mid=(l+r)>>1;
	tree[h]+=(w-q+1)*e;
	if(q>mid)updata(h<<1|1,mid+1,r,q,w,e);
	else if(w<=mid)updata(h<<1,l,mid,q,w,e);
	else{
		updata(h<<1,l,mid,q,mid,e);
		updata(h<<1|1,mid+1,r,mid+1,w,e);
	}
}
ll query(ll h,ll l,ll r,ll q,ll w){
	if(l==q&&r==w){
		return tree[h]+lazy[h]*(w-q+1);
	}
	ll mid=(l+r)>>1;
	if(lazy[h]){
		tree[h]+=lazy[h]*(r-l+1);
		updata(h<<1,l,mid,l,mid,lazy[h]);
		updata(h<<1|1,mid+1,r,mid+1,r,lazy[h]);
		lazy[h]=0;
	}
	if(q>mid)return query(h<<1|1,mid+1,r,q,w);
	else if(w<=mid)return query(h<<1,l,mid,q,w);
	else return query(h<<1,l,mid,q,mid)+query(h<<1|1,mid+1,r,mid+1,w); 
}
int main(){
	ll i,j,k,m,n;
	scanf("%lld%lld",&n,&m);
	for(i = 1;i <= n;i++)scanf("%lld",&a[i]);
	build(1,1,n);
	while(m--){
		ll q,w,e;
		ll t;
		scanf("%lld",&t);
		if(t==1){
			scanf("%lld%lld%lld",&q,&w,&e);
			updata(1,1,n,q,w,e);
		}
		if(t==2){
			scanf("%lld%lld",&q,&w);
			printf("%lld\n",query(1,1,n,q,w));
		}	
	}
	return 0;
}

posted @ 2017-01-13 15:27  Drinkwater_cnyali  阅读(203)  评论(0编辑  收藏  举报