首页 > 其他分享 >Charlie_

Charlie_

时间:2023-11-07 21:48:40浏览次数:20  
标签:return add2 int Charlie sum mid add

#include<bits/stdc++.h>
#define int long long 
using namespace std;
int n,a[100001],m,P;
struct aa
{
	int l,r,sum,add,add2=1;
}t[400040];
void build(int p,int l,int r)
{
	t[p].l=l,t[p].r=r;
	if(l==r) 
	{
		t[p].sum=a[l];
		return ;
	}
	int mid=(l+r)>>1;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
	t[p].sum=t[p*2].sum+t[p*2+1].sum;
}
void spread(int p)
{
	if(t[p].add||t[p].add2!=1)
	{
		t[p*2].sum=t[p].add2*t[p*2].sum+t[p].add*(t[p*2].r-t[p*2].l+1);
		t[p*2+1].sum=t[p].add2*t[p*2+1].sum+t[p].add*(t[p*2+1].r-t[p*2+1].l+1);
		t[p*2].add2*=t[p].add2;
		t[p*2+1].add2*=t[p].add2;
		t[p*2].add+=t[p].add;
		t[p*2+1].add+=t[p].add;
		t[p].add=0;
		t[p].add2=1;
	}
}
int ask(int p,int l,int r)
{
	if(l<=t[p].l&&r>=t[p].r) return t[p].sum%P;
	spread(p);
	int mid=(t[p].l+t[p].r)>>1;
	int val=0;
	if(l<=mid) val=(val+ask(p*2,l,r))%P;
	if(r>mid) val=(val+ask(p*2+1,l,r))%P;
	return val%P;
}
void change1(int p,int l,int r,int d)
{
	if(l<=t[p].l&&r>=t[p].r)
	{
		t[p].sum+=d*(t[p].r-t[p].l+1);
		t[p].add+=d;
		t[p].sum%=P,t[p].add%=P;
		return ;
	}
	spread(p);
	int mid=(t[p].l+t[p].r)>>1;
	if(l<=mid) change1(p*2,l,r,d);
	if(r>mid) change1(p*2+1,l,r,d);
	t[p].sum=(t[p*2].sum+t[p*2+1].sum)%P;
}
void change2(int p,int l,int r,int d)
{
	if(l<=t[p].l&&r>=t[p].r)
	{
		t[p].sum*=d;
		t[p].add2*=d;
		t[p].sum%=P,t[p].add2%=P;
		return ;
	}
	spread(p);
	int mid=(t[p].l+t[p].r)>>1;
	if(l<=mid) change2(p*2,l,r,d);
	if(r>mid) change2(p*2+1,l,r,d);
	t[p].sum=(t[p*2].sum+t[p*2+1].sum)%P;
}
signed main()
{
	cin>>n>>P;
	for(int i=1;i<=n;i++) cin>>a[i];
	build(1,1,n);
	cin>>m;
	for(int i=1;i<=m;i++)
	{
		int s,x,y,z;
		cin>>s>>x>>y;
		if(s==1)
		{
			cin>>z;
			change2(1,x,y,z);
		}
		else if(s==2)
		{
			cin>>z;
			change1(1,x,y,z);
		}
		else cout<<ask(1,x,y)%P<<"\n";
	}
}

标签:return,add2,int,Charlie,sum,mid,add
From: https://www.cnblogs.com/minecraft666/p/17816091.html

相关文章