线段树 样例过了 全WA 求助
查看原帖
线段树 样例过了 全WA 求助
476767
羊叫兽同学楼主2022/7/6 17:22

rt

#include<iostream>
#include<cstdio>
#define int long long
#define aaa12321 yyds
using namespace std;
const int aaa=1145140;
int n,m,s,l,i,j,f[aaa],mod,kind,qwq;
struct tree
{
	int l,r,sum,lazy1,lazy2;
}a[aaa];
void build(int l,int r,int p)
{
	a[p].l=l;
	a[p].r=r;
	a[p].lazy2=1;
	if(l==r)
	{
		a[p].sum=f[l];
		return ;
	}
	int mid=(l+r)/2;
	build(l,mid,p*2);
	build(mid+1,r,p*2+1);
	a[p].sum=(a[p*2].sum+a[p*2+1].sum)%mod;
	return ;
}
void check(int p)
{
	int d;
	if(a[p].lazy1!=0)
	{
		d=a[p].lazy1;
		a[p].lazy1=0;
		a[p*2].sum=(a[p*2].sum+d*(a[p*2].r-a[p*2].l+1)%mod)%mod;
		a[p*2+1].sum=(a[p*2+1].sum+d*(a[p*2+1].r-a[p*2+1].l+1)%mod)%mod;
		a[p*2].lazy1=(a[p*2].lazy1+d)%mod;
		a[p*2+1].lazy2=(a[p*2+1].lazy2+d)%mod;
	}
	if(a[p].lazy2!=1)
	{
		d=a[p].lazy2;
		a[p].lazy2=1;
		a[p*2].sum=a[p*2].sum*d%mod;
		a[p*2].lazy1=a[p*2].lazy1*d%mod;
		a[p*2].lazy2=a[p*2].lazy2*d%mod;
		a[p*2+1].sum=a[p*2+1].sum*d%mod;
		a[p*2+1].lazy1=a[p*2+1].lazy1*d%mod;
		a[p*2+1].lazy2=a[p*2+1].lazy2*d%mod; 
	}
	return ;
}
void change1(int l,int r,int p,int d)
{
	if(l<=a[p].l&&a[p].r<=r)
	{
		a[p].sum=a[p].sum*d%mod;
		a[p].lazy1=a[p].lazy1*d%mod;
		a[p].lazy2=a[p].lazy2*d%mod;
		return ;
	}
	check(p);
	int mid=(a[p].l+a[p].r)/2;
	if(l<=mid)
		change1(l,r,p*2,d);
	if(mid<r)
		change1(l,r,p*2+1,d);
	a[p].sum=(a[p*2].sum+a[p*2+1].sum)%mod;
	return ;
}
void change2(int l,int r,int p,int d)
{
	if(l<=a[p].l&&a[p].r<=r)
	{
		a[p].sum=(a[p].sum+d*(a[p].r-a[p].l+1)%mod)%mod;
		a[p].lazy1=(a[p].lazy1+d)%mod;
		return ;
	}
	check(p);
	int mid=(a[p].l+a[p].r)/2;
	if(l<=mid)
		change2(l,r,p*2,d);
	if(mid<r)
		change2(l,r,p*2+1,d);
	a[p].sum=(a[p*2].sum+a[p*2+1].sum)%mod;
	return ;
}
int ask(int l,int r,int p)
{
	if(l<=a[p].l&&a[p].r<=r)
	{
		return a[p].sum;
	}
	check(p);
	int mid=(a[p].l+a[p].r)/2,sum=0;
	if(l<=mid)
		sum=(sum+ask(l,r,p*2))%mod;
	if(mid<r)
		sum=(sum+ask(l,r,p*2+1))%mod;
	return sum;
}
signed main()
{
	scanf("%lld%lld%lld",&n,&m,&mod);
	for(i=1;i<=n;i++)
	{
		scanf("%lld",&f[i]);
		f[i]%=mod;
	}
	build(1,n,1);
	int x,y,k;
	for(;m>=1;m--)
	{
		scanf("%lld%lld%lld",&kind,&x,&y);
		if(kind==1)
		{
			scanf("%lld",&k);
			change1(x,y,1,k);
		}
		if(kind==2)
		{
			scanf("%lld",&k);
			change2(x,y,1,k);
		}
		if(kind==3)
			printf("%lld\n",ask(x,y,1));
	}
}
2022/7/6 17:22
加载中...