UM 赛时首杀。
容易发现全局 对子段和产生的影响,只与长度有关。因此设 表示,未经过修改时,最大的长为 的子段,则修改后即为 。
那么只需要预处理出所有 组成的上凸壳,每次查询在凸壳上二分,即可以 的复杂度处理询问。具体来说,一条斜率为 的直线经过 ,直线的截距即为 ,使其取到 时,直线恰与凸壳相切——设切点前后两条边的斜率分别为 ,则有 。
至于如何求出凸壳这件事,考虑小白逛公园的思路,进行分治。将 的子区间分成三部分,完全被 或 包含的子区间的凸壳递归下去求,还有一个跨越两边的部分。
设 表示 长为 的后缀和, 表示 长为 的前缀和。全部跨越中点的子区间也就是 和 两两配对起来。
首先能把 的凸壳和 的凸壳求出来,两两配对起来,也就是 ,不难发现这个新的东西的凸壳,也就是二者的闵可夫斯基和,也容易求出。
最后再把三部分的凸壳合并起来即可。
合并凸壳、闵可夫斯基和的复杂度都是线性的,因此这个分治的复杂度是 。
实现上,简单封装一下,不然根本不是人写的,注意三点共线每个点都要保留,如果有两个点重合的话只保留一个。
略卡常,可以把询问离线下来排个序,然后利用单调性扫一遍,或者用上一次的答案作为这次二分的左边界,之类的。
#include <bits/stdc++.h>
#define F(i, a, b) for (int i = (a); i <= (b); ++i)
#define dF(i, a, b) for (int i = (a); i >= (b); --i)
typedef long long LL;
using namespace std;
const int N = 1e6 + 5;
int n, m, q; LL a[N], s[N], x[N];
struct node {
int x; LL y;
node operator + (const node &a) const { return {x + a.x, y + a.y}; }
node operator - (const node &a) const { return {x - a.x, y - a.y}; }
__int128 operator ^ (const node &a) const
{ return (__int128) a.y * x - (__int128) y * a.x; }
bool operator == (const node &a) const
{ return x == a.x && y == a.y; }
} ;
vector<node> t[N << 2];
void Insert(vector<node> &vec, node y) {
while (vec.size() > 1) {
node x = vec.back(); vec.pop_back();
if (x == y) continue;
if ((x - vec.back() ^ y - x) > 0) ;
else { vec.push_back({x}); break; }
}
vec.push_back(y);
}
vector<node> Merge(vector<node> x, vector<node> y) {
vector<node> res; res.push_back({0, 0});
int i = 0, j = 0;
while (i + 1 < x.size() && j + 1 < y.size())
if (x[i + 1].x < y[j + 1].x) Insert(res, x[++i]);
else Insert(res, y[++j]);
while (i + 1 < x.size()) Insert(res, x[++i]);
while (j + 1 < y.size()) Insert(res, y[++j]);
return res;
}
void Solve(int l, int r, int u) {
if (l == r) {
t[u].push_back({0, 0});
t[u].push_back({1, a[l]});
return ;
}
int mid = l + r >> 1;
Solve(l, mid, u << 1);
Solve(mid + 1, r, u << 1 | 1);
t[u] = Merge(t[u << 1], t[u << 1 | 1]);
vector<node> ls, rs, ms;
ls.push_back({0, 0}), rs.push_back({0, 0}), ms.push_back({0, 0});
dF(i, mid, l) Insert(ls, {mid - i + 1, s[mid] - s[i - 1]});
F(i, mid + 1, r) Insert(rs, {i - mid, s[i] - s[mid]});
dF(i, (int) ls.size() - 1, 1) ls[i] = ls[i] - ls[i - 1];
dF(i, (int) rs.size() - 1, 1) rs[i] = rs[i] - rs[i - 1];
int i = 0, j = 0;
while (i + 1 < ls.size() && j + 1 < rs.size())
if ((ls[i + 1] ^ rs[j + 1]) > 0) ms.push_back(rs[++j]);
else ms.push_back({ls[++i]});
while (i + 1 < ls.size()) ms.push_back({ls[++i]});
while (j + 1 < rs.size()) ms.push_back({rs[++j]});
for (int i = 1; i < ms.size(); ++i)
ms[i] = ms[i - 1] + ms[i];
t[u] = Merge(t[u], ms);
}
pair<LL, int> qu[N]; LL ans[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> q;
F(i, 1, n) cin >> a[i];
F(i, 1, n) s[i] = s[i - 1] + a[i];
Solve(1, n, 1);
m = (int) t[1].size() - 1;
F(i, 1, q) cin >> qu[i].first;
F(i, 1, q) qu[i].second = i;
sort(qu + 1, qu + q + 1);
int now = 1, lst = 1;
F(i, 1, q) {
auto [k, id] = qu[i];
int l = lst, r = m, mid, res = 1;
while (l <= r) {
mid = l + r >> 1;
if (((node){1, -k} ^ t[1][mid] - t[1][mid - 1]) > 0)
res = mid, l = mid + 1;
else r = mid - 1;
}
auto [x, y] = t[1][res];
ans[id] = y + k * x, lst = res;
}
F(i, 1, q) cout << ans[i] << "\n";
return 0;
}