题解:CF1109F Sasha and Algorithm of Silence's Sounds

· · 题解

一语点醒梦中人。

题目大意:一个网格图只保留值在 [l, r] 的格子可以构成树形结构的 (l, r) 对个数。

扫描线,枚举 r,维护一个最小的 l 使得区间 [l, r] 保留在图中是一棵树,记 p_r = l。不难发现 p 具有单调性,故实现上我们用双指针 + LCT 维护。

点边容斥,树的判定是 V - E = 1(V 是点数、E 是边数)。而保留 [l, r] 的所有点,有 V = r - l + 1,整理一下式子可以得到 E + l = r。

类似经典题 CF526F,LCT 维护的是生成树,所以 E_{\max} + l = r,而在 l = r 的时候肯定可以取到这个 \max,所以说我们只需要再次扫描线并动态统计 E + l 的最大值及其个数即可。而这个可以上线段树。

令 S = nm,时间复杂度 \mathcal{O}(S \log S),足以通过此题。

:::info[code]

#include <bits/stdc++.h>
// #define fastio
#define int long long
#define bw(x) (1 << (x))
#define eb emplace_back
#define pii pair<int, int>
#define inf (0x3f3f3f3f3f3f3f3f)
#define F(x, v) for (auto x : (v))
#define ALL(x) (x).begin(), (x).end()
#define L(i, a, b) for (register int i = (a); i <= (b); ++i)
#define R(i, a, b) for (register int i = (a); i >= (b); --i)
#define FRE(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout)
#define debug(a) cerr << "\033[32m[DEBUG] " << #a << " = " << (a) << " at line " << __LINE__ << "\033[0m\n"
using namespace std; bool bgmem;
#ifdef fastio
struct IO {
    #define ion bw(20)
    char i[ion], o[ion], *icl = i, *icr = i, *oc = o;
    char gc() { return (icl == icr && (icr = (icl = i) + fread(i, 1, ion, stdin), icl == icr)) ? EOF : *icl++; }
    void pc(char c) { if (oc - o == ion) fwrite(o, 1, ion, stdout), oc = o; *oc++ = c; }
    void rd(auto &x) { char c; int f = 1; x = 0; while (c = gc(), c < '0' || c > '9') if (c == '-') f = -1; while ('0' <= c && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = gc(); x *= f; } 
    void pr(auto x) { int a[64], p = 0; if (x < 0) x = -x, pc('-'); do a[p++] = x % 10; while (x /= 10); while (p--) pc(a[p] + '0'); } 
    IO& operator>>(char &c) { return c = gc(), *this; }
    IO& operator<<(char c) { return pc(c), *this; }
    IO& operator<<(const char *c) { while (*c) pc(*c++); return *this; }
    IO& operator>>(auto &x) { return rd(x), *this; }
    IO& operator<<(auto x) { return pr(x), *this; }
} io;
#define cin io
#define cout io
#endif
template < const int P > class modint { public:
    int x;
    modint(int _ = 0) { x = (_ % P + P) % P; }
    explicit operator int() { return x; }
    friend modint operator+(modint x, modint y) { return x.x + y.x < P ? x.x + y.x : x.x + y.x - P; }
    friend modint operator-(modint x, modint y) { return x.x - y.x < 0 ? x.x - y.x + P : x.x - y.x; }
    friend modint operator*(modint x, modint y) { return modint((__int128)(x.x * y.x)); }
    modint& operator+=(modint x) { return (*this) = (*this) + x; }
    modint& operator-=(modint x) { return (*this) = (*this) - x; }
    modint& operator*=(modint x) { return (*this) = (*this) * x; }
};
inline int cmax(int& x, int c) { return x = max(x, c); }
inline int cmin(int& x, int c) { return x = min(x, c); }
int tes = 1, cas;
namespace qwq {
    const int N = 2e5 + 5, M = 1005;
    int n, m, a[M][M], mx[N], my[N], pre[N], d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    namespace lct {  // lct 板子
        int son[N][2], siz[N], fa[N], val[N], tag[N], tot = 0;
        #define dir(u) (son[fa[u]][1] == u)
        #define irt(u) (fa[u] == 0 || (son[fa[u]][0] != u && son[fa[u]][1] != u))
        void modify(int u) { swap(son[u][0], son[u][1]), tag[u] ^= 1; }
        void up(int u) { siz[u] = siz[son[u][0]] + siz[son[u][1]] + 1; }
        void down(int u) { if (tag[u]) modify(son[u][0]), modify(son[u][1]), tag[u] = 0; }
        void all(int u) { if (!irt(u)) all(fa[u]); down(u); }
        void rotate(int u) {
            int v = fa[u], w = fa[v], d = dir(u);
            if (!irt(v)) son[w][dir(v)] = u;
            fa[u] = w, son[v][d] = son[u][d ^ 1];
            if (son[u][d ^ 1]) fa[son[u][d ^ 1]] = v;
            son[u][d ^ 1] = v, fa[v] = u, up(v), up(u);
        } 
        void splay(int u) { all(u); for (int v = fa[u]; !irt(u); rotate(u), v = fa[u]) if (!irt(v)) rotate(dir(u) == dir(v) ? v : u); } 
        void access(int u) { for (int v = 0; u; v = u, u = fa[u]) splay(u), son[u][1] = v, up(u); } 
        void mak(int u) { access(u), splay(u), modify(u); } 
        int frt(int u) { access(u), splay(u); while (son[u][0]) u = son[u][0], down(u); return splay(u), u; } 
        void split(int u, int v) { mak(u), access(v), splay(v); } 
        void link(int u, int v) { mak(u); if (frt(v) != u) fa[u] = v; } 
        void cut(int u, int v) { mak(u); if (frt(v) == u && fa[v] == u && !son[v][0]) son[u][1] = fa[v] = 0, up(u); }
    } namespace sgt {  // 线段树板子
        #define ls (u << 1)
        #define rs (ls | 1)
        #define mid ((L + R) >> 1)
        // 结构体 V 存储最大值(v)与最大值个数(c),维护 E - l
        struct V { int v, c; V() {} V(int _, int __) { v = _, c = __; } } tr[N << 2]; int tg[N << 2];
        V operator+(V x, V y) { return V(max(x.v, y.v), x.v == y.v ? x.c + y.c : (x.v > y.v ? x.c : y.c)); }
        void up(int u) { tr[u] = tr[ls] + tr[rs]; }
        void mo(int u, int v) { tr[u].v += v, tg[u] += v; }
        void down(int u) { mo(ls, tg[u]), mo(rs, tg[u]), tg[u] = 0; }
        void bld(int u, int L, int R) {
            if (L == R) return tr[u] = V(L, 1), void();
            bld(ls, L, mid), bld(rs, mid + 1, R), up(u);
        } void upd(int u, int L, int R, int l, int r, int v) {
            if (l > R || r < L) return;
            if (l <= L && R <= r) return mo(u, v);
            down(u), upd(ls, L, mid, l, r, v), upd(rs, mid + 1, R, l, r, v), up(u);
        } V que(int u, int L, int R, int l, int r) {
            if (l > R || r < L) return V(-inf, 0);
            if (l <= L && R <= r) return tr[u];
            return down(u), que(ls, L, mid, l, r) + que(rs, mid + 1, R, l, r);
        }
    }
    void init() {}
    void clear() {}
    void solve() {
        cin >> n >> m;
        L(i, 1, n) L(j, 1, m) cin >> a[i][j], mx[a[i][j]] = i, my[a[i][j]] = j;
        int l = 1, ans = 0; L(r, 1, n * m) {  // 第一次扫描线
            int x = mx[r], y = my[r];
            L(O, 0, 3) {  // 加入结点时尝试与周围 4 个点连边
                int tx = x + d[O][0], ty = y + d[O][1];
                if (tx < 1 || ty < 1 || tx > n || ty > m || a[tx][ty] < l || a[tx][ty] > r) continue;
                for (; l <= r && lct::frt(r) == lct::frt(a[tx][ty]); ++l) {  // 双指针移动 l 直到 r 与该结点不再是一个环
                    //  均摊是 O(n)
                    L(P, 0, 3) {  // 因为双指针扫过,所以要断开 l 与生成树的连接
                        int i = mx[l] + d[P][0], j = my[l] + d[P][1];
                        if (i < 1 || j < 1 || i > n || j > m || a[i][j] < l || a[i][j] >= r) continue;
                        lct::cut(a[i][j], l);
                    }
                }
                if (l <= a[tx][ty]) lct::link(a[tx][ty], r);
            }
            pre[r] = l;  // 记录数组 p
        }
        sgt::bld(1, 1, n * m);  // 建树,初始化 tr[i] = i
        L(i, 1, n * m) {  // 第二次扫描线
            int x = mx[i], y = my[i];
            L(O, 0, 3) {  // 加入结点 i 连向 [1, i - 1] 所有结点的边
                int tx = x + d[O][0], ty = y + d[O][1];
                if (tx < 1 || ty < 1 || tx > n || ty > m || a[tx][ty] > a[x][y]) continue;
                sgt::upd(1, 1, n * m, 1, a[tx][ty], 1);
            }
            auto [v, c] = sgt::que(1, 1, n * m, pre[i], i);
            if (i == v) ans += c;  // 统计答案
        }
        cout << ans << "\n";
    }
} bool edmem; signed main() {
    // FRE("", "");
#ifndef fastio
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(6);
#endif
    // cin >> tes;
    qwq::init(); while (++cas <= tes) { qwq::clear(), qwq::solve(); }
#ifdef fastio
    fwrite(io.o, 1, io.oc - io.o, stdout);
#endif
    cerr << "time  : " << (double)clock() / CLOCKS_PER_SEC * 1000 << "ms\n";
    cerr << "memory: " << fabs(&edmem - &bgmem) / 1024 / 1024 << "mb\n";
    return 0;
}
// I will never love You Know Who again. =(

:::