#include <bits/stdc++.h>
using namespace std;
#define ar array
#define sz(v) int(std::size(v))
using i64 = long long;
const int N = 2e5;
int n, q, x[N];
i64 rec(int l, int r, bool dir) {
int p = x[dir ? r : l];
if (l == 0) return x[n - 1] - p;
if (r == n - 1) return p - x[0];
if (p - x[l - 1] <= x[r + 1] - p) {
int q = lower_bound(x, x + n, 2 * p - x[r + 1]) - x;
return p - x[q] + rec(q, r, 0);
} else {
int q = upper_bound(x, x + n, 2 * p - x[l - 1]) - x - 1;
return x[q] - p + rec(l, q, 1);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 0; i < n; i++) cin >> x[i];
cin >> q;
while (q--) {
int s;
cin >> s;
int p = upper_bound(x, x + n, s) - x - 1;
if (p == -1) cout << x[n - 1] - s << '\n';
else if (p == n - 1) cout << s - x[0] << '\n';
else if (s - x[p] <= x[p + 1] - s) cout << s - x[p] + rec(p, p, 0) << '\n';
else cout << x[p + 1] - s + rec(p + 1, p + 1, 1) << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
464 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
480 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
464 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
480 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
15 ms |
3160 KB |
Output is correct |
18 |
Correct |
16 ms |
3152 KB |
Output is correct |
19 |
Correct |
19 ms |
3000 KB |
Output is correct |
20 |
Correct |
15 ms |
3092 KB |
Output is correct |
21 |
Correct |
15 ms |
3100 KB |
Output is correct |
22 |
Correct |
16 ms |
3140 KB |
Output is correct |
23 |
Correct |
15 ms |
3200 KB |
Output is correct |
24 |
Correct |
15 ms |
2916 KB |
Output is correct |
25 |
Correct |
15 ms |
2968 KB |
Output is correct |
26 |
Correct |
15 ms |
3160 KB |
Output is correct |
27 |
Correct |
15 ms |
3160 KB |
Output is correct |
28 |
Correct |
15 ms |
3164 KB |
Output is correct |
29 |
Correct |
16 ms |
2912 KB |
Output is correct |
30 |
Correct |
17 ms |
2952 KB |
Output is correct |
31 |
Correct |
15 ms |
3096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
332 ms |
5160 KB |
Output is correct |
8 |
Correct |
329 ms |
5136 KB |
Output is correct |
9 |
Correct |
325 ms |
4948 KB |
Output is correct |
10 |
Correct |
325 ms |
4944 KB |
Output is correct |
11 |
Correct |
330 ms |
4992 KB |
Output is correct |
12 |
Correct |
335 ms |
5156 KB |
Output is correct |
13 |
Correct |
36 ms |
4300 KB |
Output is correct |
14 |
Correct |
29 ms |
1616 KB |
Output is correct |
15 |
Correct |
219 ms |
6188 KB |
Output is correct |
16 |
Correct |
219 ms |
6196 KB |
Output is correct |
17 |
Correct |
218 ms |
6196 KB |
Output is correct |
18 |
Correct |
32 ms |
4180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
464 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
480 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
15 ms |
3160 KB |
Output is correct |
18 |
Correct |
16 ms |
3152 KB |
Output is correct |
19 |
Correct |
19 ms |
3000 KB |
Output is correct |
20 |
Correct |
15 ms |
3092 KB |
Output is correct |
21 |
Correct |
15 ms |
3100 KB |
Output is correct |
22 |
Correct |
16 ms |
3140 KB |
Output is correct |
23 |
Correct |
15 ms |
3200 KB |
Output is correct |
24 |
Correct |
15 ms |
2916 KB |
Output is correct |
25 |
Correct |
15 ms |
2968 KB |
Output is correct |
26 |
Correct |
15 ms |
3160 KB |
Output is correct |
27 |
Correct |
15 ms |
3160 KB |
Output is correct |
28 |
Correct |
15 ms |
3164 KB |
Output is correct |
29 |
Correct |
16 ms |
2912 KB |
Output is correct |
30 |
Correct |
17 ms |
2952 KB |
Output is correct |
31 |
Correct |
15 ms |
3096 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
0 ms |
344 KB |
Output is correct |
38 |
Correct |
332 ms |
5160 KB |
Output is correct |
39 |
Correct |
329 ms |
5136 KB |
Output is correct |
40 |
Correct |
325 ms |
4948 KB |
Output is correct |
41 |
Correct |
325 ms |
4944 KB |
Output is correct |
42 |
Correct |
330 ms |
4992 KB |
Output is correct |
43 |
Correct |
335 ms |
5156 KB |
Output is correct |
44 |
Correct |
36 ms |
4300 KB |
Output is correct |
45 |
Correct |
29 ms |
1616 KB |
Output is correct |
46 |
Correct |
219 ms |
6188 KB |
Output is correct |
47 |
Correct |
219 ms |
6196 KB |
Output is correct |
48 |
Correct |
218 ms |
6196 KB |
Output is correct |
49 |
Correct |
32 ms |
4180 KB |
Output is correct |
50 |
Correct |
327 ms |
7136 KB |
Output is correct |
51 |
Correct |
256 ms |
7060 KB |
Output is correct |
52 |
Correct |
238 ms |
7248 KB |
Output is correct |
53 |
Correct |
402 ms |
7136 KB |
Output is correct |
54 |
Correct |
382 ms |
7432 KB |
Output is correct |
55 |
Correct |
386 ms |
7360 KB |
Output is correct |
56 |
Correct |
55 ms |
6996 KB |
Output is correct |
57 |
Correct |
56 ms |
6936 KB |
Output is correct |
58 |
Correct |
55 ms |
6996 KB |
Output is correct |