题解:P11706 「KTSC 2020 R1」穿越

· · 题解

看到 Subtask #1,容易想到动态规划。令 f_{i,j} 代表抵达第 i 列第 j 行的最低代价:

注意到 f_i 仅与 f_{i-1} 有关,因此可以滚动数组压维,状态变为 f_j 代表抵达第 j 行的最小代价,初始 f=0 答案 \min f,转移变为对每个 i\in[0,n) 依次执行:

\begin{aligned} f_j&\xleftarrow+\text B&(j\in[l_i,r_i])\\ f_j&\xleftarrow{\min}\text A+\min f \end{aligned}

:::info[code]

#include <bits/stdc++.h>
#include <bits/extc++.h>

namespace {
    std::size_t n, m;
    std::vector<std::size_t> P, Q;
}

void init(int N, int M, std::vector<int> Y1, std::vector<int> Y2) {
    n = static_cast<std::size_t>(N);
    m = static_cast<std::size_t>(M);

    P.resize(n);
    Q.resize(n);
    for (std::size_t i = 0; i < n; ++i) {
        P[i] = static_cast<std::size_t>(Y1[i]);
        Q[i] = static_cast<std::size_t>(Y2[i]);
    }
}

long long minimize(int A, int B) {
    std::vector<long long> cost(m);
    for (std::size_t i = 0; i < n; ++i) {
        for (std::size_t j = P[i]; j <= Q[i]; ++j)
            cost[j] += B;
        auto minimum = *std::min_element(cost.begin(), cost.end());
        for (std::size_t j = 0; j < m; ++j)
            cost[j] = std::min(cost[j], minimum + A);
    }
    return *std::min_element(cost.begin(), cost.end());
}

:::

以上做法时间复杂度 \mathcal O(n+qnm),然后离散化将 m 一维的状态数压缩至 \mathcal O(n^2),时间复杂度优化至 \mathcal O(n\log n+qn^2)。考虑优化转移,观察转移形式,发现涉及三种操作:

以上操作可以用线段树维护。单次转移时间复杂度降低至 \mathcal O(\log n),总体时间复杂度优化至 \mathcal O(n\log n+qn\log n)。

:::info[code]

#include <bits/stdc++.h>
#include <bits/extc++.h>

namespace {
    std::size_t n, m, s, h;
    std::vector<std::size_t> P, Q;
}

void init(int N, int M, std::vector<int> Y1, std::vector<int> Y2) {
    n = static_cast<std::size_t>(N);
    m = static_cast<std::size_t>(M);

    std::vector<int> Y{0, M};
    Y.reserve(Y.size() + n * 2);
    for (std::size_t i = 0; i < n; ++i) {
        Y.push_back(Y1[i] + 0);
        Y.push_back(Y2[i] + 1);
    }

    std::sort(Y.begin(), Y.end());
    m = static_cast<std::size_t>(Y.erase(std::unique(Y.begin(), Y.end()), Y.end()) - Y.begin() - 1);

    P.resize(n);
    Q.resize(n);
    for (std::size_t i = 0; i < n; ++i) {
        P[i] = static_cast<std::size_t>(std::lower_bound(Y.begin(), Y.end(), Y1[i] + 0) - Y.begin() - 0);
        Q[i] = static_cast<std::size_t>(std::lower_bound(Y.begin(), Y.end(), Y2[i] + 1) - Y.begin() - 1);
    }
}

