P7739 [NOI2021] 密码箱 题解

· · 题解

这就是 NOI2021 看似不可做的签到吗?

这道题看着非常不可做,那我们就先抓住一个点研究一下,比如说这个连分数。

若原来的密码值为 \frac{a}{b},我们考虑在数列前加入一个数 x,则现在的密码值即为 \frac{a'}{b'} = x + \frac{b}{a} = \frac{ax + b}{a}。注意到原来的分子变成了现在的分母,和矩阵乘法维护递推的形式很像,因此考虑用线性代数方法维护。我们将密码值表示成向量 \begin{pmatrix}a&b\end{pmatrix},那么前面的操作就可以看成 \begin{pmatrix}a'&b'\end{pmatrix} = \begin{pmatrix}ax + b\\a\end{pmatrix} = \begin{pmatrix}a&b\end{pmatrix} \begin{pmatrix}x&1\\1&0\end{pmatrix}。手玩一下容易发现初始的密码值对应的向量应为 \begin{pmatrix}1&0\end{pmatrix}。

现在我们考虑对 \texttt{W} 和 \texttt{E} 操作的处理。这两个操作其实有很好的性质,但是我们也可以延续前面的方法,用线性代数方法维护。\texttt{W} 操作显然是简单的。我们要使得 \begin{pmatrix}x + 1&1\\1&0\end{pmatrix} = W \begin{pmatrix}x&1\\1&0\end{pmatrix},容易发现 W = \begin{pmatrix}1&1\\0&1\end{pmatrix}。对于 \texttt{E} 操作,我们需要分情况讨论。设现在的密码最后一个数为 x,那么我们先讨论较为简单的 x > 1 的情况,此时显然 E = \begin{pmatrix}1&1\\1&0\end{pmatrix} \begin{pmatrix}1&1\\1&0\end{pmatrix} \begin{pmatrix}1&-1\\0&1\end{pmatrix} = \begin{pmatrix}2&-1\\1&0\end{pmatrix}。我们再讨论 x = 1 的情况。那么此时,E \begin{pmatrix}x&1\\1&0\end{pmatrix} = E \begin{pmatrix}1&1\\1&0\end{pmatrix} = \begin{pmatrix}1&1\\1&0\end{pmatrix} \begin{pmatrix}1&1\\0&1\end{pmatrix} = \begin{pmatrix}1&2\\1&1\end{pmatrix},经过计算易得 E = \begin{pmatrix}2&-1\\1&0\end{pmatrix},两种情况恰好在线性代数的意义下相同!

那么剩下的问题就简单了。查询的形式显然指向了 FHQ Treap 或 Splay,维护这些矩阵的乘积是简单的。

参考代码中使用了 FHQ Treap。

建议降紫?

#include <bits/stdc++.h>
#define ll long long
#define mod 998244353LL
#define inf 0x3f3f3f3f3f3f3f3fLL
#define matE matrix(2, 998244352, 1, 0)
#define matW matrix(1, 1, 0, 1)
using namespace std;

struct matrix {
    ll f[2][2];
    matrix(ll f1_1 = 1, ll f1_2 = 0, ll f2_1 = 0, ll f2_2 = 1) { f[0][0] = f1_1; f[0][1] = f1_2; f[1][0] = f2_1; f[1][1] = f2_2; }
    matrix operator*(const matrix& b) { return matrix(((f[0][0] * b.f[0][0]) % mod + f[0][1] * b.f[1][0]) % mod, ((f[0][0] * b.f[0][1]) % mod + f[0][1] * b.f[1][1]) % mod, ((f[1][0] * b.f[0][0]) % mod + f[1][1] * b.f[1][0]) % mod, ((f[1][0] * b.f[0][1]) % mod + f[1][1] * b.f[1][1]) % mod); }
    pair <ll, ll> ansval() { return make_pair(f[0][0], f[0][1]); }
};

