0pts样例已过求调
查看原帖
0pts样例已过求调
1402248
niubile楼主2024/10/24 10:26
#include<bits/stdc++.h>
using namespace std;
#define int long long
int a[100005];
int d[400005];
int b[400005];
int c1[400005];
int n,q,m1;
void build(int s,int t,int p)
{
	if(s==t)
	{
		d[p]=a[s];
		return;
	}
	int m=(s+t)/2;
	build(s,m,2*p);
	build(m+1,t,2*p+1);
	d[p]=d[p*2]+d[p*2+1];
	return;
}
int ans(int l,int r,int s,int t,int p)
{
	if(l<=s&&t<=r)
	{
		return d[p];
	}
	int m=(s+t)/2;
	if(c1[p]>1)
	{
		d[p*2]*=c1[p]%m1;
		d[p*2+1]*=c1[p]%m1;
		c1[p*2+1]*=c1[p]%m1;
		c1[p*2]*=c1[p]%m1;
		b[p*2+1]*=c1[p];
		b[p*2]*=c1[p];
		c1[p]=1;
	}
	if(b[p])
	{
			d[p*2]+=b[p]*(m-s+1)%m1;
			d[p*2+1]+=b[p]*(t-m)%m1;
			b[p*2]+=b[p];
			b[p*2+1]+=b[p];
			b[p]=0;
	}
	int sum=0;
	if(l<=m)
	{
		sum=ans(l,r,s,m,p*2)%m1;
	}
	if(m<r)
	{
		sum+=ans(l,r,m+1,t,p*2+1)%m1;
	}
	return sum;
}
void plu(int l,int r,int c,int s,int t,int p)
{
	if(l<=s&&t<=r)
	{
		d[p]+=c*(t-s+1)%m1;
		b[p]+=c;
		return;
	}
	int m=(s+t)/2;
	if(b[p]&&s!=t)
	{
		d[p*2]+=b[p]*(m-s+1)%m1;
		d[p*2+1]+=b[p]*(t-m)%m1;
		b[p*2+1]+=b[p];
		b[p*2]+=b[p];
		b[p]=0;
	}
	if(l<=m)
	{
		plu(l,r,c,s,m,p*2);
	}
	if(m<r)
	{
		plu(l,r,c,m+1,t,p*2+1);
	}
	d[p]=d[p*2]+d[p*2+1];
	return;
}
void cheng(int l,int r,int c,int s,int t,int p)
{
	if(l<=s&&t<=r)
	{
		d[p]*=c%m1;
		c1[p]*=c;
		b[p]*=c;
		return;
	}
	if(c1[p]>1)
	{
		d[p*2]*=c1[p]%m1;
		d[p*2+1]*=c1[p]%m1;
		c1[p*2+1]*=c1[p];
		c1[p*2]*=c1[p];
		b[p*2+1]*=c1[p];
		b[p*2]*=c1[p];
		c1[p]=1;
	}
	int m=(s+t)/2;
	if(l<=m)
	{
		cheng(l,r,c,s,m,p*2);
	}
	if(m<r)
	{
		cheng(l,r,c,m+1,t,p*2+1);
	}
	d[p]=d[p*2]+d[p*2+1];
	return;
}
void work(int p)
{
	if(p==2)
	{
		int x,y,z;
		scanf("%lld%lld%lld",&x,&y,&z);
		plu(x,y,z,1,n,1);
	}
	if(p==1)
	{
		int x,y,z;
		scanf("%lld%lld%lld",&x,&y,&z);
		cheng(x,y,z,1,n,1);
	}
	if(p==3)
	{
		int x,y;
		scanf("%lld%lld",&x,&y);
		int k=ans(x,y,1,n,1)%m1;
		printf("%lld\n",k);
	}
	return;
}
signed main()
{
	for(int i=1;i<=400005;i++)
	{
		c1[i]=1;
	}
	scanf("%lld%lld%lld",&n,&q,&m1);
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
	}
	build(1,n,1);
	for(int i=1;i<=q;i++)
	{
		int p;
		scanf("%lld",&p);
		work(p);
	}
	return 0;
}
2024/10/24 10:26
加载中...