#include <bits/stdc++.h>
#include <cassert>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 201010
#define MAXS 23
#define INF 1000000000000000001
#define bb ' '
#define ln '\n'
#define Ln '\n'
ll X[MAX];
ll ans[MAX];
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int N;
cin >> N;
int i;
for (i = 1; i <= N; i++) cin >> X[i];
int Q;
cin >> Q;
X[0] = -INF;
X[N + 1] = INF;
for (i = 1; i <= N; i++) {
int s, e, loc;
s = e = loc = i;
int l, r;
while (1) {
if (s == 1 && e == N) break;
if (X[loc] - X[s - 1] > X[e + 1] - X[loc]) {
ll d = X[loc] - X[s - 1];
l = e + 1;
r = N + 1;
while (r - l > 1) {
int m = l + r >> 1;
if (X[m] - X[loc] >= d) r = m;
else l = m;
}
ans[i] += X[l] - X[loc];
loc = e = l;
}
else {
ll d = X[e + 1] - X[loc];
l = 0;
r = s - 1;
while (r - l > 1) {
int m = l + r >> 1;
if (X[loc] - X[m] > d) l = m;
else r = m;
}
ans[i] += X[loc] - X[r];
loc = s = r;
}
}
}
while (Q--) {
ll x;
cin >> x;
int ind = lower_bound(X + 1, X + N + 1, x) - X;
int pv = ind - 1;
if (x - X[pv] <= X[ind] - x) ind = pv;
cout << abs(x - X[ind]) + ans[ind] << ln;
}
}
Compilation message
travel.cpp: In function 'int main()':
travel.cpp:40:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
40 | int m = l + r >> 1;
| ~~^~~
travel.cpp:52:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
52 | int m = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
324 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
712 KB |
Output is correct |
16 |
Correct |
2 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
324 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
712 KB |
Output is correct |
16 |
Correct |
2 ms |
364 KB |
Output is correct |
17 |
Correct |
304 ms |
5372 KB |
Output is correct |
18 |
Correct |
306 ms |
5364 KB |
Output is correct |
19 |
Correct |
309 ms |
5448 KB |
Output is correct |
20 |
Correct |
319 ms |
5344 KB |
Output is correct |
21 |
Correct |
327 ms |
5376 KB |
Output is correct |
22 |
Correct |
337 ms |
5320 KB |
Output is correct |
23 |
Correct |
328 ms |
5404 KB |
Output is correct |
24 |
Correct |
387 ms |
5344 KB |
Output is correct |
25 |
Correct |
387 ms |
5284 KB |
Output is correct |
26 |
Correct |
387 ms |
5368 KB |
Output is correct |
27 |
Correct |
408 ms |
5472 KB |
Output is correct |
28 |
Correct |
383 ms |
5324 KB |
Output is correct |
29 |
Correct |
203 ms |
5288 KB |
Output is correct |
30 |
Correct |
214 ms |
5244 KB |
Output is correct |
31 |
Correct |
201 ms |
5268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
360 ms |
7332 KB |
Output is correct |
8 |
Correct |
369 ms |
7344 KB |
Output is correct |
9 |
Correct |
360 ms |
7356 KB |
Output is correct |
10 |
Correct |
359 ms |
7276 KB |
Output is correct |
11 |
Correct |
366 ms |
7296 KB |
Output is correct |
12 |
Correct |
370 ms |
7312 KB |
Output is correct |
13 |
Correct |
35 ms |
4196 KB |
Output is correct |
14 |
Correct |
24 ms |
1480 KB |
Output is correct |
15 |
Correct |
459 ms |
8384 KB |
Output is correct |
16 |
Correct |
469 ms |
8328 KB |
Output is correct |
17 |
Correct |
460 ms |
8496 KB |
Output is correct |
18 |
Correct |
35 ms |
4172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
324 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
712 KB |
Output is correct |
16 |
Correct |
2 ms |
364 KB |
Output is correct |
17 |
Correct |
304 ms |
5372 KB |
Output is correct |
18 |
Correct |
306 ms |
5364 KB |
Output is correct |
19 |
Correct |
309 ms |
5448 KB |
Output is correct |
20 |
Correct |
319 ms |
5344 KB |
Output is correct |
21 |
Correct |
327 ms |
5376 KB |
Output is correct |
22 |
Correct |
337 ms |
5320 KB |
Output is correct |
23 |
Correct |
328 ms |
5404 KB |
Output is correct |
24 |
Correct |
387 ms |
5344 KB |
Output is correct |
25 |
Correct |
387 ms |
5284 KB |
Output is correct |
26 |
Correct |
387 ms |
5368 KB |
Output is correct |
27 |
Correct |
408 ms |
5472 KB |
Output is correct |
28 |
Correct |
383 ms |
5324 KB |
Output is correct |
29 |
Correct |
203 ms |
5288 KB |
Output is correct |
30 |
Correct |
214 ms |
5244 KB |
Output is correct |
31 |
Correct |
201 ms |
5268 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
360 ms |
7332 KB |
Output is correct |
39 |
Correct |
369 ms |
7344 KB |
Output is correct |
40 |
Correct |
360 ms |
7356 KB |
Output is correct |
41 |
Correct |
359 ms |
7276 KB |
Output is correct |
42 |
Correct |
366 ms |
7296 KB |
Output is correct |
43 |
Correct |
370 ms |
7312 KB |
Output is correct |
44 |
Correct |
35 ms |
4196 KB |
Output is correct |
45 |
Correct |
24 ms |
1480 KB |
Output is correct |
46 |
Correct |
459 ms |
8384 KB |
Output is correct |
47 |
Correct |
469 ms |
8328 KB |
Output is correct |
48 |
Correct |
460 ms |
8496 KB |
Output is correct |
49 |
Correct |
35 ms |
4172 KB |
Output is correct |
50 |
Correct |
369 ms |
9452 KB |
Output is correct |
51 |
Correct |
363 ms |
9412 KB |
Output is correct |
52 |
Correct |
394 ms |
9676 KB |
Output is correct |
53 |
Correct |
446 ms |
9432 KB |
Output is correct |
54 |
Correct |
449 ms |
9428 KB |
Output is correct |
55 |
Correct |
450 ms |
9456 KB |
Output is correct |
56 |
Correct |
258 ms |
9280 KB |
Output is correct |
57 |
Correct |
241 ms |
9276 KB |
Output is correct |
58 |
Correct |
285 ms |
9372 KB |
Output is correct |