#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <int N>
struct St {
int st[N << 1]{};
void upd(int i, int v) {
for (st[i += N] = v; i >>= 1;) st[i] = max(st[i << 1], st[i << 1 | 1]);
}
int walk(int l, int x) {
for (l += N;; l >>= 1) {
if (l & 1) {
if (st[l] > x) {
while (l < N) l ^= st[l <<= 1] <= x;
return l - N;
}
++l;
}
}
}
};
int main() {
int n;
cin >> n;
int x[n + 1]{};
St<1 << 18> L, R;
for (int i = 1; i <= n; ++i) {
cin >> x[i];
if (i == 1) continue;
L.upd(n - i, x[i] * 2 - x[i - 1]);
R.upd(i - 1, x[i] - x[i - 1] * 2);
}
L.upd(n - 1, 2e9);
ll ans[n + 1]{};
for (int i = 1; i <= n; ++i) {
int l = i, r = i;
while (l > 1) {
while (r < n and x[r + 1] - x[r] < x[r] - x[l - 1]) ++r;
ans[i] += x[r] - x[l];
if (r == n) break;
while (l > 1 and x[l] - x[l - 1] <= x[r + 1] - x[l]) --l;
ans[i] += x[r] - x[l];
}
ans[i] += x[n] - x[1];
}
int q;
cin >> q;
while (q--) {
int s;
cin >> s;
if (s >= x[n]) cout << ans[n] << endl;
else {
int i = upper_bound(x + 1, x + n + 1, max(s, x[1])) - x;
cout << (x[i] - s < s - x[i - 1] ? ans[i] : ans[i - 1]) << endl;
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |