蒟蒻切的第一题黑,写一篇题解纪念一下
首先看到题目里面的式子,考虑当前序列为 时,经过一次变换会变成什么情况:末项变成 ,其余不变。想了2h想出用矩阵表示这个过程: ,用矩阵乘法来描述这个变换,设
容易得到
然后现在的序列就成了这样:
然后看操作,W操作相当于
,
还是考虑用矩乘描述:
可以得到 ,所以W操作相当于在序列末尾乘上一个。
看E操作,先考虑 的情况,相当于在序列末尾乘上
,
第一项矩阵是令 ,后两项是在序列末尾添加两个 ,合并一下,相当于在序列末乘上
。然后考虑 的情况,相当于后两项
,合并后两项
,在中间插入一个矩阵很麻烦,考虑转化成和之前一样在末尾添加的形式,
设
,
发现
,刚好也等于
,所以一个 E 操作就相当于在序列末乘上一个
。
接下来因为有一个区间翻转操作,容易想到用平衡树维护,具体细节看代码,这题的难度大部分在想前面的矩阵上面,大概下位黑
ACcode
#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;
}