random_device seed;
mt19937_64 rwght(seed());
struct segment {
    int sz; int son[2]; ll w;
    bool ftag, rtag; matrix cval, cfval, val, fval, rval, rfval;
    segment() { w = rwght(); }
    segment(char opt) { if(opt == 'E') cval = val = rval = matE, cfval = fval = rfval = matW; else cval = val = rval = matW, cfval = fval = rfval = matE; w = rwght(); }
};
inline void merge(segment a, segment b, segment& c) { c.val = b.val * c.cval * a.val, c.fval = b.fval * c.cfval * a.fval, c.rval = a.rval * c.cval * b.rval, c.rfval = a.rfval * c.cfval * b.rfval; c.sz = a.sz + b.sz + 1;  return; }
inline void flip(segment& a) { a.ftag ^= 1; swap(a.val, a.fval); swap(a.rval, a.rfval); swap(a.cval, a.cfval); return; }
inline void reverse(segment& a) { a.rtag ^= 1; swap(a.val, a.rval); swap(a.fval, a.rfval); return; }

int cntreap, root;
segment treap[400009];
inline void down(int x) {
    if(treap[x].ftag) { flip(treap[treap[x].son[0]]), flip(treap[treap[x].son[1]]); treap[x].ftag = false; }
    if(treap[x].rtag) { swap(treap[x].son[0], treap[x].son[1]); reverse(treap[treap[x].son[0]]), reverse(treap[treap[x].son[1]]); treap[x].rtag = false; }
    return;
}
void split(int rnk, int x, int& a, int& b) {
    if(!x) { a = 0, b = 0; return; } down(x);
    if(rnk <= treap[treap[x].son[0]].sz) { split(rnk, treap[x].son[0], a, b); treap[x].son[0] = b, b = x; }
    else { split(rnk - treap[treap[x].son[0]].sz - 1, treap[x].son[1], a, b); treap[x].son[1] = a, a = x; }
    merge(treap[treap[x].son[0]], treap[treap[x].son[1]], treap[x]);
    return;
}
void merge(int a, int b, int& x) {
    down(a), down(b);
    if(!a || !b) { x = a + b; return; }
    if(treap[a].w < treap[b].w) x = a, merge(treap[x].son[1], b, treap[x].son[1]);
    else x = b, merge(a, treap[x].son[0], treap[x].son[0]);
    merge(treap[treap[x].son[0]], treap[treap[x].son[1]], treap[x]);
    return;
}
inline void split(int x, int y, int& l, int& mid, int& r) { split(x - 1, root, l, r), split(y - treap[l].sz, r, mid, r); return; }
inline void insert(int x, char val) {
    if(x < 0 || x > treap[root].sz) return;
    int l = 0, r = 0; split(x, root, l, r);
    treap[++cntreap] = val, treap[cntreap].sz++;
    merge(l, cntreap, l), merge(l, r, root);
    return;
}
inline void reverse(int x, int y) {
    if(x < 1 || y > treap[root].sz || x > y) return;
    int l = 0, mid = 0, r = 0; split(x, y, l, mid, r); 
    reverse(treap[mid]);
    merge(l, mid, l), merge(l, r, root);
    return;
}
inline void flip(int x, int y) {
    if(x < 1 || y > treap[root].sz || x > y) return;
    int l = 0, mid = 0, r = 0; split(x, y, l, mid, r); 
    flip(treap[mid]);
    merge(l, mid, l), merge(l, r, root);
    return;
}

int n, m, l, r; char opt[19], val[100009];
pair <ll, ll> ans;

int main() {
    scanf("%d %d", &n, &m); scanf("%s", val + 1); for(int i = 1; i <= n; i++) insert(i - 1, val[i]);
    ans = (treap[root].val * matW).ansval(); printf("%lld %lld\n", ans.first, ans.second);
    for(int i = 1; i <= m; i++) { 
        scanf("%s", opt); if(opt[0] == 'A') { scanf("%s", val); insert(treap[root].sz, val[0]); }
        else { scanf("%d %d", &l, &r); if(opt[0] == 'R') reverse(l, r); else flip(l, r); }
        ans = (treap[root].val * matW).ansval(); printf("%lld %lld\n", ans.first, ans.second);
    }
    return 0;
}