30分,求调,用的结构体tag
查看原帖
30分,求调,用的结构体tag
538376
yuyudong楼主2024/7/18 13:01
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[1000005];
ll sum[4000050];
ll n,m,mod;
struct Tag{
	ll A,B;
}tag[4000005];
Tag operator+(const Tag &x,const Tag &y)
{
	return (Tag){x.A*y.A,x.B*y.A+y.B};
}
void scan()
{
    cin>>n>>m>>mod;
    for(ll i=1;i<=n;i++) scanf("%lld",&a[i]);
}
void build(ll p,ll l,ll r)
{
	tag[p]={1,0};
	if(l==r)
	{
		sum[p]=a[l];
		return;
	}
	ll mid=(l+r)/2;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
	sum[p]=sum[p*2]%mod+sum[p*2+1]%mod;
}
void maketag(ll p,Tag z,ll l,ll r)
{
	tag[p]=tag[p]+z;
	sum[p]=sum[p]*z.A%mod+z.B*(r-l+1)%mod;
	sum[p]%=mod;
}
void pushdown(ll p,ll l,ll r)
{
	if(tag[p].A!=1||tag[p].B!=0)
	{
		ll mid=(l+r)>>1;
		maketag(p*2,tag[p],l,mid);
		maketag(p*2+1,tag[p],mid+1,r);
		tag[p]=(Tag){1,0};
	}
}
void pushup(ll p)
{
	sum[p]=sum[p*2]+sum[p*2+1]%mod;
	sum[p]%=mod; 
 } 
void Update(ll p,ll l,ll r,ll x,ll y,Tag z)
{
	if(x<=l&&r<=y) 
	{
		tag[p]=tag[p]+z;
		sum[p]=(sum[p]*z.A%mod+z.B*(r-l+1)%mod)%mod;
		return; 
	}
	ll mid=(l+r)/2;
	pushdown(p,l,r);
	if(x<=mid) Update(p*2,l,mid,x,y,z);
	if(mid<y) Update(p*2+1,mid+1,r,x,y,z);
	pushup(p);
}
ll Sum(ll p,ll x,ll y,ll l,ll r)
{
	if(x<=l&&r<=y) return sum[p];
	int mid=(l+r)/2;
	pushdown(p,l,r);
	ll res=0;
	if(x<=mid) res=res+Sum(p*2,x,y,l,mid)%mod;
	if(mid<y) res=res+Sum(p*2+1,x,y,mid+1,r)%mod;
	return res%mod; 
}
int main()
{
	scan();
	build(1,1,n);
	for(int i=1;i<=m;i++)
	{
		ll tmp;
		scanf("%lld",&tmp);
		if(tmp==1)
		{
			ll x,y,k;
			scanf("%lld%lld%lld",&x,&y,&k);
			Tag t={k,0};
			Update(1,1,n,x,y,t);
		}
		else if(tmp==2)
		{
			ll x,y,k;
			scanf("%lld%lld%lld",&x,&y,&k);
			Tag t={1,k};
			Update(1,1,n,x,y,t);
		}
		else
		{
			ll x,y;
			scanf("%lld%lld",&x,&y);;
			printf("%lld\n",Sum(1,x,y,1,n)%mod);
		}
	}
	return 0;
 } 

2024/7/18 13:01
加载中...