long long minimize(int A, int B) {
    for (s = 1, h = 0; s < m; s <<= 1, ++h);
    std::vector<long long> minimum(s << 1, INT64_MAX);
    std::vector<long long> cmintag(s << 1, INT64_MAX);
    std::vector<long long> plustag(s << 1, 0);

    auto apply = [&](std::size_t i, long long cmin, long long plus) {
        minimum[i] = (minimum[i] == INT64_MAX ? minimum[i] : std::min(minimum[i] + plus, cmin));
        cmintag[i] = (cmintag[i] == INT64_MAX ? cmin : std::min(cmintag[i] + plus, cmin));
        plustag[i] = (plustag[i] == INT64_MAX ? plustag[i] : plustag[i] + plus);
    };

    auto modify = [&](std::size_t u, std::size_t v, long long cmin, long long plus) {
        u += s;
        v += s;
        for (std::size_t i = h; i >= 1; --i) {
            auto p = u >> i, q = v >> i;
            if ((true) && (cmintag[p] != INT64_MAX || plustag[p] != 0)) { apply(p << 1, cmintag[p], plustag[p]); apply(p << 1 | 1, cmintag[p], plustag[p]); cmintag[p] = INT64_MAX; plustag[p] = 0; }
            if (p != q && (cmintag[q] != INT64_MAX || plustag[q] != 0)) { apply(q << 1, cmintag[q], plustag[q]); apply(q << 1 | 1, cmintag[q], plustag[q]); cmintag[q] = INT64_MAX; plustag[q] = 0; }
        }
        for (std::size_t x = u, y = v; x <= y; x >>= 1, y >>= 1) {
            if (+x & 1) apply(x++, cmin, plus);
            if (~y & 1) apply(y--, cmin, plus);
        }
        for (std::size_t i = 1; i <= h; ++i) {
            auto p = u >> i, q = v >> i;
            if ((true)) {
                minimum[p] = std::min(minimum[p << 1], minimum[p << 1 | 1]);
                if (minimum[p] != INT64_MAX) minimum[p] = std::min(minimum[p] + plustag[p], cmintag[p]);
            }
            if (p != q) {
                minimum[q] = std::min(minimum[q << 1], minimum[q << 1 | 1]);
                if (minimum[q] != INT64_MAX) minimum[q] = std::min(minimum[q] + plustag[q], cmintag[q]);
            }
        }
    };

    std::fill_n(minimum.data() + s, m, 0);
    for (auto i = s; i--; minimum[i] = std::min(minimum[i << 1], minimum[i << 1 | 1]));

    for (std::size_t i = 0; i < n; ++i) {
        modify(P[i], Q[i], INT64_MAX, B);
        modify(0, m - 1, minimum[1] + A, 0);
    }
    return minimum[1];
}

:::

一件显然的事情是:所有穿过隧道的方案,都由若干次操作 A 和若干次操作 B 组成。设某一个方案有 x_i 次操作 A 和 y_i 次操作 B 组成,则这个方案的代价即为 \text C=\text Ax_i+\text By_i。

将每个方案的 (x_i,y_i) 看作平面直角坐标系上的点,题目所求:给定 \text A,\text B,求最小的 \text C 使得存在一个方案 (x_i,y_i) 满足 \text C=\text Ax_i+\text By_i。设一条直线 l:\text C=\text Ax+\text By,则题目所求可以转化为:求最小的 \text C,使得直线 l 穿过某个方案对应的点。

题目所求还可以进一步转化:给定 \text A,\text B,设直线 l:\text C=\text Ax+\text By,求使得直线 l 与所有方案对应的点构成的凸多边形相切的 \text C。由于 \text A,\text B\ge 0,因此直线形如贯穿左上右下,所以若直线与凸多边形相切,只可能是在凸多边形的左下部分,称其为目标凸包。

换句话说,只有目标凸包上的点对应的方案,才可能是最优方案。由于“凸包点数 \mathcal O(n^{\frac 23})(见下文证明)”,如果我们得到了目标凸包,就可以遍历凸包上的每一个顶点 (x,y),取最小的 \text C=\text Ax+\text By 作为答案,以 \mathcal O(n^{\frac 23}) 的时间复杂度回答单次询问。

结论:整点凸包,两维上界均为 n,点数量级为 \mathcal O(n^{\frac23})。 》 证明:不妨认为对整点凸包上相邻两点,其连线上不存在其他格点

设相邻两点为 (x_i,y_i),(x_{i+1},y_{i+1}),记 |\varDelta x_i|=|x_{i+1}-x_i|,|\varDelta y_i|=|y_{i+1}-y_i|,s_i=|\varDelta x_i|+|\varDelta y_i| 则上述条件等价于 \gcd(|\varDelta x_i|,|\varDelta y_i|)=1。

同时,由于两维上界为 n,显然有 \sum|\varDelta x_i|,\sum|\varDelta y_i|\le 2n,即有 \sum s_i\le 4n。为了定量描述,我们设 f(n) 表示 s_i\le n 时,满足条件的线段 (x_i,y_i),(x_{i+1},y_{i+1}) 数量。

利用莫比乌斯反演可得 f(n)\approx\sum\limits_{i=1}^n\mu(i)\lfloor\frac ni\rfloor^2,放缩 \mu(i)\le 1,由 \sum\limits_{i=1}^{\infty}\frac 1{i^2}\approx\frac{\pi^2}6,可得 f(n)\approx n^2。

令 s_i 代表使 f(k)\ge i 的最小的 k,有 f(s_i)\approx i,即 s_i\approx\sqrt i。对 s_i 求和,设共有 c 个线段,有 \sum\limits_{i=1}^c s_i\approx\sum\limits_{i=1}^c\sqrt i\approx \int_0^c\sqrt x\text{dx}=c^{\frac 32}\le 4n。

因此,有 c\approx O(n^{\frac 23}),证毕。

现在我们只需要求出目标凸包上的每个顶点即可。回想一下前面利用线段树优化动态规划实现的 minimize(A, B) 在当前场景下的意义:以单次 \mathcal O(n\log n) 的时间复杂度求解最小的 \text C 使得直线 \text C=\text Ax+\text By 与目标凸包相切,基于此原理稍作修改便可得到切点,设求切点为 cut(A, B)。

首先我们可以求出目标凸包最左侧和最下侧的顶点,分别是 cut(1, 0)(\text C=x,垂直于 x 轴的一条直线)和 cut(0, 1)(\text C=y,垂直于 y 轴的一条直线)。接下来我们设计递归算法 solve(Xl, Yl, Xr, Yr),其作用为求凸包上两顶点 \text L(\text{Xl},\text{Yl}),\text R(\text{Xr},\text{Yr}) 之间所有的顶点。其流程如下:

初始调用 solve(cut(0, 1), cut(1, 0)) 即可解出目标凸包上的所有顶点。时间复杂度优化到 \mathcal O(n^{\frac 53}\log n+qn^{\frac 23})。

:::info[code]

#include <bits/stdc++.h>
#include <bits/extc++.h>

namespace {
    struct Tuple {
        long long w;
        int u, v;

        friend auto operator<=>(Tuple x, Tuple y) {
            return x.w != y.w ? x.w <=> y.w : x.u != y.u ? x.u <=> y.u : x.v <=> y.v;
        }
    };

    std::size_t n, m, s, h;
    std::vector<std::size_t> P, Q;
    std::vector<std::pair<int, int>> convex, filtered;
    std::vector<Tuple> minimum, cmintag, plustag;

    void apply(std::size_t i, Tuple cmin, Tuple plus) {
        minimum[i] = (minimum[i].w == INT64_MAX ? minimum[i] : std::min(cmin, Tuple{minimum[i].w + plus.w, minimum[i].u, minimum[i].v + plus.v}));
        cmintag[i] = (cmintag[i].w == INT64_MAX ? cmin : std::min(cmin, Tuple{cmintag[i].w + plus.w, cmintag[i].u, cmintag[i].v + plus.v}));
        plustag[i] = Tuple{plustag[i].w + plus.w, plustag[i].u, plustag[i].v + plus.v};
    }

    void modify(std::size_t u, std::size_t v, Tuple cmin, Tuple plus) {
        u += s;
        v += s;
        for (std::size_t i = h; i >= 1; --i) {
            auto p = u >> i, q = v >> i;
            if ((true) && (cmintag[p].w != INT64_MAX || plustag[p].w != 0 || plustag[p].v != 0)) {
                apply(p << 1, cmintag[p], plustag[p]);
                apply(p << 1 | 1, cmintag[p], plustag[p]);
                cmintag[p] = {INT64_MAX, 0, 0};
                plustag[p] = {0,         0, 0};
            }
            if (p != q && (cmintag[q].w != INT64_MAX || plustag[q].w != 0 || plustag[q].v != 0)) {
                apply(q << 1, cmintag[q], plustag[q]);
                apply(q << 1 | 1, cmintag[q], plustag[q]);
                cmintag[q] = {INT64_MAX, 0, 0};
                plustag[q] = {0,         0, 0};
            }
        }
        for (std::size_t x = u, y = v; x <= y; x >>= 1, y >>= 1) {
            if (+x & 1) apply(x++, cmin, plus);
            if (~y & 1) apply(y--, cmin, plus);
        }
        for (std::size_t i = 1; i <= h; ++i) {
            auto p = u >> i, q = v >> i;
            if ((true)) {
                minimum[p] = std::min(minimum[p << 1], minimum[p << 1 | 1]);
                if (minimum[p].w != INT64_MAX) {
                    minimum[p].w += plustag[p].w;
                    minimum[p].v += plustag[p].v;
                    if (cmintag[p].w != INT64_MAX && cmintag[p] < minimum[p]) minimum[p] = cmintag[p];
                }
            }
            if (p != q) {
                minimum[q] = std::min(minimum[q << 1], minimum[q << 1 | 1]);
                if (minimum[q].w != INT64_MAX) {
                    minimum[q].w += plustag[q].w;
                    minimum[q].v += plustag[q].v;
                    if (cmintag[q].w != INT64_MAX && cmintag[q] < minimum[q]) minimum[q] = cmintag[q];
                }
            }
        }
    }
}

