题解:P13274 [NOI 2025] 三目运算符
Redshift_Shine · · 题解
场外选手,仅通过小样例,结论似乎没错,所以过来写一发。
通过观察和上厕所可观察到
而通过手推和想象可以将
由此类推不断想象可以得到以下结论:
- 若串中出现子串
\texttt{101} ,则该子串中的第二个\texttt{1} 在第一次操作中一定会变为\texttt{0} ,此时答案不小于1 。 - 前缀
\texttt{0} 无法对字符串造成任何改变。 - 找到字符串中的第一个
\texttt{110} 子串在字符串中的 0-index 下标i ,则答案为n-i-2 。该子串将会不断将\texttt{0} 向后推直到推出字符串边界。对于其余情况,\texttt{100} 无法对答案造成影响,而所有连环存在的\texttt{01010101...} 都会在一次操作内变为\texttt{01000000...} 。否则,若不存在子串\texttt{110} ,由第一步骤决定答案。
由此,通过在线段树节点内存储该节点内:
- 第一个
\texttt{0} 的位置 - 第一个
\texttt{1} 的位置 - 是否以
\texttt{0} 开头、结尾 - 是否以
\texttt{1} 开头、结尾 - 是否以
\texttt{01} 开头、结尾 - 是否以
\texttt{10} 开头、结尾 - 是否存在
\texttt{010} - 是否存在
\texttt{101} - 懒标记
可以解决该问题。时间复杂度
以下是代码,但极其不建议参考。
#ifdef Redshift_Debug
#define debug(...) fprintf(stderr, __VA_ARGS__)
#include <chrono>
#else
#define debug(...)
#endif
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
const int N = 4e5 + 10;
using ll = long long;
int n, q, tps;
string s;
ll res;
struct st
{
int ps0, ps1;
bool l0, r0, sc0, l1, r1, sc1, l10, l01, r10, r01, exc101, exc010, tag;
st operator+(const st &x) const
{
return st{min(ps0, x.ps0),
min(ps1, x.ps1),
l0,
x.r0,
sc0 or x.sc0 or (r0 and x.l0),
l1,
x.r1,
sc1 or x.sc1 or (r1 and x.l1),
l10,
l01,
x.r10,
x.r01,
exc010 or x.exc010 or (r0 and x.l10) or (r01 and x.l1),
exc101 or x.exc101 or (r1 and x.l01) or (r10 and x.l0),
false};
}
void flip()
{
swap(ps0, ps1);
swap(l0, l1);
swap(r0, r1);
swap(sc0, sc1);
swap(l10, l01);
swap(r10, r01);
swap(exc010, exc101);
tag ^= 1;
}
} tr[N << 2];
void init_global()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
}
void build(int x, int l, int r)
{
if (l == r)
{
tr[x] = st{(s[l] == '0' ? l : n),
(s[l] == '1' ? l : n),
(s[l] == '0'),
(s[l] == '0'),
false,
(s[l] == '1'),
(s[l] == '1'),
false,
false,
false,
false,
false,
false,
false,
false};
return;
}
int mid = (l + r) >> 1;
build(x << 1, l, mid);
build(x << 1 | 1, mid + 1, r);
tr[x] = tr[x << 1] + tr[x << 1 | 1];
if (l == mid)
tr[x].l01 |= (tr[x << 1].r0 and tr[x << 1 | 1].l1), tr[x].l10 |= (tr[x << 1].r1 and tr[x << 1 | 1].l0);
if (r == mid + 1)
tr[x].r01 |= (tr[x << 1].r0 and tr[x << 1 | 1].l1), tr[x].r10 |= (tr[x << 1].r1 and tr[x << 1 | 1].l0);
}
void init_local()
{
cin >> n >> q;
cin >> s;
if (n != s.size())
s = s.substr(s.size() - n);
build(1, 0, n - 1);
}
void psh(int x)
{
tr[x << 1].flip(), tr[x << 1 | 1].flip();
tr[x].tag = false;
}
void update(int x, int l, int r, int lb, int rb)
{
if (l >= lb and r <= rb)
{
tr[x].flip();
return;
}
if (tr[x].tag)
psh(x);
int mid = (l + r) >> 1;
if (lb <= mid)
update(x << 1, l, mid, lb, rb);
if (rb > mid)
update(x << 1 | 1, mid + 1, r, lb, rb);
tr[x] = tr[x << 1] + tr[x << 1 | 1];
if (l == mid)
tr[x].l01 |= (tr[x << 1].r0 and tr[x << 1 | 1].l1), tr[x].l10 |= (tr[x << 1].r1 and tr[x << 1 | 1].l0);
if (r == mid + 1)
tr[x].r01 |= (tr[x << 1].r0 and tr[x << 1 | 1].l1), tr[x].r10 |= (tr[x << 1].r1 and tr[x << 1 | 1].l0);
}
int qrysc1(int x, int l, int r)
{
if (l != r and tr[x].tag)
psh(x);
int mid = (l + r) >> 1;
int ret = 0;
if (tr[x << 1].sc1)
ret = qrysc1(x << 1, l, mid);
else if (tr[x << 1].r1 and tr[x << 1 | 1].l1)
ret = mid + 1;
else
ret = qrysc1(x << 1 | 1, mid + 1, r);
tr[x] = tr[x << 1] + tr[x << 1 | 1];
if (l == mid)
tr[x].l01 |= (tr[x << 1].r0 and tr[x << 1 | 1].l1), tr[x].l10 |= (tr[x << 1].r1 and tr[x << 1 | 1].l0);
if (r == mid + 1)
tr[x].r01 |= (tr[x << 1].r0 and tr[x << 1 | 1].l1), tr[x].r10 |= (tr[x << 1].r1 and tr[x << 1 | 1].l0);
return ret;
}
int qryp0(int x, int l, int r, int tar)
{
if (l >= tar)
return tr[x].ps0;
if (tr[x].tag)
psh(x);
int mid = (l + r) >> 1;
if (tar > mid)
return qryp0(x << 1 | 1, mid + 1, r, tar);
return min(qryp0(x << 1, l, mid, tar), tr[x << 1 | 1].ps0);
}
void run()
{
res = 0;
if (tr[1].sc1)
{
tps = qrysc1(1, 0, n - 1);
res = max((int)tr[1].exc101, n - qryp0(1, 0, n - 1, tps + 1));
}
else if (tr[1].exc101)
res ^= 1;
for (int i = 1, l, r; i <= q; i++)
{
cin >> l >> r;
l--, r--;
update(1, 0, n - 1, l, r);
if (tr[1].sc1)
{
tps = qrysc1(1, 0, n - 1);
res ^= 1ll * (i + 1) * max((int)tr[1].exc101, n - qryp0(1, 0, n - 1, tps + 1));
}
else if (tr[1].exc101)
res ^= (i + 1);
}
cout << res << '\n';
}
int main()
{
#ifdef Redshift_Debug
auto st = chrono::high_resolution_clock::now();
#endif
int T = 1;
init_global();
cin >> T >> T;
while (T--)
{
init_local();
run();
}
#ifdef Redshift_Debug
auto ed = chrono::high_resolution_clock::now();
fprintf(stderr, "%.9lf\n", (ed - st).count() / 1e9);
#endif
}