感觉本题没有太多思维难度,纯DS
大致思路把每个位置的三个属性看做一个
1
×
4
1\times 4
1×4 的矩阵(第四个位置维护常数项),并用线段树维护区间矩阵和
同时,我们计算出六种修改操作的转移矩阵,修改时直接将原矩阵乘上转移矩阵。由于矩阵满足结合律与分配率,所以可以将单点修改扩展为区间修改。
下面是分别六种操作的转移矩阵
- [ 1 0 0 0 1 1 0 0 0 0 1 0 0 0 0 1 ] \begin{bmatrix} 1 \space 0 \space 0 \space 0 \\ 1 \space 1 \space 0 \space 0 \\ 0 \space 0 \space 1 \space 0 \\ 0 \space 0 \space 0 \space 1 \end{bmatrix} ⎣⎢⎢⎡1 0 0 01 1 0 00 0 1 00 0 0 1⎦⎥⎥⎤
- [ 1 0 0 0 0 1 0 0 0 1 1 0 0 0 0 1 ] \begin{bmatrix} 1 \space 0 \space 0 \space 0 \\ 0 \space 1 \space 0 \space 0 \\ 0 \space 1 \space 1 \space 0 \\ 0 \space 0 \space 0 \space 1 \end{bmatrix} ⎣⎢⎢⎡1 0 0 00 1 0 00 1 1 00 0 0 1⎦⎥⎥⎤
- [ 1 0 1 0 0 1 0 0 0 0 1 0 0 0 0 1 ] \begin{bmatrix} 1 \space 0 \space 1 \space 0 \\ 0 \space 1 \space 0 \space 0 \\ 0 \space 0 \space 1 \space 0 \\ 0 \space 0 \space 0 \space 1 \end{bmatrix} ⎣⎢⎢⎡1 0 1 00 1 0 00 0 1 00 0 0 1⎦⎥⎥⎤
- [ 1 0 0 0 0 1 0 0 0 0 1 0 v 0 0 1 ] \begin{bmatrix} 1 \space 0 \space 0 \space 0 \\ 0 \space 1 \space 0 \space 0 \\ 0 \space 0 \space 1 \space 0 \\ v \space 0 \space 0 \space 1 \end{bmatrix} ⎣⎢⎢⎡1 0 0 00 1 0 00 0 1 0v 0 0 1⎦⎥⎥⎤
- [ 1 0 0 0 0 v 0 0 0 0 1 0 0 0 0 1 ] \begin{bmatrix} 1 \space 0 \space 0 \space 0 \\ 0 \space v \space 0 \space 0 \\ 0 \space 0 \space 1 \space 0 \\ 0 \space 0 \space 0 \space 1 \end{bmatrix} ⎣⎢⎢⎡1 0 0 00 v 0 00 0 1 00 0 0 1⎦⎥⎥⎤
- [ 1 0 0 0 0 1 0 0 0 0 0 0 0 0 v 1 ] \begin{bmatrix} 1 \space 0 \space 0 \space 0 \\ 0 \space 1 \space 0 \space 0 \\ 0 \space 0 \space 0 \space 0 \\ 0 \space 0 \space v \space 1 \end{bmatrix} ⎣⎢⎢⎡1 0 0 00 1 0 00 0 0 00 0 v 1⎦⎥⎥⎤
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#pragma GCC optimize(2)
const int Maxn=2.5e5+10;
const int Maxm=Maxn<<2;
const int mod=998244353;
const int m=4;
struct mat{
int d[6][6];
bool operator ==(const mat &x)const
{
for(int i=1;i<=m;++i)
for(int j=1;j<=m;++j)
if(x.d[i][j]!=d[i][j])
return 0;
return 1;
}
}f[Maxm],I;
struct ary{
int d[6];
bool operator ==(const ary &x)const
{
for(int i=1;i<=m;++i)
if(x.d[i]!=d[i])
return 0;
return 1;
}
}c[Maxm],a[Maxn];
int n,q;
inline int add_mod(int x,int y)
{
int tmp=(x+y);
if(tmp>=mod)tmp-=mod;
return tmp;
}
inline int mul_mod(int x,int y)
{
return (1ll*x*y)%mod;
}
inline void check_add(int &x,int y)
{
x=add_mod(x,y);
}
inline void check_mul(int &x,int y)
{
x=mul_mod(x,y);
}
mat mul(mat a,mat b)
{
mat c;memset(c.d,0,sizeof(c.d));
for(int i=1;i<=m;++i)
for(int j=1;j<=m;++j)
for(int k=1;k<=m;++k)
check_add(c.d[i][j],mul_mod(a.d[i][k],b.d[k][j]));
return c;
}
ary add(ary a,ary b)
{
ary c;
for(int i=1;i<=m;++i)
c.d[i]=add_mod(a.d[i],b.d[i]);
return c;
}
inline int read()
{
int s=0,w=1;
char ch=getchar();
while(ch<'0' || ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0' && ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
return s*w;
}
inline void push_up(int k)
{
c[k]=add(c[k<<1],c[k<<1|1]);
}
inline void upd(int k,mat v)
{
f[k]=mul(f[k],v);
ary t;memset(t.d,0,sizeof(t.d));
for(int i=1;i<=m;++i)
for(int j=1;j<=m;++j)
check_add(t.d[i],mul_mod(c[k].d[j],v.d[j][i]));
c[k]=t;
}
inline void push_down(int k)
{
if(f[k]==I)return;
upd(k<<1,f[k]);
upd(k<<1|1,f[k]);
f[k]=I;
}
void modify(int k,int l,int r,int x,int y,mat v)
{
if(x<=l && r<=y)return upd(k,v);
push_down(k);
int mid=(l+r)>>1;
if(x<=mid)modify(k<<1,l,mid,x,y,v);
if(mid<y)modify(k<<1|1,mid+1,r,x,y,v);
push_up(k);
}
ary query(int k,int l,int r,int x,int y)
{
if(x<=l && r<=y)return c[k];
push_down(k);
int mid=(l+r)>>1;
ary ret;memset(ret.d,0,sizeof(ret.d));
if(x<=mid)ret=query(k<<1,l,mid,x,y);
if(mid<y)ret=add(ret,query(k<<1|1,mid+1,r,x,y));
return ret;
}
void build(int k,int l,int r)
{
f[k]=I;
// if(l==r)
// {
// c[k]=a[l];
// printf("k = %d\n",k);
// for(int i=1;i<=m;++i)
// printf("%d ",c[k].d[i]);
// putchar('\n');
// }
if(l==r){c[k]=a[l];return;}
int mid=(l+r)>>1;
build(k<<1,l,mid);
build(k<<1|1,mid+1,r);
push_up(k);
// printf("k = %d\n",k);
// for(int i=1;i<=m;++i)
// printf("%d ",c[k].d[i]);
// putchar('\n');
}
int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
n=read();
memset(I.d,0,sizeof(I.d));
for(int i=1;i<=m;++i)
I.d[i][i]=1;
for(int i=1;i<=n;++i)
{
a[i].d[1]=read();
a[i].d[2]=read();
a[i].d[3]=read();
a[i].d[4]=1;
}
build(1,1,n);
q=read();
while(q--)
{
int opt=read(),l=read(),r=read();
if(opt==7)
{
ary tmp=query(1,1,n,l,r);
for(int i=1;i<m;++i)
printf("%d ",tmp.d[i]);
putchar('\n');continue;
}
mat t=I;
if(opt==1)t.d[2][1]=1;
if(opt==2)t.d[3][2]=1;
if(opt==3)t.d[1][3]=1;
if(opt==4)t.d[4][1]=read();
if(opt==5)t.d[2][2]=read();
if(opt==6)
{
t.d[3][3]=0;
t.d[4][3]=read();
}
modify(1,1,n,l,r,t);
}
return 0;
}