#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<i64> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<i64> dist(n - 1);
for (int i = 0; i + 1 < n; i++) {
dist[i] = a[i + 1] - a[i];
}
vector<pair<i64, int>> que;
i64 cur = 0;
for (int i = 0; i < q; i++) {
i64 w;
cin >> w;
cur += w;
que.push_back({cur, i});
}
sort(que.begin(), que.end());
vector<int> sm(q), pm(q);
for (int i = q - 1; i >= 0; i--) {
sm[i] = que[i].second;
if (i + 1 < q) sm[i] = min(sm[i], sm[i + 1]);
}
for (int i = 0; i < q; i++) {
pm[i] = que[i].second;
if (i > 0) pm[i] = min(pm[i], pm[i - 1]);
}
vector<i64> we(n);
for (int i = 0; i < n; i++) {
{
auto f = [&](i64 x, i64 d) {
int ind = lower_bound(que.begin(), que.end(),
pair<i64, int>{x, -1}) - que.begin();
if (ind == q) return false;
int ind2 = lower_bound(que.begin(), que.end(),
pair<i64, int>{x - d, -1}) - que.begin() - 1;
if (ind2 < 0) return true;
return pm[ind2] > sm[ind];
};
i64 d = (i + 1 < n ? dist[i] : (i64)1e18);
i64 s = 0, e = d;
while (s < e) {
i64 mid = (s + e + 1) / 2;
if (f(mid, d)) s = mid;
else e = mid - 1;
}
we[i] += e;
}
{
auto f = [&](i64 x, i64 d) {
int ind = upper_bound(que.begin(), que.end(),
pair<i64, int>{-x, INT_MAX}) - que.begin() - 1;
if (ind < 0) return false;
int ind2 = upper_bound(que.begin(), que.end(),
pair<i64, int>{d - x, INT_MAX}) - que.begin();
if (ind2 == q) return true;
return sm[ind2] > pm[ind];
};
i64 d = (i > 0 ? dist[i - 1] : (i64)1e18);
i64 s = 0, e = d;
while (s < e) {
i64 mid = (s + e + 1) / 2;
if (f(mid, d)) s = mid;
else e = mid - 1;
}
we[i] += e;
}
}
for (int i = 0; i < n; i++) {
cout << we[i] << '\n';
}
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |