跳到正文

题解

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

Elup约 22 分钟
原文

这是 2026年2月28日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年2月28日
收录
2026年2月28日
更新
2026年11月16日
篇幅
约 22 分钟
题目
P15456

版本

  1. v2当前
  2. v1

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

一些定义

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

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

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

⊕\oplus00112233
0000112233
1122330011
2211003322
3333221100

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

一、最大值

⊕\oplus 运算没有什么好的性质,考虑打表找规律。针对题目的询问写一个暴力,随机手输 0∼30 \sim 3 组成的序列,可以发现大部分情况下得到的结果总是一段从 00 开始的连续数列。

打表一(没错还有二)
C++
#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≤m \le 这个最大值。

观察 ⊕\oplus 操作对 2,32, 3 总数的改变量,用蓝色,橙色,绿色和红色分别表示 −2,−1,0{\color{blue}-2}, {\color{orange}-1}, {\color{green}0} 和 +1\color{red}+1:

⊕\oplus00112233
000\color{green} 01\color{green} 12\color{green} 23\color{green} 3
112\color{red} 23\color{red} 30\color{orange} 01\color{orange} 1
221\color{orange} 10\color{orange} 03\color{orange} 32\color{orange} 2
333\color{green} 32\color{green} 21\color{blue} 10\color{blue} 0

只有 1⊕01 \oplus 0 和 1⊕11 \oplus 1 会使 2,32, 3 的总数 +1\color{red}+1,因此我们只要一直做这两种操作就可以使 2,32, 3 的总数达到最大值,且初始值到最大值之间的值都可以取到。

我们可以使用线段树维护如下结构体以计算区间 2,32, 3 总数的最大值:

结构体
C++
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;
    }
};

再使用树状数组维护区间里每个数的数量,我们就可以求出可达的最大值 maxmax 以及区间内初始 2,32, 3 的总数 nownow,根据前面的结论可以得到:若 m>maxm > max,无解;若 m∈[now,max]m \in [now, max],有解。接下来只需讨论 m<nowm < now 的情况。

二、奇偶性

继续观察表格,我们可以发现若以 0,30, 3 为左值进行 ⊕\oplus 运算,改变量一定是偶数(−2\color{blue}-2 或 0\color{green}0);否则以 1,21, 2 为左值进行 ⊕\oplus 运算,改变量一定是奇数(−1\color{orange}-1 或 +1\color{red}+1)。

如果区间内不存在能改变奇偶性的操作,即除了最右边的数外都 ∈{0,3}\in \{0, 3\},那么有解当且仅当 mm 与 nownow 的奇偶性相同。证明:通过 0⊕3=3⊕0=30 \oplus 3 = 3 \oplus 0 = 3 可以消除 33 左右的 00,那么只要 now≥2now \ge 2 就可以通过 3⊕3=03 \oplus 3 = 0 使其 −2\color{blue}-2。有一种特殊情况是 now=2now = 2 且最右边的数是 22,在这种情况下,通过 3⊕2=13 \oplus 2 = 1 仍然可以使 now−2now \color{blue}-2。

否则区间内存在能改变奇偶性的操作,此时一定有解吗?答案是不一定。

三、m=0m = 0

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

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

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

n=5,m=0n = 5, m = 0 的输出
代码
00022
33022
30322
03322
30012
03012
00312
33312
00023
33023
30323
03323
30013
03013
00313
33313

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

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

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

打表二
C++
#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=0m = 0,至此所有情况都讨论完毕了。

四、总结

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

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

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

AC 代码
C++
#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;
}