题解:P15456 [JOI 2026 SemiFinal] 奇妙的机器 / Strange Machine

· · 题解

声明:这篇题解不包含对通过打表得到的结论的严谨证明,因为我不会。

一些定义

为了方便,设白色为 1,黑色为 0,将瓷砖二进制编码为 2 \times 正面颜色 + 反面颜色。

用 l \oplus r 表示题目中将两张瓷砖合成一张的操作,相当于交换 l 的两个二进制位再异或 r。

可以得到 \oplus 的运算表如下:

\oplus 0 1 2 3
0 0 1 2 3
1 2 3 0 1
2 1 0 3 2
3 3 2 1 0

询问是否能通过合成相邻瓷砖使得区间内正面为白色的瓷砖的总数恰好是 m,即 2, 3 的总数恰好是 m。

一、最大值

:::info[打表一(没错还有二)] ```cpp #include <bits/stdc++.h> int calc(int x, int y) { // 合并 x 和 y if (x == 0 || x == 3) { return y ^ x; } else { return y ^ (3 - x); } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::set<std::string> set; std::function<void(std::string)> DFS = [&](std::string s) { if (set.count(s)) { return; } set.emplace(s); for (int i = 0; i + 1 < (int)s.size(); i++) { auto t = s; t.erase(i, 2); t.insert(t.begin() + i, (char)(calc(s[i] - '0', s[i + 1] - '0') + '0')); DFS(t); } }; std::string s; std::cin >> s; DFS(s); std::set<int> valid; // 所有 2, 3 总数的可行值 for (auto i : set) { valid.emplace(std::count(i.begin(), i.end(), '2') + std::count(i.begin(), i.end(), '3')); } for (auto i : valid) { std::cerr << i << '\n'; } return 0; } ``` ::: 这启发我们考虑可行的最大值,**在大部分情况下**,有解等价于 $m \le$ 这个最大值。 观察 $\oplus$ 操作对 $2, 3$ 总数的改变量,用蓝色,橙色,绿色和红色分别表示 ${\color{blue}-2}, {\color{orange}-1}, {\color{green}0}$ 和 $\color{red}+1$: | $\oplus$ | $0$ | $1$ | $2$ | $3$ | | :-: | :-: | :-: | :-: | :-: | | $0$ | $\color{green} 0$ | $\color{green} 1$ | $\color{green} 2$ | $\color{green} 3$ | | $1$ | $\color{red} 2$ | $\color{red} 3$ | $\color{orange} 0$ | $\color{orange} 1$ | | $2$ | $\color{orange} 1$ | $\color{orange} 0$ | $\color{orange} 3$ | $\color{orange} 2$ | | $3$ | $\color{green} 3$ | $\color{green} 2$ | $\color{blue} 1$ | $\color{blue} 0$ | 只有 $1 \oplus 0$ 和 $1 \oplus 1$ 会使 $2, 3$ 的总数 $\color{red}+1$,因此我们只要一直做这两种操作就可以使 $2, 3$ 的总数达到最大值,且初始值到最大值之间的值都可以取到。 我们可以使用线段树维护如下结构体以计算区间 $2, 3$ 总数的最大值: :::info[结构体]{open} ```cpp struct Node { int max; // 区间 2, 3 总数的最大值 bool head0; // 区间左边是否为若干(可以为零)个 1 接一个 0 bool hOdd, tOdd, all1; // 区间左边 1 的奇偶性,区间右边 1 的奇偶性和区间是否全为 1 void init(int v) { max = v > 1; head0 = !v; hOdd = tOdd = all1 = v == 1; } friend Node operator + (Node a, Node b) { Node c; c.max = a.max + b.max; c.max += a.tOdd & (b.hOdd ^ b.head0); // 最大值 +1 c.head0 = a.head0 | (a.all1 & b.head0); c.hOdd = a.hOdd ^ (a.all1 ? b.hOdd : 0); c.tOdd = b.tOdd ^ (b.all1 ? a.tOdd : 0); c.all1 = a.all1 & b.all1; return c; } }; ``` ::: 再使用树状数组维护区间里每个数的数量,我们就可以求出可达的最大值 $max$ 以及区间内初始 $2, 3$ 的总数 $now$,根据前面的结论可以得到:若 $m > max$,无解;若 $m \in [now, max]$,有解。接下来只需讨论 $m < now$ 的情况。 ## 二、奇偶性 继续观察表格,我们可以发现若以 $0, 3$ 为左值进行 $\oplus$ 运算,改变量一定是偶数($\color{blue}-2$ 或 $\color{green}0$);否则以 $1, 2$ 为左值进行 $\oplus$ 运算,改变量一定是奇数($\color{orange}-1$ 或 $\color{red}+1$)。 如果区间内不存在能改变奇偶性的操作,即除了最右边的数外都 $\in \{0, 3\}$,那么有解当且仅当 $m$ 与 $now$ 的奇偶性相同。证明:通过 $0 \oplus 3 = 3 \oplus 0 = 3$ 可以消除 $3$ 左右的 $0$,那么只要 $now \ge 2$ 就可以通过 $3 \oplus 3 = 0$ 使其 $\color{blue}-2$。有一种特殊情况是 $now = 2$ 且最右边的数是 $2$,在这种情况下,通过 $3 \oplus 2 = 1$ 仍然可以使 $now \color{blue}-2$。 否则区间内存在能改变奇偶性的操作,此时一定有解吗?答案是不一定。 ## 三、$m = 0

