跳到正文

题解

P7739 [NOI2021] 密码箱 题解

_Chrono_约 13 分钟
原文
文章信息与版本

文章信息

发布
2026年4月4日
收录
2026年8月26日
更新
2026年11月18日
篇幅
约 13 分钟
题目
P7739

版本

  1. v1当前

蒟蒻切的第一题黑,写一篇题解纪念一下

题目传送门

首先看到题目里面的式子,考虑当前序列为 a1,a2,…,ak−1,aba_1,a_2,\dots,a_{k-1},\frac{a}{b} 时,经过一次变换会变成什么情况:末项变成 ak−1+ba=ak−1⋅a+baa_{k-1}+\frac{b}{a}=\frac{a_{k-1}\cdot a+b}{a},其余不变。想了2h想出用矩阵表示这个过程:[ab]\begin{bmatrix} a \\ b \end{bmatrix} →\to [ak−1⋅a+ba]\begin{bmatrix} a_{k-1}\cdot a+b \\ a \end{bmatrix},用矩阵乘法来描述这个变换,设

[wxyz]×[ab]=[ak−1⋅a+ba]\begin{bmatrix}w & x \\y & z\end{bmatrix} \times\begin{bmatrix}a \\ b\end{bmatrix}=\begin{bmatrix} a_{k-1}\cdot a+b \\ a \end{bmatrix}

容易得到

[wxyz]=[ak−1110]\begin{bmatrix} w & x\\ y & z \end{bmatrix} = \begin{bmatrix} a_{k-1} & 1\\ 1 & 0 \end{bmatrix}

然后现在的序列就成了这样:

[a0110]×[a1110]×[a2110]×⋯×[ak110]\begin{bmatrix} a_0 & 1\\ 1 & 0 \end{bmatrix} \times \begin{bmatrix} a_1 & 1\\ 1 & 0 \end{bmatrix} \times \begin{bmatrix} a_2 & 1\\ 1 & 0 \end{bmatrix} \times \dots \times \begin{bmatrix} a_{k} & 1\\ 1 & 0 \end{bmatrix}

然后看操作,W操作相当于 [ak110]\begin{bmatrix} a_{k} & 1\\ 1 & 0 \end{bmatrix} →\to [ak+1110]\begin{bmatrix} a_{k}+1 & 1\\ 1 & 0 \end{bmatrix}, 还是考虑用矩乘描述:

[ak110]×[wxyz]=[ak+1110]\begin{bmatrix} a_{k} & 1\\ 1 & 0 \end{bmatrix} \times \begin{bmatrix} w & x\\ y & z \end{bmatrix} = \begin{bmatrix} a_{k}+1 & 1\\ 1 & 0 \end{bmatrix}

可以得到 [wxyz]=[1011]\begin{bmatrix} w & x\\ y & z \end{bmatrix} = \begin{bmatrix} 1 & 0 \\ 1 & 1 \end{bmatrix},所以W操作相当于在序列末尾乘上一个[1011]\begin{bmatrix} 1 & 0 \\ 1 & 1 \end{bmatrix}。

看E操作,先考虑 ak≠1a_k \ne 1 的情况,相当于在序列末尾乘上 [10−11]×[1110]×[1110]\begin{bmatrix} 1 & 0 \\ -1 & 1 \end{bmatrix} \times \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix} \times \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}, 第一项矩阵是令 ak→ak−1a_k \to a_k-1,后两项是在序列末尾添加两个 11,合并一下,相当于在序列末乘上 [21−10]\begin{bmatrix} 2 & 1 \\ -1 & 0 \end{bmatrix}。然后考虑 ak=1a_k = 1 的情况,相当于后两项 [ak−1110]×[1110]→[ak−1110]×[1011]×[1110]\begin{bmatrix} a_{k-1} & 1\\ 1 & 0 \end{bmatrix} \times \begin{bmatrix} 1 & 1\\ 1 & 0 \end{bmatrix} \to \begin{bmatrix} a_{k-1} & 1\\ 1 & 0 \end{bmatrix} \times \begin{bmatrix} 1 & 0\\ 1 & 1 \end{bmatrix} \times \begin{bmatrix} 1 & 1\\ 1 & 0 \end{bmatrix},合并后两项 [1011]×[1110]=[1121]\begin{bmatrix} 1 & 0\\ 1 & 1 \end{bmatrix} \times \begin{bmatrix} 1 & 1\\ 1 & 0 \end{bmatrix}= \begin{bmatrix} 1 & 1\\ 2 & 1 \end{bmatrix},在中间插入一个矩阵很麻烦,考虑转化成和之前一样在末尾添加的形式, 设 [1110]×[wxyz]=[1121]\begin{bmatrix} 1 & 1\\ 1 & 0 \end{bmatrix}\times \begin{bmatrix} w & x\\ y & z \end{bmatrix}= \begin{bmatrix} 1 & 1\\ 2 & 1 \end{bmatrix}, 发现 [wxyz]\begin{bmatrix} w & x\\ y & z \end{bmatrix},刚好也等于 [21−10]\begin{bmatrix} 2 & 1 \\ -1 & 0 \end{bmatrix},所以一个 E 操作就相当于在序列末乘上一个 [21−10]\begin{bmatrix} 2 & 1 \\ -1 & 0 \end{bmatrix}。

接下来因为有一个区间翻转操作,容易想到用平衡树维护,具体细节看代码,这题的难度大部分在想前面的矩阵上面,大概下位黑

ACcode
C++
#include <iostream>
#include <random>

