[Solution][USACO21JAN][P] Minimum Cost Paths

· · 题解

一张 n \cdot m 的棋盘,点编号为 ([1, n], [1, m]),起点位于 (1, 1)。

一步可以从 (x, y) 走到 (x, y+ 1),代价 x^2;可以从 (x, y) 走到 (x + 1, y),代价 c_y。

$2 \le n \le 10^9$,$2 \le m \le 2 \times 10^5$,$1 \le c_i \le 10^9$,$1 \le q \le 2 \times 10^5$,$x_i \in [1, n]$,$y_i \in [1, m]$。各数据均为整数。

记 f_y(x) 为 (1, 1) \rightsquigarrow (x, y) 的代价。有 f_1(x) = (x - 1)c_1。显然 f_1 单调增。

令 g_y(x) = f_y(x) - f_y(x - 1)。显然 g_1 为常函数。

考虑由 f_1 转移到 f_2 的过程。

  1. 令 f_2(x) \leftarrow f_1(x) + x^2(称这一操作为横更新)。

    注意到,x^2 - (x - 1)^2 是单调增的,故横更新后的 f_2, g_2 单调增。

  2. 注意到,纵更新只会更新 $g_2 > c_y$ 的部分,也即 $f_2$ 的一个后缀,故纵更新后 $f_2, g_2$ 单调不降。

类似地,归纳可得,\forall 1 \le y \le m,f_y, g_y 单调不降。

注意到横更新比较优美,而纵更新至多更新 m 组。故考虑询问离线(按 y 排序),并依次处理 f_{1 \dots m}。

记 p_y(x) 为 f_{1 \dots y}(x) 最后一次被纵更新时的位置。p_y 可以分为 O(m) 段,各段内值相同。

根据定义,f_y(x) = f_{p(y, x)}(x) + (y - p_y(x))x^2。

又有若 \forall l \le x \le r, p_y(x) = p_y(l) 则 \forall l \lt x \le r, g_{p(y, l)}(x) = g_{p(y, l)}(r)。即:若 [l, r] 中 p_y 相等,则 f_{p(y)} 在 [l, r] 中成等差数列或一次函数(因为此时 [l, r] 被更新,而被更新后初始状态应如此)。

于是可以类似维护凸壳地,维护各段 p_y, f_{p(y)},转移与查询均可二分。

时间复杂度 O(m \log nm + q \log m),空间复杂度 O(m + q)。

namespace Main::Main {
  static constexpr const lu M(2e5), Q(2e5);
  class function {
    lu k; llu b;
  public:
    constexpr function(const lu k = lu(), const llu b = llu()): k(k), b(b) {}
    inline void err() const { fprintf(stderr, "k = %lu b = %llu\n", k, b); }
    inline constexpr llu operator()(const lu x) { return 1ull * k * x + b; }
  };
  struct query {
    lu x, y; lu id; llu ans;
    inline void in(const lu id) { io >> x >> y, this->id = id; }
    friend inline constexpr bool operator<(const query& a, const query& b)
    { return a.id < b.id; }
    static inline constexpr bool comp(const query& a, const query& b)
    { return a.y < b.y; }
  };
  struct func {
    function f; lu x, y;
    constexpr func(const function& f, const lu x, const lu y): f(f), x(x), y(y) {}
    // [x, infty) [y, infty)
    inline void err() const { f.err(), fprintf(stderr, "x = %lu y = %lu\n", x, y); }
    inline constexpr operator lu() { return x; }
    inline constexpr llu operator()(const lu _x, const lu _y)
    { return f(_x - x) + 1ull * (_y - y) * _x * _x; }
  };
  static lu n, m; static lu c[M + 1]; static lu q; static query r[Q];
  static vector<func> ans;
  static inline void insert(const lu y) {
    // fprintf(stderr, "insert %lu\n", y);
    ld l(-1), r(ans.size());
    while (r - l > 1) {
      const lu t((l + r) / 2), x(t + 1 == ans.size() ? n : ans[t + 1].x - 1);
      (
        (
          x == ans[t].x ?
            x == 1 || ans[t](x, y) - ans[t - 1](x - 1, y) < c[y] :
            ans[t](x, y) - ans[t](x - 1, y) < c[y]
        ) ? l : r
      ) = t;
    }
    // fprintf(stderr, "insert l = %ld, r = %ld\n", l, r);
    if ((lu)r == ans.size()) return;
    if ((r + 1ul == ans.size() ? n : ans[r + 1].x - 1) == ans[r].x) {
      const lu t(ans[r].x); ans.erase(ans.begin() + r, ans.end());
      ans.emplace_back(function(c[y], ans.back()(t - 1, y) + c[y]), t, y);
      return;
    } 
    lu p(max(ans[r].x - 1, 1ul)), q(r + 1ul == ans.size() ? n : ans[r + 1].x - 1);
    while (q - p > 1)
      (ans[r]((p + q) / 2, y) - ans[r]((p + q) / 2 - 1, y) < c[y] ? p : q) = (p + q) / 2;
    const func t(
      function(c[y], (q == ans[r].x ? ans[r - 1](q - 1, y) : ans[r](q - 1, y)) + c[y]),
      q, y
    );
    ans.erase(ans.begin() + r + (q != ans[r].x), ans.end()), ans.push_back(t);
  }
  static inline llu query(const lu x, const lu y)
  { return (*(upper_bound(ans.begin(), ans.end(), x) - 1))(x, y); }
  static inline void main() {
    io >> n >> m; for (lu i(1); i <= m; ++i) get(c[i]);
    get(q); for (lu i{}; i < q; ++i) r[i].in(i); sort(r, r + q, query::comp);
    ans.emplace_back(function(c[1], 0), 1, 1);
    for (lu i(1), j{}; ; ) {
      // for (const func& f: ans) f.err();
      while (j < q and r[j].y == i) r[j].ans = query(r[j].x, r[j].y), ++j;
      ; if (i == m) break; insert(++i);
    }
    sort(r, r + q); for (lu i{}; i < q; ++i) put(r[i].ans, '\n');
  }
}