萌新初学线段树,所有操作完全没用求助
查看原帖
萌新初学线段树,所有操作完全没用求助
171487
cmll02楼主2021/3/5 18:30

RT,求调qaq

#include <stdio.h>
inline int read()
{
	int num=0;char c=getchar();
	while(c<48||c>57)c=getchar();
	while(c>47&&c<58)num=(num<<3)+(num<<1)+(c^48),c=getchar();
	return num;
}
#define int long long
const int mod = 571373;
int qp(int x,int p)
{
	int res=1;
	while(p)
	{
		if(p&1)res=res*x%mod;
		x=x*x%mod;
		p>>=1;
	}
	return res;
}
struct Mod{
	long long x;
	Mod(int x=0):x(x){}
	Mod operator=(const int& xx)
	{
		x=xx;
		return *this;
	}
	Mod operator+(const Mod &b)
	{
		return (Mod)((x+b.x)%571373);
	}
	Mod operator-(const Mod &b)
	{
		return (Mod)((x-b.x+571373)%571373);
	}
	Mod operator*(const Mod &b)
	{
		return (Mod)((x*b.x)%571373);
	}
	Mod operator/(const Mod &b)
	{
		return (Mod)(x*qp(b.x,571371)%571373);
	}
	Mod operator^(const Mod &b)
	{
		return (Mod)(qp(x,b.x));
	}
	Mod operator+(const int &b)
	{
		return (Mod)((x+b)%571373);
	}
	Mod operator-(const int &b)
	{
		return (Mod)((x-b+571373)%571373);
	}
	Mod operator*(const int &b)
	{
		return (Mod)((x*b)%571373);
	}
	Mod operator*=(const int &b)
	{
		x=x*b;
		return *this;
	}
	Mod operator+=(const int &b)
	{
		x=x+b;
		return *this;
	}
	Mod operator*=(const Mod &b)
	{
		x=x*b.x;
		return *this;
	}
	Mod operator+=(const Mod &b)
	{
		x=x+b.x;
		return *this;
	}
	Mod operator/(const int &b)
	{
		return (Mod)(x*qp(b,571371)%571373);
	}
	Mod operator^(const int &b)
	{
		return (Mod)(qp(x,b));
	}
	bool operator==(const int &b)
	{
		return x==b;
	}
};
Mod a[100005<<2],sum[100005<<2],tag[100005<<2],charm[100005<<2];
inline void maintain(int o,int l,int r)
{
	if(l==r)return;
	sum[o]=(sum[o<<1]+sum[o<<1|1])*tag[o]+charm[o]*(r-l+1);
}
inline void pushdown(int o,int l,int r)
{
	if(l==r)
	{
		sum[o]=sum[o]*tag[o]+charm[o];
		tag[o]=1,charm[o]=0;
		return;
	}
	int lc=o<<1,rc=lc^1;
	tag[lc]*=tag[o],charm[lc]*=tag[o],charm[lc]+=charm[o];
	tag[rc]*=tag[o],charm[rc]*=tag[o],charm[rc]+=charm[o];
	tag[o]=1,charm[o]=0;
	maintain(lc,l,l+r>>1);
	maintain(rc,(l+r>>1)+1,r);
	maintain(o,l,r);
}
void mul(int o,int l,int r,int L,int R,Mod x)
{
	if(L<=l&&r<=R)
	{
		tag[o]*=x;
		charm[o]*=x;
		maintain(o,l,r);
		return;
	}
	int m=l+r>>1;
	if(L<=m)mul(o<<1,l,m,L,R,x);
	if(m<R)mul(o<<1|1,m+1,r,L,R,x);
	maintain(o,l,r);
}
Mod query(int o,int l,int r,int L,int R)
{
	if(L<=l&&r<=R)return sum[o];
	pushdown(o,l,r);
	maintain(o,l,r);
	int m=l+r>>1;
	Mod ans=0;
	if(L<=m)ans+=query(o<<1,l,m,L,R);
	if(m<R)ans+=query(o<<1|1,m+1,r,L,R);
	return ans;
}
void add(int o,int l,int r,int L,int R,int x)
{
	if(L<=l&&r<=R)
	{
		charm[o]+=x;
		maintain(o,l,r);
		return;
	}
	pushdown(o,l,r);
	int m=l+r>>1;
	if(L<=m)add(o<<1,l,m,L,R,x);
	if(m<R)add(o<<1|1,m+1,r,L,R,x);
	maintain(o,l,r);
}
signed main()
{
	int N=read(),m=read(),n=1;
	read();
	while(n<N)n<<=1;
	for(int i=n;i<N+n;i++)sum[i]=read();
	for(int i=1;i<n+n;i++)tag[i]=1,charm[i]=0;
	for(int i=n-1;i>=1;i--)sum[i]=sum[i<<1]+sum[i<<1|1];
	while(m--)
	{
		int op=read(),x=read(),y=read(),k;
		if(op!=3)k=read();
		if(op==1)mul(1,1,n,x,y,k);
		else if(op==2)add(1,1,n,x,y,k);
		else printf("%d\n",query(1,1,n,x,y).x);
		for(int i=1;i<=n;i++)printf("%d ",query(1,1,n,i,i).x);puts("");
	}
}
2021/3/5 18:30
加载中...