This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e16;
const int MAXN = 2e5 + 25;
ll a[MAXN], n, q;
ll pref[MAXN], u[MAXN], mx[MAXN], mn[MAXN];
int main () {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= q; i++) {
cin >> u[i]; u[i] += u[i - 1];
mx[i] = max(u[i], mx[i - 1]);
mn[i] = min(u[i], mn[i - 1]);
}
a[0] = -inf; a[n + 1] = inf;
for (int i = 1; i <= n; i++) {
ll l = 1, r = q, ans1 = 0;
ll tl = a[i - 1], tr = a[i + 1];
while (l <= r) {
ll mid = (l + r) >> 1;
if (tl + mx[mid] < a[i] + mn[mid]) {
l = mid + 1; ans1 = mid;
} else {
r = mid - 1;
}
}
l = 1, r = q; ll ans2 = 0;
while (l <= r) {
ll mid = (l + r) >> 1;
if (a[i] + mx[mid] < tr + mn[mid]) {
l = mid + 1; ans2 = mid;
} else {
r = mid - 1;
}
}
ll weight = -mn[ans1] + mx[ans2];
//cout << tl + mx[ans1] << " " << a[i] + mn[ans1] << ": " << a[i] + mx[ans2] << " " << tr + mn[ans2] << '\n';
if (ans1 < q) {
ll x = tl + mx[ans1], y = a[i] + mn[ans1];
if (u[ans1 + 1] < 0) {
weight += y - x;
}
}
if (ans2 < q) {
ll x = a[i] + mx[ans2], y = tr + mn[ans2];
if (u[ans2 + 1] > 0) {
weight += y - x;
}
}
cout << weight << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |