[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_1 转移到f_2 的过程。
令
f_2(x) \leftarrow f_1(x) + x^2 (称这一操作为横更新)。注意到,
x^2 - (x - 1)^2 是单调增的,故横更新后的f_2, g_2 单调增。注意到,纵更新只会更新 $g_2 > c_y$ 的部分,也即 $f_2$ 的一个后缀,故纵更新后 $f_2, g_2$ 单调不降。
类似地,归纳可得,
注意到横更新比较优美,而纵更新至多更新
记
根据定义,
又有若
于是可以类似维护凸壳地,维护各段
时间复杂度
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');
}
}