蒟蒻爆零求助qaq
查看原帖
蒟蒻爆零求助qaq
287297
Lyr_ids楼主2021/11/1 17:48

rt

#include<bits/stdc++.h>
#define ll long long
//#include<conio.h>
//#include<windows.h>
using namespace std;
const int MAXN=1000005;
ll arr[MAXN],tree[MAXN<<2],tag[MAXN<<2];
inline ll ls(ll x)
{
	return x<<1;
}
inline ll rs(ll x)
{
	return x<<1|1;
}
inline void push_back(ll p)
{
	tree[p]=tree[ls(p)]+tree[rs(p)];
}
void build(ll l,ll r,ll p)
{
	if(l==r)
	{
		tree[p]=arr[l];
		return;	
	}
	ll mid=l+r>>1;
	build(l,mid,ls(p));
	build(mid+1,r,rs(p));
	push_back(p);
}
inline void push__down(ll l,ll r,ll p,ll k)
{
	tag[p]+=k;
	tree[p]+=(r-l+1)*k;
}
inline void push_down(ll l,ll r,ll p)
{
	ll mid=l+r>>1;
	push__down(l,mid,ls(p),tag[p]);
	push__down(mid+1,r,rs(p),tag[p]);
	tag[p]=0;
}
inline void res(ll l_,ll r_,ll l,ll r,ll p,ll k)
{
	if(l_<=l&&r<=r_)
	{
		tree[p]+=(r-l+1)*k;
		tag[p]+=k;
		return;
	}
	ll mid=l+r>>1;
	push_down(l,r,p);
	if(l_<=mid)
		res(l_,r_,l,mid,ls(p),k);
	if(r_>mid)
		res(l_,r_,mid+1,r,rs(p),k);
	push_back(p);
}
ll que(ll l_,ll r_,ll l,ll r,ll p)
{
	if(l_<=l&&r<=r_)
		return tree[p];
	ll ans=0;
	ll mid=l+r>>1;
	push_down(l,r,p);
	if(l_<=mid)
		ans+=que(l_,r_,l,mid,ls(p));
	if(r_>mid)
		ans+=que(l_,r_,mid+1,r,rs(p));
	return ans;
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%lld",&arr[i]);
	build(1,1,n);
	while(m--)
	{
		int tmp;
		scanf("%d",&tmp);
		switch(tmp)
		{
			case 1:
				ll a,b,c;
				scanf("%lld%lld%lld",&a,&b,&c);
				res(a,b,1,n,1,c);
				break;
			default:
				ll a,b;
				scanf("%lld%lld",&a,&b);
				printf("%lld\n",que(a,b,1,n,1));
				break;
		}
	}
	return 0;
}

2021/11/1 17:48
加载中...