#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] = 1e18;
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) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
29 ms |
5428 KB |
Output is correct |
21 |
Correct |
31 ms |
5432 KB |
Output is correct |
22 |
Correct |
28 ms |
5396 KB |
Output is correct |
23 |
Correct |
29 ms |
5552 KB |
Output is correct |
24 |
Correct |
39 ms |
5748 KB |
Output is correct |
25 |
Correct |
155 ms |
9032 KB |
Output is correct |
26 |
Correct |
142 ms |
9020 KB |
Output is correct |
27 |
Correct |
142 ms |
8744 KB |
Output is correct |
28 |
Correct |
146 ms |
8708 KB |
Output is correct |
29 |
Correct |
129 ms |
8388 KB |
Output is correct |
30 |
Correct |
88 ms |
8040 KB |
Output is correct |
31 |
Correct |
70 ms |
7640 KB |
Output is correct |
32 |
Correct |
53 ms |
6936 KB |
Output is correct |
33 |
Correct |
12 ms |
1332 KB |
Output is correct |
34 |
Correct |
110 ms |
8384 KB |
Output is correct |
35 |
Correct |
130 ms |
8480 KB |
Output is correct |
36 |
Correct |
144 ms |
9048 KB |
Output is correct |
37 |
Correct |
133 ms |
8644 KB |
Output is correct |
38 |
Correct |
147 ms |
8848 KB |
Output is correct |
39 |
Correct |
137 ms |
8928 KB |
Output is correct |
40 |
Correct |
106 ms |
8812 KB |
Output is correct |
41 |
Correct |
28 ms |
3416 KB |
Output is correct |
42 |
Correct |
54 ms |
10080 KB |
Output is correct |
43 |
Correct |
112 ms |
16404 KB |
Output is correct |
44 |
Correct |
48 ms |
10564 KB |
Output is correct |
45 |
Correct |
106 ms |
12476 KB |
Output is correct |
46 |
Correct |
114 ms |
16280 KB |
Output is correct |
47 |
Correct |
81 ms |
13736 KB |
Output is correct |
48 |
Correct |
87 ms |
13716 KB |
Output is correct |