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 <iostream>
#include <vector>
using namespace std;
long long n, test, a[200010], cur, x, minn, maxx, b[200010], l, r, mid, ans[200010];
vector<long long> v;
int main() {
cin >> n >> test;
for (int i = 1; i <= n; i++) cin >> a[i];
v.push_back(0);
while (test--) {
cin >> x;
cur += x;
if (cur > maxx) {
if (v.back() <= 0) v.push_back(cur);
else v.back() = cur;
maxx = cur;
} else if (cur < minn) {
if (v.back() >= 0) v.push_back(cur);
else v.back() = cur;
minn = cur;
}
}
for (int i = 1; i < (int)v.size(); i++) b[i] = abs(v[i-1]-v[i]);
ans[1] -= minn;
for (int i = 2; i <= n; i++) {
l = 1; r = v.size()-1;
while (l <= r) {
int mid = (l+r)/2;
if (a[i]-a[i-1] <= b[mid]) r = mid-1;
else l = mid+1;
}
if (r < (int)v.size()-1) {
r++;
if (v[r-1] < 0) ans[i] -= max(a[i-1]-a[i],v[r-1]);
else ans[i-1] += min(a[i]-a[i-1],v[r-1]);
if (v[r] < 0) ans[i] -= max(a[i-1]+min(a[i]-a[i-1],v[r-1])-a[i],v[r]);
else ans[i-1] += min(a[i]+max(a[i-1]-a[i],v[r-1])-a[i-1],v[r]);
} else {
if (v[r-1] < 0) ans[i] -= max(a[i-1]-a[i],v[r-1]);
else ans[i-1] += min(a[i]-a[i-1],v[r-1]);
if (v[r] < 0) ans[i] -= max(a[i-1]+min(a[i]-a[i-1],v[r-1])-a[i],v[r]);
else ans[i-1] += min(a[i]+max(a[i-1]-a[i],v[r-1])-a[i-1],v[r]);
}
}
ans[n] += maxx;
for (int i = 1; i <= n; i++) cout << ans[i] << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |