#include<bits/stdc++.h>
#define print(_v) for (auto& _e : _v) {cerr << _e << ' ';} cerr << '\n';
using namespace std;
using ll = long long;
const int N = 2e5 + 5;
ll a[N], b[N], p[N], res[N];
int n, q;
vector<pair<ll, ll>> c;
void pre_calc() {
for (int i = 1; i <= q; ++i)
p[i] = p[i-1] + b[i];
vector<ll> pos, neg;
if (p[1] >= 0) neg.emplace_back(0);
bool flag = 0;
for (int i = 1; i <= q;) {
int j = i; ll v = 0;
if (p[i] >= 0) {
while (j <= q && p[j] >= 0) v = max(v, p[j]), j++;
pos.emplace_back(v);
} else {
while (j <= q && p[j] <= 0) v = min(v, p[j]), j++;
neg.emplace_back(v);
if (j == q + 1) flag = 1;
}
i = j;
}
c.clear();
ll cur = 0;
for (int i = 0; i < pos.size(); ++i) {
cur = min(cur, neg[i]);
if (c.empty() || pos[i] > c.back().first)
c.emplace_back(pos[i], cur);
}
if (flag) c.emplace_back(pos.empty() ? 0 : pos.back(), min(cur, neg.back()));
}
ll val(int i, int j) {
return min(c[j].first, a[i+1] - a[i] + c[j].second);
}
int check(int i, int j) {
return (j == 0 || val(i, j) > val(i, j-1))
&& (val(i, j) >= val(i, j+1));
}
void solve(int f = 0) {
// tim dinh cua day min(c[i].first, D + c[i].second) voi moi D
for (int i = 1; i <= n; ++i) {
if (c.size() == 1) {
res[f ? n-i+1 : i] += max(0LL, val(i, 0)); continue;
}
int lo = 0, hi = int(c.size()) - 3, mid,
ans = val(i, c.size() - 1) > val(i, c.size() - 2) ? c.size() - 1 : c.size() - 2;
while (lo <= hi) {
mid = (lo + hi) >> 1;
if (check(i, mid))
ans = mid, lo = hi + 1;
else if (val(i, mid) < val(i, mid + 1))
lo = mid + 1;
else
hi = mid - 1;
}
res[f ? n-i+1 : i] += max(0LL ,val(i, ans));
}
}
int main() {
cin.tie(0)->sync_with_stdio(0); cout.tie(0);
// min(pre[i], D - min[i])
// -> day tang roi giam
cin >> n >> q;
a[n + 1] = 1e17;
for (int i = 1; i <= n; ++i)
cin >> a[i];
for (int i = 1; i <= q; ++i)
cin >> b[i];
pre_calc();
solve();
for (int i = 1; i <= n; ++i)
a[i] = -a[i];
reverse(a + 1, a + n + 1);
for (int i = 1; i <= q; ++i)
b[i] = -b[i];
pre_calc();
solve(1);
for (int i = 1; i <= n; ++i)
cout << res[i] << '\n';
return 0;
}
Compilation message
Main.cpp: In function 'void pre_calc()':
Main.cpp:29:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for (int i = 0; i < pos.size(); ++i) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
472 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
464 KB |
Output is correct |
10 |
Correct |
2 ms |
500 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
260 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
2 ms |
388 KB |
Output is correct |
16 |
Correct |
2 ms |
472 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
472 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
464 KB |
Output is correct |
10 |
Correct |
2 ms |
500 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
260 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
2 ms |
388 KB |
Output is correct |
16 |
Correct |
2 ms |
472 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
41 ms |
5664 KB |
Output is correct |
21 |
Correct |
33 ms |
5696 KB |
Output is correct |
22 |
Correct |
32 ms |
5664 KB |
Output is correct |
23 |
Correct |
29 ms |
5688 KB |
Output is correct |
24 |
Correct |
46 ms |
6048 KB |
Output is correct |
25 |
Correct |
178 ms |
9284 KB |
Output is correct |
26 |
Correct |
165 ms |
9308 KB |
Output is correct |
27 |
Correct |
185 ms |
9124 KB |
Output is correct |
28 |
Correct |
153 ms |
8860 KB |
Output is correct |
29 |
Correct |
163 ms |
8548 KB |
Output is correct |
30 |
Correct |
101 ms |
8112 KB |
Output is correct |
31 |
Correct |
70 ms |
7828 KB |
Output is correct |
32 |
Correct |
57 ms |
6972 KB |
Output is correct |
33 |
Correct |
14 ms |
1284 KB |
Output is correct |
34 |
Correct |
131 ms |
8476 KB |
Output is correct |
35 |
Correct |
161 ms |
8404 KB |
Output is correct |
36 |
Correct |
170 ms |
9092 KB |
Output is correct |
37 |
Correct |
147 ms |
8704 KB |
Output is correct |
38 |
Correct |
156 ms |
9100 KB |
Output is correct |
39 |
Correct |
152 ms |
8788 KB |
Output is correct |
40 |
Correct |
123 ms |
8736 KB |
Output is correct |
41 |
Incorrect |
31 ms |
3384 KB |
Output isn't correct |
42 |
Halted |
0 ms |
0 KB |
- |