namespace preFile
{
    void init()
    {
#ifndef ONLINE_JUDGE
        freopen("D:\\C++\\Debug\\lg.in", "r", stdin);
        freopen("D:\\C++\\Debug\\lg.out", "w", stdout);
#endif
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout.tie(nullptr);
    }
}

std::mt19937 rnd(std::random_device{}());
const int maxn = 2e5 + 3;
const int mod = 998244353;
int n, q, cnt, rt;
std::string s;
char flip(char c) { return (c == 'W' ? 'E' : 'W'); }

struct Mat
{
    int a[2][2];
    Mat() { a[0][0] = 1, a[1][1] = 1,  a[0][1] = a[1][0] = 0; }

    Mat(char c)
    {
        if (c == 'E')  a[0][0] = 2, a[1][0] = mod - 1, a[0][1] = 1, a[1][1] = 0;
        else a[0][0] = 1, a[0][1] = 0, a[1][0] = 1, a[1][1] = 1;
    }

    Mat operator*(const Mat& o) const
    {
        Mat ret; ret.a[0][0] = ret.a[0][1] = ret.a[1][0] = ret.a[1][1] = 0;
        for (int k = 0; k <= 1; k++)
            for (int i = 0; i <= 1; i++)
                for (int j = 0; j <= 1; j++)
                    ret.a[i][j] = (ret.a[i][j] + 1LL * a[i][k] * o.a[k][j]) % mod;
        return ret;
    }
} fir('W');

struct node
{
    Mat sum, revs, flis, frs; char tp; int siz, pri, rev, fli, ls, rs;
    node(char c) : sum(Mat(c)), revs(Mat(c)), flis(Mat(flip(c))), frs(Mat(flip(c))), 
                   tp(c), siz(1), pri((int)rnd()), rev(0), fli(0), ls(0), rs(0) {}
    node() : sum(Mat()), revs(Mat()), flis(Mat()), frs(Mat()),
             tp(' '), siz(0), pri((int)rnd()), rev(0), fli(0), ls(0), rs(0) {}
} t[maxn];

void pushup(int u)
{
    t[u].siz = t[t[u].ls].siz + t[t[u].rs].siz + 1;
    t[u].sum = t[t[u].ls].sum * Mat(t[u].tp) * t[t[u].rs].sum;
    t[u].revs = t[t[u].rs].revs * Mat(t[u].tp) * t[t[u].ls].revs;
    t[u].flis = t[t[u].ls].flis * Mat(flip(t[u].tp)) * t[t[u].rs].flis;
    t[u].frs = t[t[u].rs].frs * Mat(flip(t[u].tp)) * t[t[u].ls].frs;
}

void makefli(int u)
{
    t[u].fli ^= 1; t[u].tp = flip(t[u].tp);
    std::swap(t[u].sum, t[u].flis);
    std::swap(t[u].revs, t[u].frs);
}

void makerev(int u)
{
    t[u].rev ^= 1;
    std::swap(t[u].ls, t[u].rs);
    std::swap(t[u].sum, t[u].revs);
    std::swap(t[u].flis, t[u].frs);
}

void pushdown(int u)
{
    if (t[u].fli)
    {
        if (t[u].ls) makefli(t[u].ls);
        if (t[u].rs) makefli(t[u].rs);
        t[u].fli = 0;
    }
    if (t[u].rev)
    {
        if (t[u].ls) makerev(t[u].ls);
        if (t[u].rs) makerev(t[u].rs);
        t[u].rev = 0;
    }
}

void split(int u, int siz, int& lp, int& rp)
{
    if (!u) return lp = rp = 0, void(); pushdown(u);
    if (t[t[u].ls].siz < siz) lp = u, split(t[u].rs, siz - t[t[u].ls].siz - 1, t[u].rs, rp);
    else rp = u, split(t[u].ls, siz, lp, t[u].ls); pushup(u);
}

int merge(int u, int v)
{
    if (!u || !v) return u | v;
    pushdown(u), pushdown(v);
    if (t[u].pri > t[v].pri)  return t[u].rs = merge(t[u].rs, v), pushup(u), u;
    else return t[v].ls = merge(u, t[v].ls), pushup(v), v;
}

void append(char c)
{
    t[++cnt] = node(c);
    rt = merge(rt, cnt);
}

void flip(int l, int r)
{
    int lp, mid, rp;
    split(rt, r, lp, rp);
    split(lp, l - 1, lp, mid);
    makefli(mid);
    rt = merge(lp, merge(mid, rp));
}

void reverse(int l, int r)
{
    int lp, mid, rp;
    split(rt, r, lp, rp);
    split(lp, l - 1, lp, mid);
    makerev(mid);
    rt = merge(lp, merge(mid, rp));
}

void printans()
{
    Mat tmp = fir * t[rt].sum;
    std::cout << tmp.a[0][0] << ' ' << tmp.a[1][0] << '\n';
}

int main()
{
    preFile::init();
    std::cin >> n >> q;
    for (int i = 1; i <= n; i++)
    {
        char c;  std::cin >> c;
        append(c);
    }
    printans();
    while (q--)
    {
        std::string op; std::cin >> op;
        if (op[0] == 'A')
        {
            char c; std::cin >> c;
            append(c);
        }
        else if (op[0] == 'R')
        {
            int l, r; std::cin >> l >> r;
            reverse(l, r);
        }
        else
        {
            int l, r; std::cin >> l >> r;
            flip(l, r);
        }
        printans();
    }
    return 0;
}
原文