#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 200001;
const int lg = 19;
int lg2[N], mn[N][lg], mx[N][lg];
int query_min(int l, int r) {
int j = lg2[r - l + 1];
return min(mn[l][j], mn[r - (1 << j) + 1][j]);
}
int query_max(int l, int r) {
int j = lg2[r - l + 1];
return max(mx[l][j], mx[r - (1 << j) + 1][j]);
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int n, q;
cin >> n;
vector<int> x(n), ll(n), rr(n);
for (int i = 0; i < n; i++) {
cin >> x[i];
ll[i] = rr[i] = i;
}
for (int i = 1; i < n; i++) {
int dist = x[i] - x[i - 1];
int l = i, r = n - 1;
while (l < r) {
int mid = (l + r + 1) / 2;
if (x[mid] - x[i] < dist) l = mid;
else r = mid - 1;
}
if (x[l] - x[i] < dist) rr[i - 1] = l; // if i'm going left to i - 1, rb must > l
}
for (int i = 0; i < n - 1; i++) {
int dist = x[i + 1] - x[i];
int l = 0, r = i;
while (l < r) {
int mid = (l + r) / 2;
if (x[i] - x[mid] <= dist) r = mid;
else l = mid + 1;
}
if (x[i] - x[l] <= dist) ll[i + 1] = l; // if i'm going right to i + 1, lb must < l
}
// for (int i = 0; i < n; i++) cout << ll[i] << ' ' << rr[i] << '\n';
// lg2[1] = 0;
for (int i = 2; i <= n; i++) lg2[i] = lg2[i / 2] + 1;
for (int i = 0; i < n; i++) mx[i][0] = rr[i], mn[i][0] = ll[i];
for (int j = 1; j < lg; j++) {
for (int i = 0; i + (1 << j) - 1 < n; i++) {
mn[i][j] = min(mn[i][j - 1], mn[i + (1 << (j - 1))][j - 1]);
mx[i][j] = max(mx[i][j - 1], mx[i + (1 << (j - 1))][j - 1]);
}
}
cin >> q;
while (q--) {
int s, ans = 0;
cin >> s;
int left = 0, lb, rb, cp;
if (s >= x.back()) left = 1, lb = rb = n - 1, ans += s - x.back(), cp = n - 1;
else if (s > x.front()) {
auto it = lower_bound(x.begin(), x.end(), s);
int prv, nxt;
if ((*it) == s) {
prv = *prev(it);
nxt = *next(it);
lb = rb = cp = it - x.begin();
} else {
prv = *prev(it);
nxt = *it;
if (s - prv <= nxt - s) lb = rb = cp = it - x.begin() - 1, ans += s - prv;
else lb = rb = cp = it - x.begin(), ans += nxt - s;
}
if (lb == 0) left = 1;
else if (rb == n - 1) left = 0;
else {
prv = x[lb - 1];
nxt = x[rb + 1];
if (x[cp] - prv <= nxt - x[cp]) left = 0;
else left = 1;
}
} else lb = rb = 0, ans += x.front() - s, cp = 0;
// cout << "ans:" << lb << ' ' << rb << ' ' << ans << '\n';
for (int ti = 0; ti < (lg << 1); ti++) {
left ^= 1;
if (lb <= 0 && rb > n) break;
if (left && lb > 0) {
int l = 0, r = lb - 1;
while (l < r) {
int mid = (l + r) / 2;
if (rb >= query_max(mid, lb - 1)) r = mid;
else l = mid + 1;
}
ans += abs(x[cp] - x[l]);
lb = cp = l;
// cout << "ans:" << lb << ' ' << rb << ' ' << ans << '\n';
} else if (!left && rb < n - 1) {
int l = rb + 1, r = n - 1;
while (l < r) {
int mid = (l + r + 1) / 2;
if (lb <= query_min(rb + 1, mid)) l = mid;
else r = mid - 1;
}
ans += abs(x[l] - x[cp]);
rb = cp = l;
// cout << "ans:" << lb << ' ' << rb << ' ' << ans << '\n';
}
// cout << "iter\n";
}
// cout << "final ans:" << lb << ' ' << rb << ' ' << ans << '\n';
cout << ans << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
4628 KB |
Output is correct |
15 |
Correct |
3 ms |
4696 KB |
Output is correct |
16 |
Correct |
2 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
4628 KB |
Output is correct |
15 |
Correct |
3 ms |
4696 KB |
Output is correct |
16 |
Correct |
2 ms |
4700 KB |
Output is correct |
17 |
Correct |
98 ms |
68148 KB |
Output is correct |
18 |
Correct |
81 ms |
67920 KB |
Output is correct |
19 |
Correct |
80 ms |
68136 KB |
Output is correct |
20 |
Correct |
76 ms |
68128 KB |
Output is correct |
21 |
Correct |
78 ms |
67996 KB |
Output is correct |
22 |
Correct |
77 ms |
67920 KB |
Output is correct |
23 |
Correct |
75 ms |
67980 KB |
Output is correct |
24 |
Correct |
81 ms |
68176 KB |
Output is correct |
25 |
Correct |
79 ms |
68140 KB |
Output is correct |
26 |
Correct |
79 ms |
68148 KB |
Output is correct |
27 |
Correct |
82 ms |
68140 KB |
Output is correct |
28 |
Correct |
86 ms |
68152 KB |
Output is correct |
29 |
Correct |
79 ms |
67984 KB |
Output is correct |
30 |
Correct |
76 ms |
68084 KB |
Output is correct |
31 |
Correct |
78 ms |
68088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
249 ms |
67412 KB |
Output is correct |
8 |
Correct |
213 ms |
69972 KB |
Output is correct |
9 |
Correct |
261 ms |
70052 KB |
Output is correct |
10 |
Correct |
215 ms |
69972 KB |
Output is correct |
11 |
Correct |
258 ms |
70196 KB |
Output is correct |
12 |
Correct |
211 ms |
69972 KB |
Output is correct |
13 |
Correct |
37 ms |
6192 KB |
Output is correct |
14 |
Correct |
29 ms |
3664 KB |
Output is correct |
15 |
Correct |
200 ms |
71148 KB |
Output is correct |
16 |
Correct |
192 ms |
70996 KB |
Output is correct |
17 |
Correct |
199 ms |
71324 KB |
Output is correct |
18 |
Correct |
36 ms |
6224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
4628 KB |
Output is correct |
15 |
Correct |
3 ms |
4696 KB |
Output is correct |
16 |
Correct |
2 ms |
4700 KB |
Output is correct |
17 |
Correct |
98 ms |
68148 KB |
Output is correct |
18 |
Correct |
81 ms |
67920 KB |
Output is correct |
19 |
Correct |
80 ms |
68136 KB |
Output is correct |
20 |
Correct |
76 ms |
68128 KB |
Output is correct |
21 |
Correct |
78 ms |
67996 KB |
Output is correct |
22 |
Correct |
77 ms |
67920 KB |
Output is correct |
23 |
Correct |
75 ms |
67980 KB |
Output is correct |
24 |
Correct |
81 ms |
68176 KB |
Output is correct |
25 |
Correct |
79 ms |
68140 KB |
Output is correct |
26 |
Correct |
79 ms |
68148 KB |
Output is correct |
27 |
Correct |
82 ms |
68140 KB |
Output is correct |
28 |
Correct |
86 ms |
68152 KB |
Output is correct |
29 |
Correct |
79 ms |
67984 KB |
Output is correct |
30 |
Correct |
76 ms |
68084 KB |
Output is correct |
31 |
Correct |
78 ms |
68088 KB |
Output is correct |
32 |
Correct |
0 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
0 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2392 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
249 ms |
67412 KB |
Output is correct |
39 |
Correct |
213 ms |
69972 KB |
Output is correct |
40 |
Correct |
261 ms |
70052 KB |
Output is correct |
41 |
Correct |
215 ms |
69972 KB |
Output is correct |
42 |
Correct |
258 ms |
70196 KB |
Output is correct |
43 |
Correct |
211 ms |
69972 KB |
Output is correct |
44 |
Correct |
37 ms |
6192 KB |
Output is correct |
45 |
Correct |
29 ms |
3664 KB |
Output is correct |
46 |
Correct |
200 ms |
71148 KB |
Output is correct |
47 |
Correct |
192 ms |
70996 KB |
Output is correct |
48 |
Correct |
199 ms |
71324 KB |
Output is correct |
49 |
Correct |
36 ms |
6224 KB |
Output is correct |
50 |
Correct |
424 ms |
72284 KB |
Output is correct |
51 |
Correct |
384 ms |
72276 KB |
Output is correct |
52 |
Correct |
372 ms |
72364 KB |
Output is correct |
53 |
Correct |
290 ms |
72172 KB |
Output is correct |
54 |
Correct |
290 ms |
72324 KB |
Output is correct |
55 |
Correct |
278 ms |
72180 KB |
Output is correct |
56 |
Correct |
171 ms |
72216 KB |
Output is correct |
57 |
Correct |
150 ms |
72020 KB |
Output is correct |
58 |
Correct |
159 ms |
72100 KB |
Output is correct |