线段树求❤调(马蜂优良)
查看原帖
线段树求❤调(马蜂优良)
809765
Limitless_lmw楼主2024/10/22 17:23

复制板子的结果(代码能力下降的())

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll n,m;
ll a[100005];

struct tree{
	ll l,r;
	ll val;
	ll tag;
}t[500005];

void build(ll l,ll r, ll p){
	t[p].l=l,t[p].r=r,t[p].tag=0;
	if(l==r){
		t[p].tag=0;
		t[p].val=a[l];
		return ;
	}
	build(l,l+r>>1,p<<1);
	build((l+r>>1)+1,r,p<<1|1);
	t[p].val=t[p<<1].val+t[p<<1|1].val;
	return ;
}

inline void spread(ll p){
	t[p<<1].val+=(t[p].tag*(t[p<<1].r-t[p<<1].l+1));
	t[p<<1|1].val+=(t[p].tag*(t[p<<1|1].r-t[p<<1|1].l+1));
	t[p<<1].tag+=t[p].tag;
	t[p<<1|1].tag+=t[p].tag;
	t[p].tag=0;
	return ;
}

inline void update(ll l,ll r,ll p,ll sl,ll sr,ll k){
	if(l<=sl&&sr<=r){
		t[p].tag+=k;
		t[p].val+=((sr-sl+1)*k);
		return ;
	}
	spread(p);
	ll mid=l+r>>1;
	if(sl<=mid) update(l,mid,p<<1,sl,mid,k);
	if(sr>mid) update(mid+1,r,p<<1|1,mid+1,sr,k);
	t[p].val=t[p<<1].val+t[p<<1|1].val;
	return ;
}

inline ll query(ll sx,ll sy,ll l,ll r,ll p){
    if(sx<=l&&sy>=r) return t[p].val;
    ll res=0;
    ll mid=(l+r)>>1;
    spread(p);
    if(sx<=mid) res+=query(sx,sy,l,mid,p<<1);
    if(sy>mid) res+=query(sx,sy,mid+1,r,p<<1|1);
    return res;
}

signed main(){
	scanf("%lld %lld",&n,&m);
	for(ll i =1 ;i<=n; i++) scanf("%lld",a+i);
	build(1,n,1);
	while(m--){
		ll opt;
		scanf("%lld",&opt);
		if(opt==1){
			ll x,y,k;
			scanf("%lld %lld %lld",&x,&y,&k);
			update(1,n,1,x,y,k);
		}else if(opt==2){
			ll x,y;
			scanf("%lld %lld",&x,&y);
			cout<<query(1,n,1,x,y)<<'\n';
		}
	}
	return 0;
}
2024/10/22 17:23
加载中...