P12050 三叶虫树 题解

· · 题解

这是一篇吴语题解(北吴临绍小片),本字不清楚的字用同音字打星号表示。

Solution

记黑颜色为 1,白颜色为 0。

不妨得*开始辰光个棋盘看作是全白个,每冒*翻转行搭列个颜色。设 r_i 表示第 i 行有勿有翻转过,c_i 表示第 i 列有勿有翻转过,箇末棋盘高登* (i,j) 颜色就是 r_i\oplus c_j(其中 \oplus 表示异或)。

箇末棋盘个连通块个数是多少咛*?首先,连通块咸般是个矩阵,介廿*显然个。

可以发现一个棋盘高登*个连通块对应一个行个极长连续段搭列个极长连续段。证明:

因此,设行搭列个极长连续段个数分别是 x,y,棋盘个连通块个数便是 x\times y。

考虑维护行搭列个极长连续段。序列个极长连续段是相邻两个颜色不同的位置数加 1,可以简单维护。

回到开头来,一开始个棋盘是黑白相间个,何则介办咛*?可以发现,介实际高登*便是初始辰光翻转了偶数行搭偶数列。

综上,介道题目就介做好了,复杂度线性。

代码

#include <bits/stdc++.h>
#define REP(i, l, r) for (int i = (l); i <= (r); ++ i)
#define DEP(i, r, l) for (int i = (r); i >= (l); -- i)
#define fi first
#define se second
#define pb emplace_back
#define mems(x, v) memset((x), (v), sizeof(x))
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
using namespace std;
namespace Milkcat {
    typedef long long LL;
    typedef pair<LL, LL> pii;
    const int N = 1e6 + 5;
    struct solver {
        int ct, n, a[N]; // ct 表示极长个连续段个数
        void init(int x) { n = x, ct = 1; }
        void flip(int x) {
            if (x > 1) ct -= (a[x] != a[x - 1]);
            if (x < n) ct -= (a[x] != a[x + 1]);
            a[x] ^= 1;
            if (x > 1) ct += (a[x] != a[x - 1]);
            if (x < n) ct += (a[x] != a[x + 1]);
        } // 翻转一个位置
    } A, B;
    int n, q, o, x;
    int main() {
        cin >> n >> q, A.init(n), B.init(n);
        REP(i, 1, n)
            if (!(i & 1)) A.flip(i), B.flip(i);
        REP(te, 1, q) {
            cin >> o >> x;
            if (o == 1) A.flip(x);
            if (o == 2) B.flip(x);
            cout << 1LL * A.ct * B.ct << '\n';
        }
        return 0;
    }
}
int main() {
    cin.tie(0)->sync_with_stdio(0);
    int T = 1;
    while (T --) Milkcat::main();
    return 0;
}