P15588 [KTSC 2026] 飞扬的松鼠 2 / Flying Squirrel 2

· · 题解

首先我们将 L_i, R_i 进行一些合法化处理:从左往右令 L_i = \max(L_i, L_{i - 1}), R_i = \min(R_i, R_{i - 1} + 1),从右往左令 L_i = \max(L_i, L_{i + 1} - 1), R_i = \min(R_i, R_{i + 1}),再令 R_i = \min(R_i, m)。

考虑 B_i = 0 怎么做。有一个 slope trick 做法,但是对正解帮助不大所以不展开讲。

考虑用另一种方式描述高度在 [L_i, R_i] 中的限制。令 l_i = \min\limits_{R_j \ge i} j, r_i = \min\limits_{L_j \ge i} j。那么高度从 i - 1 \to i 的这一次升高必须在编号为 [l_i, r_i] 中的柱子完成。

于是问题可以变成一个二分图最小权匹配问题:左部点为每一次升高,右部点为所有柱子。左部点 i 向右部点 j \in [l_i, r_i] 连边,边权为 a_j。求一个大小为 H 的匹配,使得边权和最小。

由于所有能被左部点匹配覆盖的柱子集合构成拟阵,所以上述问题可以直接贪心:按 a_i 从小到大逐个考虑柱子,如果加入后满足「加入的所有柱子能被左部点匹配覆盖」,就加入。

为了判定这个条件,可以使用 Hall 定理,即判断每一个已加入的柱子子集 S,设所有和 S 点集有连边的左部点集合(即 [l_i, r_i] \cap S \ne \varnothing 的所有 i)为 N(S),是否都满足 |N(S)| \ge |S|。

可以只判定 S 形成一个连续区间 [x, y] 的情况。此时相当于判定是否 \sum\limits_{i = 1}^H [[l_i, r_i] \cap [x, y] \ne \varnothing] \ge y - x + 1,即 y - x + 1 + \sum\limits_{i = 1}^H [r_i < x] + [l_i > y] \le H。

