P7453 [THUSCH2017] 大魔法师 题解

· · 题解

Soltion

一眼线段树套矩乘

操作有加常数的操作,所以我们需要在矩阵里加上一个 1。

最终的矩阵是长成了:

\begin{bmatrix}A,B,C,1\end{bmatrix}

我们考虑操作

魔力激发

火元素激发水元素能量:A_{i}=A_{i}+B_{i}。

\begin{bmatrix}A,B,C,1\end{bmatrix} \times \begin{bmatrix} 1, 0, 0, 0 \\ 1, 1, 0, 0 \\ 0, 0, 1, 0 \\ 0, 0, 0, 1 \\ \end{bmatrix} = [A+B,B,C,1]

土元素激发火元素能量:B_{i}=B_{i}+C_{i}。

\begin{bmatrix}A,B,C,1\end{bmatrix} \times \begin{bmatrix} 1, 0, 0, 0 \\ 0, 1, 0, 0 \\ 0, 1, 1, 0 \\ 0, 0, 0, 1 \\ \end{bmatrix} = [A,B+C,C,1]

水元素激发土元素能量:C_{i}=C_{i}+A_{i}。

\begin{bmatrix}A,B,C,1\end{bmatrix} \times \begin{bmatrix} 1, 0, 1, 0 \\ 0, 1, 0, 0 \\ 0, 0, 1, 0 \\ 0, 0, 0, 1 \\ \end{bmatrix} = [A,B,C+A,1]

魔力增强

火元素能量定值增强:A_i=A_i+v。

\begin{bmatrix}A,B,C,1\end{bmatrix} \times \begin{bmatrix} 1, 0, 0, 0 \\ 0, 1, 0, 0 \\ 0, 0, 1, 0 \\ v, 0, 0, 1 \\ \end{bmatrix} = [A+v,B,C,1]

水元素能量翻倍增强:B_i=B_i\times v

\begin{bmatrix}A,B,C,1\end{bmatrix} \times \begin{bmatrix} 1, 0, 0, 0 \\ 0, v, 0, 0 \\ 0, 0, 1, 0 \\ 0, 0, 0, 1 \\ \end{bmatrix} = [A,B*v,C,1]

土元素能量吸收融合:C_i=v

\begin{bmatrix}A,B,C,1\end{bmatrix} \times \begin{bmatrix} 1, 0, 0, 0 \\ 0, 1, 0, 0 \\ 0, 0, 0, 0 \\ 0, 0, v, 1 \\ \end{bmatrix} = [A,B,v,1]

对于这些操作,直接把转移矩阵当成线段树乘法里的要乘的数,然后下传即可。

魔力释放

对于魔力释放操作,我们可以直接用线段树把区间内的矩阵都加起来。这样是可以的,我们考虑对于第一类操作来说,通过加法分配律,我们可以证明他是可以区间操作的,第二类操作,我们考虑我们区间和的时候,我们同样的把矩阵最后的那个 1 也加了起来,这样每次在一个区间内修改的时候,加的就是 n 倍的 v,赋值的时候也是 n 倍的 v。所以对于魔力释放是没有影响的。

但是我们注意要卡卡常数,这里提供三种办法:

  1. 我们在矩阵乘的时候可以循环展开,或者直接写64个式子,或者我们跳过 a[i][k] 或者 b[k][j] 为 0 的时候。

  2. 我们考虑在标记下传时,如果此时 tag 时单位矩阵,我们没必要下传

  3. 我们还是考虑标记下穿,因为叶子节点没有儿子,所以我们给叶子节点传标记的时候,只下传到值里,不下传到标记里。

这就是这个题的做法,都是基本的矩阵转移。我们可以参考下代码:

