P7739 [NOI2021] 密码箱 题解
PlatinumBessieS · · 题解
这就是 NOI2021 看似不可做的签到吗?
这道题看着非常不可做,那我们就先抓住一个点研究一下,比如说这个连分数。
若原来的密码值为
现在我们考虑对
那么剩下的问题就简单了。查询的形式显然指向了 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;
}