跳到正文

题解

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

KaguyaH约 9 分钟
原文
文章信息与版本

文章信息

发布
2021年11月13日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 9 分钟
获赞
13
题目
P7294

版本

  1. v1当前

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

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

qq 组询问,每次询问 (1,1)⇝(xi,yi)(1, 1) \rightsquigarrow (x_i, y_i) 所需最小代价。询问互相独立。

2≤n≤1092 \le n \le 10^9,2≤m≤2×1052 \le m \le 2 \times 10^5,1≤ci≤1091 \le c_i \le 10^9,1≤q≤2×1051 \le q \le 2 \times 10^5,xi∈[1,n]x_i \in [1, n],yi∈[1,m]y_i \in [1, m]。各数据均为整数。

记 fy(x)f_y(x) 为 (1,1)⇝(x,y)(1, 1) \rightsquigarrow (x, y) 的代价。有 f1(x)=(x−1)c1f_1(x) = (x - 1)c_1。显然 f1f_1 单调增。

令 gy(x)=fy(x)−fy(x−1)g_y(x) = f_y(x) - f_y(x - 1)。显然 g1g_1 为常函数。

考虑由 f1f_1 转移到 f2f_2 的过程。

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

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

  2. xx 由 22 扫描至 nn,令 f2(x)←min⁡{f2(x),f2(x−1)+c2}f_2(x) \leftarrow \min\{ f_2(x), f_2(x - 1) + c_2 \}(称这一操作为纵更新)。

    注意到,纵更新只会更新 g2>cyg_2 > c_y 的部分,也即 f2f_2 的一个后缀,故纵更新后 f2,g2f_2, g_2 单调不降。

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

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

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

根据定义,fy(x)=fp(y,x)(x)+(y−py(x))x2f_y(x) = f_{p(y, x)}(x) + (y - p_y(x))x^2。

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

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

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

C++
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');
  }
}
原文
存档评论3 条抓取于