在区间内存在能改变奇偶性的操作时,挖掘性质变得十分困难,打表启动!

程序读入固定的 n, m,输出所有长为 n 的、经上述判断后仍无解的序列。尝试若干组 n, m 后,可以发现程序只在 m = 0 时有输出,这说明经上述判断后,若 m \neq 0 一定有解。接下来只需讨论 m = 0 的情况。

数 0 的存在使得较大 n 的情况包含了所有较小 n 的情况,因此我们不用观察太多的 n,取几个就行。观察 n = 5(不要太小或太大就行),m = 0 的输出:

:::info[n = 5, m = 0 的输出]{open}

00022
33022
30322
03322
30012
03012
00312
33312
00023
33023
30323
03323
30013
03013
00313
33313

:::

结合几个 n = 其他数的输出,容易发现输出中的序列满足且必须满足以下规律:

  1. 前 n - 2 个数 \in \{0, 3\}(不用考虑 n = 1,在奇偶性部分一定会被判掉);
  2. 第 n - 1 个数 \in \{1, 2\};
  3. 第 n 个数 \in \{2, 3\};

这些规律在打表程序的输出中是充要的,在 n 更大的情况下肯定也不例外。

:::info[打表二]

#include <bits/stdc++.h>

int calc(int x, int y) {
    if (x == 0 || x == 3) {
        return y ^ x;
    } else {
        return y ^ (3 - x);
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::set<std::string> set;

    std::function<void(std::string)> DFS = [&](std::string s) {
        if (set.count(s)) {
            return;
        }

        set.emplace(s);

        for (int i = 0; i + 1 < (int)s.size(); i++) {
            auto t = s;
            t.erase(i, 2);
            t.insert(t.begin() + i, (char)(calc(s[i] - '0', s[i + 1] - '0') + '0'));
            DFS(t);
        }
    };

    int n, m;
    std::cin >> n >> m;

    for (int i = 0; i < (1 << (2 * n)); i++) {
        std::string s;

        for (int j = 0; j < n; j++) {
            s += (char)('0' + (((i >> (j << 1)) & 1) << 1) + ((i >> (j << 1 | 1)) & 1));
        }

        set.clear();
        DFS(s);

        std::set<int> valid;

        for (auto j : set) {
            valid.emplace(std::count(j.begin(), j.end(), '2') + 
                          std::count(j.begin(), j.end(), '3'));
        }

        if (!valid.count(m)) { // 无解
            if (std::count(s.begin(), s.end(), '2') + 
                std::count(s.begin(), s.end(), '3') > m) { // m < now
                if (std::count(s.begin(), prev(s.end()), '1') + 
                    std::count(s.begin(), prev(s.end()), '2')) { // 存在能改变奇偶性的操作
                    std::cerr << s << '\n';
                }
            }
        }
    }

    return 0;
}

:::

我们解决了 m = 0,至此所有情况都讨论完毕了。

四、总结

综上所述,算法流程如下:

  1. 用线段树和树状数组计算出 max 和 now 进行初步判断,若 m < now 就进行下一步;
  2. 用树状数组判断能否改变奇偶性,若可以就进行下一步;
  3. 使用通过打表得到的结论判断即可。

总时间复杂度 O(Q \log N)。

:::success[AC 代码]

#include <bits/stdc++.h>

constexpr int N = 3e5 + 1;

int calc(char a, char b) { // 二进制编码
    return (a == 'W') << 1 | (b == 'W');
}

struct Node {
    int max;
    bool head0;
    bool hOdd, tOdd, all1;

    void init(int v) {
        max = v > 1;
        head0 = !v;
        hOdd = tOdd = all1 = v == 1;
    }

    friend Node operator + (Node a, Node b) {
        Node c;
        c.max = a.max + b.max;
        c.max += a.tOdd & (b.hOdd ^ b.head0);
        c.head0 = a.head0 | (a.all1 & b.head0);
        c.hOdd = a.hOdd ^ (a.all1 ? b.hOdd : 0);
        c.tOdd = b.tOdd ^ (b.all1 ? a.tOdd : 0);
        c.all1 = a.all1 & b.all1;
        return c;
    }
};

namespace SegTree {
    constexpr int SN = N << 2;
    int n;
    Node sum[SN];

    void build(std::string& s, std::string& t, int x = 1, int l = 0, int r = n) {
        if (r - l == 1) {
            sum[x].init(calc(s[l], t[l]));
            return;
        }

        int mid = (l + r) >> 1;
        build(s, t, x << 1, l, mid);
        build(s, t, x << 1 | 1, mid, r);
        sum[x] = sum[x << 1] + sum[x << 1 | 1];
    }

    void init(int _n, std::string& s, std::string& t) {
        n = _n;
        build(s, t);
    }

    void modify(int p, int v, int x = 1, int l = 0, int r = n) {
        if (r - l == 1) {
            sum[x].init(v);
            return;
        }

        int mid = (l + r) >> 1;
        mid > p ? modify(p, v, x << 1, l, mid) : 
                  modify(p, v, x << 1 | 1, mid, r);
        sum[x] = sum[x << 1] + sum[x << 1 | 1];
    }

    Node query(int L, int R, int x = 1, int l = 0, int r = n) {
        if (l >= L && r <= R) {
            return sum[x];
        }

        int mid = (l + r) >> 1;

        if (mid > L && mid < R) {
            return query(L, R, x << 1, l, mid) + 
                   query(L, R, x << 1 | 1, mid, r);
        }

        return mid > L ? query(L, R, x << 1, l, mid) : 
                         query(L, R, x << 1 | 1, mid, r);
    }
}

namespace BitTree {
    int n;
    std::array<int, 4> sum[N];

    void build(std::string& s, std::string& t) {
        for (int i = 1; i <= n; i++) {
            sum[i][calc(s[i - 1], t[i - 1])]++;
            int fa = i + (i & -i);

            if (fa <= n) {
                sum[fa][0] += sum[i][0];
                sum[fa][1] += sum[i][1];
                sum[fa][2] += sum[i][2];
                sum[fa][3] += sum[i][3];
            }
        }
    }

    void init(int _n, std::string& s, std::string& t) {
        n = _n;
        build(s, t);
    }

    void change(int p, int u, int v) {
        for (int i = p + 1; i <= n; i += i & -i) {
            sum[i][u]--;
            sum[i][v]++;
        }
    }

    int query(int r, int mask) {
        int res = 0;

        for (int i = r; i > 0; i -= (i & -i)) {
            for (int j = 0; j < 4; j++) {
                if (mask >> j & 1) {
                    res += sum[i][j];
                }
            }
        }

        return res;
    }

    int query(int l, int r, int mask) {
        return query(r, mask) - query(l, mask);
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n;
    std::cin >> n;

    std::string s, t;
    std::cin >> s >> t;

    SegTree::init(n, s, t);
    BitTree::init(n, s, t);

    int q;
    std::cin >> q;

    for (int i = 0; i < q; i++) {
        int op;
        std::cin >> op;

        if (op == 1) {
            int x;
            char a, b;
            std::cin >> x >> a >> b;
            x--;
            SegTree::modify(x, calc(a, b));
            BitTree::change(x, calc(s[x], t[x]), calc(a, b));
            std::tie(s[x], t[x]) = std::tie(a, b);
        } else {
            int l, r, m;
            std::cin >> l >> r >> m;
            l--;

            if (SegTree::query(l, r).max < m) { // 判断 max
                std::cout << "No\n";
                continue;
            }

            int now = BitTree::query(l, r, 12); // 2^2 + 2^3 = 12

            if (now <= m) { // 判断 now
                std::cout << "Yes\n";
                continue;
            }

            int cnt = BitTree::query(l, r - 1, 6); // 2^1 + 2^2 = 6

            if (!cnt) { // 不能改变奇偶性
                std::cout << ((now & 1) == (m & 1) ? "Yes\n" : "No\n");
                continue;
            }

            if (m == 0) { // m = 0
                int x = calc(s[r - 2], t[r - 2]);
                int y = calc(s[r - 1], t[r - 1]);

                if (cnt == 1 && 
                    (x == 1 || x == 2) && 
                    (y == 2 || y == 3) && 
                    !(now & 1)) { // 结论判断
                    std::cout << "No\n";
                } else {
                    std::cout << "Yes\n";
                }
            } else {
                std::cout << "Yes\n";
            }
        }
    }

    return 0;
}

:::