#include<bits/stdc++.h>
using namespace std;
const int MOD=998244353;
const int N=3e5;
typedef struct Matrix
{
    int _[4][4];
    Matrix(int _e=1)
    {
        memset(_,0,sizeof(_));
        _[0][0]=_e,_[1][1]=_e;
        _[2][2]=_e,_[3][3]=_e;
    }
    int *operator[](int _i)
    {
        return _[_i];
    }
    friend Matrix operator+(Matrix a,Matrix b)
    {
        for(int i=0;i<4;++i)
        for(int j=0;j<4;++j)
            a[i][j]=(1ll*a[i][j]+b[i][j])%MOD;
        return a;
    }
    friend Matrix operator*(Matrix a,Matrix b)
    {
        Matrix c=Matrix(0);
        for(int i=0;i<4;++i)
        for(int k=0;k<4;++k)
        {
            if(!a[i][k])
                continue;
            for(int j=0;j<4;++j)
            {
                if(!b[k][j])
                    continue;
                c[i][j]=(1ll*c[i][j]+1ll*a[i][k]*b[k][j]%MOD)%MOD;
            }
        }
        return c;
    }
    friend bool operator!=(Matrix a,Matrix b)
    {
        for(int i=0;i<4;++i)
        for(int j=0;j<4;++j)
            if(a[i][j]!=b[i][j])
                return 1;
        return 0;
    }
} Mat;
Mat mats[N];
typedef struct SegmentTree
{
    struct Node
    {
#define lc (p<<1)
#define rc ((p<<1)|1)
        int l,r;
        Mat val;
        Mat tag;
        int md()
        {
            return ((l+r)>>1);
        }
        int le()
        {
            return (r-l+1);
        }
        bool leaf()
        {
            return (l==r);
        }
    };
    Node t[N*4];
    SegmentTree(){}
    void up(int p)
    {
        t[p].val=t[lc].val+t[rc].val;
    }
    void dwd(int p)
    {
        if(t[p].tag!=Mat())
        {
            t[lc].val=t[lc].val*t[p].tag;
            t[rc].val=t[rc].val*t[p].tag;
            if(!t[lc].leaf())
                t[lc].tag=t[lc].tag*t[p].tag;
            if(!t[rc].leaf())
                t[rc].tag=t[rc].tag*t[p].tag;
            t[p].tag=Mat();
        }
    }
    void build(int l,int r,int p=1)
    {
        t[p].l=l,t[p].r=r;
        if(l==r)
        {
            t[p].val=mats[l];
            return;
        }
        build(l,t[p].md(),lc);
        build(t[p].md()+1,r,rc);
        up(p);
    }
    void mdf(int l,int r,Mat k,int p=1)
    {
        if(l<=t[p].l&&t[p].r<=r)
        {
            t[p].val=t[p].val*k;
            t[p].tag=t[p].tag*k;
            return;
        }
        dwd(p);
        if(l<=t[p].md())
            mdf(l,r,k,lc);
        if(r>t[p].md())
            mdf(l,r,k,rc);
        up(p);
    }
    Mat qry(int l,int r,int p=1)
    {
        if(l<=t[p].l&&t[p].r<=r)
            return t[p].val;
        dwd(p);
        Mat res=0;
        if(l<=t[p].md())
            res=res+qry(l,r,lc);
        if(r>t[p].md())
            res=res+qry(l,r,rc);
        up(p);
        return res;
    }
} Tree;
Tree t;
Mat tmp;
int n,m;
int main()
{
    cin>>n;
    for(int i=1;i<=n;++i)
    {
        cin>>mats[i][0][0];
        cin>>mats[i][0][1];
        cin>>mats[i][0][2];
        mats[i][0][3]=1;
    }
    t.build(1,n);
    cin>>m;
    for(int i=1;i<=m;++i)
    {
        int opt,l,r;
        tmp=Mat();
        cin>>opt>>l>>r;
        if(opt<=6)
        {
            if(opt==1)tmp[1][0]=1;
            if(opt==2)tmp[2][1]=1;
            if(opt==3)tmp[0][2]=1;
            if(opt==4)cin>>tmp[3][0];
            if(opt==5)cin>>tmp[1][1];
            if(opt==6)cin>>tmp[3][2],tmp[2][2]=0;
            t.mdf(l,r,tmp);
        }
        else
        {
            tmp=t.qry(l,r);
            cout<<tmp[0][0]<<' '<<tmp[0][1]<<' '<<tmp[0][2]<<'\n';
        }
    }
    return 0;
}