long long calculate(int A, int B, int x, int y) {
    auto costA = static_cast<long long>(A) * x;
    auto costB = static_cast<long long>(B) * y;
    return costA + costB;
}

std::pair<int, int> cut(int A, int B) {
    for (s = 1, h = 0; s < m; s <<= 1, ++h);
    minimum.assign(s << 1, {INT64_MAX, 0, 0});
    cmintag.assign(s << 1, {INT64_MAX, 0, 0});
    plustag.assign(s << 1, {0,         0, 0});

    std::fill_n(minimum.data() + s, m, Tuple{0, 0, 0});
    for (auto i = s; i--; minimum[i] = std::min(minimum[i << 1], minimum[i << 1 | 1]));

    for (std::size_t i = 0; i < n; ++i) {
        modify(P[i], Q[i], {INT64_MAX, 0, 0}, {B, 0, 1});
        modify(0, m - 1, {minimum[1].w + A, minimum[1].u + 1, minimum[1].v}, {0, 0, 0});
    }
    return {minimum[1].u, minimum[1].v};
}

void solve(int Xl, int Yl, int Xr, int Yr) {
    int A = Yl - Yr, B = Xr - Xl;
    auto [Xm, Ym] = cut(A, B);
    if (calculate(A, B, Xm, Ym) >= calculate(A, B, Xl, Yl) || calculate(A, B, Xm, Ym) >= calculate(A, B, Xr, Yr)) {
        convex.emplace_back(Xl, Yl);
        convex.emplace_back(Xr, Yr);
    } else {
        solve(Xl, Yl, Xm, Ym);
        solve(Xm, Ym, Xr, Yr);
    }
}

void init(int N, int M, std::vector<int> Y1, std::vector<int> Y2) {
    n = static_cast<std::size_t>(N);
    m = static_cast<std::size_t>(M);

    std::vector<int> Y{0, M};
    Y.reserve(Y.size() + n * 2);
    for (std::size_t i = 0; i < n; ++i) {
        Y.push_back(Y1[i] + 0);
        Y.push_back(Y2[i] + 1);
    }

    std::sort(Y.begin(), Y.end());
    m = static_cast<std::size_t>(Y.erase(std::unique(Y.begin(), Y.end()), Y.end()) - Y.begin() - 1);

    P.resize(n);
    Q.resize(n);
    for (std::size_t i = 0; i < n; ++i) {
        P[i] = static_cast<std::size_t>(std::lower_bound(Y.begin(), Y.end(), Y1[i] + 0) - Y.begin() - 0);
        Q[i] = static_cast<std::size_t>(std::lower_bound(Y.begin(), Y.end(), Y2[i] + 1) - Y.begin() - 1);
    }

    auto [Xl, Yl] = cut(1, 0);
    auto [Xr, Yr] = cut(0, 1);
    convex.clear();
    convex.reserve(n);
    if (Xl == Xr && Yl == Yr) {
        convex.emplace_back(Xl, Yl);
        convex.emplace_back(Xr, Yr);
    } else {
        convex.emplace_back(Xl, Yl);
        solve(Xl, Yl, Xr, Yr);
        convex.emplace_back(Xr, Yr);
    }

    std::sort(convex.begin(), convex.end());
    convex.erase(std::unique(convex.begin(), convex.end()), convex.end());
    filtered.reserve(convex.size());
    for (auto [x, y] : convex)
        if (filtered.empty() || (x >= filtered.back().first && y <= filtered.back().second))
            filtered.emplace_back(x, y);
    convex = std::move(filtered);
}

long long minimize(int A, int B) {
    long long answer = INT64_MAX;
    for (auto [x, y] : convex)
        answer = std::min(answer, calculate(A, B, x, y));
    return answer;
}

:::

以上代码足以通过,但还可以进一步优化。注意到目标凸包具有性质:相邻顶点之间连线的斜率单调递增。因此我们将目标凸包上的所有顶点按 x 递增 y 递减排序,之后查询的时候可以二分斜率(或者说三分)快速求出目标顶点。最终时间复杂度 \mathcal O(n^{\frac 53}\log n+q\log n) 但是由于凸包定点数很难卡到 \mathcal O(n^{\frac 23}) 的上界因此效率并不会提高太多。