一张 的棋盘,点编号为 ,起点位于 。
一步可以从 走到 ,代价 ;可以从 走到 ,代价 。
组询问,每次询问 所需最小代价。询问互相独立。
,,,,,。各数据均为整数。
记 为 的代价。有 。显然 单调增。
令 。显然 为常函数。
考虑由 转移到 的过程。
令 (称这一操作为横更新)。
注意到, 是单调增的,故横更新后的 单调增。
由 扫描至 ,令 (称这一操作为纵更新)。
注意到,纵更新只会更新 的部分,也即 的一个后缀,故纵更新后 单调不降。
类似地,归纳可得,, 单调不降。
注意到横更新比较优美,而纵更新至多更新 组。故考虑询问离线(按 排序),并依次处理 。
记 为 最后一次被纵更新时的位置。 可以分为 段,各段内值相同。
根据定义,。
又有若 则 。即:若 中 相等,则 在 中成等差数列或一次函数(因为此时 被更新,而被更新后初始状态应如此)。
于是可以类似维护凸壳地,维护各段 ,转移与查询均可二分。
时间复杂度 ,空间复杂度 。
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');
}
}