95分
查看原帖
95分
807584
mengqifeng楼主2025/6/28 17:20
#include <iostream>
using namespace std;
#define ll long long 
struct node
{
	ll sum;
	ll tag;
};
const int maxn=1e5+5;
node tree[maxn*4];
int n,q;
ll a[maxn];
void pushup(int x)
{
	tree[x].sum=tree[x*2+1].sum+tree[x*2].sum;
}
void build(int l,int r,int u)
{
	if(l==r)
	{
		tree[u].sum=a[l];
		return ;
	}
	int mid=(l+r)>>1;
	build(l,mid,2*u);
	build(mid+1,r,2*u+1);
	pushup(u);
}
void make_tag(int u,int len,int x)
{
	tree[u].sum+=len*x;
	tree[u].tag+=x;
}
void pushdown(int u,int l,int r)
{
	int mid=(l+r)>>1;
	make_tag(u*2,mid-l+1,tree[u].tag);
	make_tag(u*2+1,r-mid,tree[u].tag);
	tree[u].tag=0;
}
bool inrange(int l,int r,int L,int R)
{
	return (L<=l)&&(R>=r);
}
bool outrange(int l,int r,int L,int R)
{
	return (l>R)||(r<L);
}
void update(int u,int l,int r,const int L,const int R,ll x)
{
	if(inrange(l,r,L,R))
		make_tag(u,r-l+1,x);
	else if(!outrange(l,r,L,R))
	{
		int mid=(l+r)>>1;
		pushdown(u,l,r);
		update(2*u,l,mid,L,R,x);
		update(2*u+1,mid+1,r,L,R,x);
		pushup(u);
	}
	else return ;
}
ll query(int u,int l,int r,const int L,const int R)
{
	if(inrange(l,r,L,R))
		return tree[u].sum;
	else if(!outrange(l,r,L,R))
	{
		pushdown(u,l,r);
		int mid=(l+r)>>1;
		return query(2*u,l,mid,L,R)+query(2*u+1,mid+1,r,L,R);
	}
	else return 0;
}
int op,x,y;
ll z;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n>>q;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	build(1,n,1);
	for(int i=1;i<=q;i++)
	{
		cin>>op;
		if(op==1)
		{
			cin>>x>>y>>z;
			update(1,1,n,x,y,z);
		}
		else 
		{
			cin>>x>>y;
			cout<<query(1,1,n,x,y)<<endl;
		}
	}
	return 0;
}

2025/6/28 17:20
加载中...