比赛 EYOI与SBOI开学欢乐赛14th 评测结果 AAAAAAAAAA
题目名称 逆元数列 最终得分 100
用户昵称 ZRQ 运行时间 3.766 s
代码语言 C++ 内存使用 7.10 MiB
提交时间 2022-10-24 21:34:07
显示代码纯文本
#include<bits/stdc++.h> 
#define ll long long
using namespace std;
const int N=200005;
int n,m,p,a[N],b[N],st[N],f[N];
ll s[N][2]; 
char ch;
inline void read(int &x){x=0;ch=getchar();while(ch<48||ch>57)ch=getchar();while(ch>47&&ch<58)x=(x<<3)+(x<<1)+(ch^48),ch=getchar();}
ll poww(int a,int b)
{
	ll res=1,base=a;
	while(b)
	{
		if(b&1) res*=base,res%=p;
		base*=base,base%=p;
		b>>=1;
	}
	return res;
}
ll ny(int x)
{
	return poww(x,p-2);
}
void calc(int i,int cur)
{
	if((st[i]^f[cur])==0)
	{
		s[cur][f[cur]]-=a[i];
		s[cur][f[cur]]+=b[i];
		s[cur][f[cur]^1]+=a[i];
		s[cur][f[cur]^1]-=b[i];
	}
	else
	{
		s[cur][f[cur]]+=a[i];
		s[cur][f[cur]]-=b[i];
		s[cur][f[cur]^1]-=a[i];
		s[cur][f[cur]^1]+=b[i];
	}
	st[i]^=1;
	return ;
}
ll query(int i,int cur)
{
	if((st[i]^f[cur])==0) return a[i];
	return b[i];
}
int main()
{
	freopen("invarray.in","r",stdin);
	freopen("invarray.out","w",stdout);
	read(n),read(m),read(p);
	for(int i=1;i<=n;++i) read(a[i]),b[i]=ny(a[i]);
	int len=sqrt(n);
	for(int i=1;i<=n;++i)
	{
		s[i/len+1][0]+=a[i];
		s[i/len+1][1]+=b[i]; 
	}
	int opt,l,r;
	while(m--)
	{
		read(opt),read(l),read(r);
		if(opt==1)
		{
			if(l/len==r/len)
			{
				int cur=l/len+1;
				for(int i=l;i<=r;++i) calc(i,cur);
			}
			else 
			{
				int j=(l/len+1)*len-1;
				for(int i=l;i<=j;++i) calc(i,l/len+1);
				j=(r/len)*len;
				for(int i=j;i<=r;++i) calc(i,r/len+1);
				for(int i=(l/len+2);i<=(r/len);++i) f[i]^=1;
			}
		}
		else
		{
			ll res=0;
			if(l/len==r/len)
			{
				int cur=l/len+1;
				for(int i=l;i<=r;++i) res+=query(i,cur); 
				printf("%lld\n",res);
			}
			else
			{
				int j=(l/len+1)*len-1;
				for(int i=l;i<=j;++i) res+=query(i,l/len+1);
				j=(r/len)*len;
				for(int i=j;i<=r;++i) res+=query(i,r/len+1);
				for(int i=(l/len+2);i<=(r/len);++i) res+=s[i][f[i]];
				printf("%lld\n",res);
			}
		}
	}
	return 0;
}