可以转而求左式的最大值。对于每个 [l_i, r_i],在 l_i 放一个 (,r_i 放一个 ),对于每个已加入的柱子 i,在 i 加入一个 *(位置相同的字符按 (*) 的顺序排列)。即求形如 ) \cdots )\ \ast \cdots \ast\ ( \cdots ( 子序列长度最大值。容易线段树维护。

考虑有 B_i = 0 的情况。可以发现如下关键结论:

那么我们先求出一个蓝柱子数量最小的最优解,然后按 a_i 从小到大依次尝试加入没选的蓝柱子 i,加入之后最长 ) \ast ( 子序列长度一定是 H + 1,我们需要删除一个红柱子,使得删除后最长 ) \ast ( 子序列长度变成 H。

那么也就是求这个蓝柱子左边的最长的 ) \cdots ) \ast \cdots \ast 子序列的 \ast 部分的交,和右边的最长的 \ast \cdots \ast ( \cdots ( 子序列的 \ast 部分的交中,a 最大的红柱子 j。仍然可以用线段树维护。

但是这样求出来的 T_i 不一定是凸的。由于每次求出来的 (i, j) 一定在最终需要交换的交换对集合 D 中,所以我们把所有 a_i - a_j 从小到大排序,得到的就是 T 的差分序列。

时间复杂度 O(n \log n),代码很好写。

:::info[代码]

#include <bits/stdc++.h>
#ifdef LOCAL
#include "grader.cpp"
#endif
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
using ll = long long;
using ull = unsigned long long;
using db = double;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int maxn = 200100;
const ll inf = 0x3f3f3f3f3f3f3f3fLL;

int n, m, f[maxn], g[maxn], L[maxn], R[maxn], b[maxn], ord[maxn];
ll a[maxn];

struct node {
    int c0, c1, c2, c01, c12, c012;
    pll x1, x2, mn;

    node() {
        c0 = c1 = c2 = c01 = c12 = c012 = 0;
        x1 = x2 = mn = pll(inf, 0);
    }
};

struct seg {
    int l, r;
} c[maxn];

inline node operator + (const node &a, const node &b) {
    node res;
    res.c0 = a.c0 + b.c0;
    res.c1 = a.c1 + b.c1;
    res.c2 = a.c2 + b.c2;
    res.c01 = max(a.c01 + b.c1, a.c0 + b.c01);
    res.c12 = max(a.c12 + b.c2, a.c1 + b.c12);
    res.c012 = max({a.c0 + b.c012, a.c01 + b.c12, a.c012 + b.c2});
    res.mn = min(a.mn, b.mn);
    if (a.c0 + b.c01 >= a.c01 + b.c1) {
        res.x1 = b.x1;
    } else {
        res.x1 = min(a.x1, b.mn);
    }
    if (a.c12 + b.c2 >= a.c1 + b.c12) {
        res.x2 = a.x2;
    } else {
        res.x2 = min(b.x2, a.mn);
    }
    return res;
}

namespace SGT {
    node a[maxn << 2];
    int N;

    inline void init() {
        N = (1 << (__lg(n + 1) + 1));
        for (int i = 1; i <= n; ++i) {
            a[N + i].c0 = a[N + i].c01 = f[i];
            a[N + i].c2 = a[N + i].c12 = g[i];
            a[N + i].c012 = max(f[i], g[i]);
        }
        for (int i = N; i; --i) {
            a[i] = a[i << 1] + a[i << 1 | 1];
        }
    }

    inline void update(int x, int y, pll z) {
        x += N;
        a[x].c1 += y;
        a[x].c01 = max(a[x].c0, a[x].c1);
        a[x].c12 = max(a[x].c1, a[x].c2);
        a[x].c012 = max({a[x].c0, a[x].c1, a[x].c2});
        a[x].x1 = a[x].x2 = a[x].mn = z;
        while (x >>= 1) {
            a[x] = a[x << 1] + a[x << 1 | 1];
        }
    }

    inline node query(int l, int r) {
        if (l > r) {
            return node();
        }
        node resl, resr;
        for (l += N - 1, r += N + 1; l ^ r ^ 1; l >>= 1, r >>= 1) {
            if (!(l & 1)) {
                resl = resl + a[l ^ 1];
            }
            if (r & 1) {
                resr = a[r ^ 1] + resr;
            }
        }
        return resl + resr;
    }
}

vector<ll> fly(int _h, vector<int> _a, vector<int> _b, vector<int> _l, vector<int> _r) {
    n = (int)_a.size();
    m = _h;
    for (int i = 1; i <= n; ++i) {
        a[i] = _a[i - 1];
        b[i] = _b[i - 1];
        L[i] = _l[i - 1];
        R[i] = _r[i - 1];
    }
    for (int i = n - 1; ~i; --i) {
        L[i] = max(L[i], L[i + 1] - 1);
        R[i] = min(R[i], R[i + 1]);
    }
    for (int i = 1; i <= n; ++i) {
        L[i] = max(L[i], L[i - 1]);
        R[i] = min(R[i], R[i - 1] + 1);
    }
    if (!(L[n] <= m && m <= R[n])) {
        return vector<ll>(m + 1, -1);
    }
    for (int i = 0; i <= n; ++i) {
        R[i] = min(R[i], m);
        if (L[i] > R[i]) {
            return vector<ll>(m + 1, -1);
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = L[i - 1] + 1; j <= L[i]; ++j) {
            c[j].r = i;
        }
        for (int j = R[i - 1] + 1; j <= R[i]; ++j) {
            c[j].l = i;
        }
    }
    for (int i = L[n] + 1; i <= m; ++i) {
        c[i].r = n;
    }
    for (int i = 1; i <= m; ++i) {
        ++f[c[i].r];
        ++g[c[i].l];
    }
    SGT::init();
    for (int i = 1; i <= n; ++i) {
        ord[i] = i;
    }
    sort(ord + 1, ord + n + 1, [&](const int &i, const int &j) {
        return b[i] < b[j] || (b[i] == b[j] && a[i] < a[j]);
    });
    ll res = 0;
    int cnt = 0, ct = 0;
    vector<int> vc;
    for (int _ = 1; _ <= n; ++_) {
        int i = ord[_];
        SGT::update(i, 1, b[i] ? pll(inf, 0) : pll(-a[i], i));
        if (SGT::a[1].c012 > m) {
            SGT::update(i, -1, pll(inf, 0));
            if (b[i]) {
                vc.pb(i);
            }
        } else {
            ++cnt;
            ct += b[i];
            res += a[i];
        }
    }
    if (cnt != m) {
        return vector<ll>(m + 1, -1);
    }
    vector<ll> ans(m + 1, -1);
    ans[ct] = res;
    vector<ll> ds;
    for (int i : vc) {
        node x = SGT::query(1, i - 1), y = SGT::query(i + 1, n);
        if (min(x.x1, y.x2).fst == inf) {
            continue;
        }
        SGT::update(i, 1, pll(inf, 0));
        pll p = min(x.x1, y.x2);
        SGT::update(p.scd, -1, pll(inf, 0));
        ds.pb(a[i] + p.fst);
    }
    sort(ds.begin(), ds.end());
    for (ll x : ds) {
        ++ct;
        ans[ct] = ans[ct - 1] + x;
    }
    return ans;
}

:::