题目大意
给定一个长为 的序列 。支持如下两种操作。
- :将区间 内每个数都变成自己的立方。
- :求
思路
乍一看无从下手,但是看这个模数这么小,肯定有什么玄机。
一开始我考虑,因为模数这么小,超过40几的数立方就会爆掉,可以从这方面考虑,但是想了想也不知道该维护些什么。
再一看,这个模数很像是质数(也不管是不是了),再考虑立方,很有可能会有循环节,打表发现,循环节只能是
中的一个数,看起来有点东西。
考虑线段树,一个节点维护48个值,分别是这个数立方48次之内的值。对于每一次修改,将这个数组循环向左移动一位,这样的修改是满足lazy tag的性质的,可以打标记。
坑点
万恶的ZOJ啊,丑陋无比。
- 数组开大了给我segmentation fault,数组大了说我RE??
- 用个pow足足慢了1.5s直接从A变T可还行
- 貌似左右儿子用 比用 要快上那么一些些??
代码
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
#include<cctype>
#define pa pair<int,int>
#define INF 0x3f3f3f3f
#define inf 0x3f
#define fi first
#define se second
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define pb push_back
using namespace std;
inline int read()
{
int f=1,sum=0;
char c=getchar();
while (!isdigit(c)) {if (c=='-') f=-1;c=getchar();}
while (isdigit(c)) {sum=sum*10+c-'0';c=getchar();}
return sum*f;
}
const int MAXN=100010;
#define Mod 99971
#define T (48)
int f[4*MAXN][T];
int a[MAXN];
void maintain(int root)
{
for (int i=0;i<T;i++)
f[root][i]=f[2*root][i]+f[2*root+1][i],f[root][i]%=Mod;
}
int g[4*MAXN],tmp[T];
void build(int root,int left,int right)
{
if (left==right)
{
f[root][0]=a[left];
for (int i=1;i<T;i++)
f[root][i]=1ll*f[root][i-1]*f[root][i-1]*f[root][i-1]%Mod;
return ;
}
int mid=(left+right)>>1;
build(2*root,left,mid);
build(2*root+1,mid+1,right);
maintain(root);
}
void pushdown(int root)
{
int len=g[root];
g[2*root]+=g[root],g[2*root+1]+=g[root];
memcpy(tmp,f[2*root],sizeof(f[2*root]));
for (int i=0;i<T;i++)
f[2*root][i]=tmp[(i+len+T)%T];
memcpy(tmp,f[2*root+1],sizeof(f[2*root+1]));
for (int i=0;i<T;i++)
f[2*root+1][i]=tmp[(i+len+T)%T];
g[root]=0;
}
void update(int root,int left,int right,int qleft,int qright)
{
if (qleft<=left && right<=qright)
{
g[root]++;
memcpy(tmp,f[root],sizeof(f[root]));
for (int i=0;i<T;i++)
f[root][i]=tmp[(i+1+T)%T];
return ;
}
if (g[root])
pushdown(root);
int mid=(left+right)>>1;
if (qright<=mid)
update(2*root,left,mid,qleft,qright);
else if (qleft>mid)
update(2*root+1,mid+1,right,qleft,qright);
else
{
update(2*root,left,mid,qleft,mid);
update(2*root+1,mid+1,right,mid+1,qright);
}
maintain(root);
}
int query(int root,int left,int right,int qleft,int qright)
{
if (qleft<=left && right<=qright)
return (int)f[root][0];
if (g[root])
pushdown(root);
int mid=(left+right)>>1;
if (qright<=mid)
return query(2*root,left,mid,qleft,qright);
else if (qleft>mid)
return query(2*root+1,mid+1,right,qleft,qright);
else
{
int ans1,ans2;
ans1=query(2*root,left,mid,qleft,mid);
ans2=query(2*root+1,mid+1,right,mid+1,qright);
return (ans1+ans2)%Mod;
}
maintain(root);
}
int main()
{
int Test;
scanf("%d",&Test);
while (Test--)
{
memset(f,0,sizeof(f));
memset(g,0,sizeof(g));
int n,m;
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
a[i]=read(),a[i]%=Mod;
build(1,1,n);
for (int i=1;i<=m;i++)
{
int opt,l,r;
scanf("%d%d%d",&opt,&l,&r);
if (opt==1)
update(1,1,n,l,r);
else
printf("%d\n",query(1,1,n,l,r));
}
}
return 0;
}