#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
const int nax = 200005;
int N, Q;
i64 X[nax];
void solve(int p) {
int l = 0, r = N + 1, res = -1;
while (l <= r) {
int mid = (l + r) >> 1;
if (X[mid] <= p) res = mid, l = mid + 1;
else r = mid - 1;
}
int L = res;
l = 0, r = N + 1, res = -1;
while (l <= r) {
int mid = (l + r) >> 1;
if (X[mid] > p) res = mid, r = mid - 1;
else l = mid + 1;
}
int R = res;
i64 ans = 0, cur = p;
// cout << "first : " << L << ' ' << R << ' ' << ans << '\n';
if (abs(cur - X[L]) <= abs(X[R] - cur)) {
ans += abs(cur - X[L]);
cur = X[L];
L -= 1;
}
else {
ans += abs(cur - X[R]);
cur = X[R];
R += 1;
}
// cout << "second : " << L << ' ' << R << ' ' << ans << '\n';
while (L >= 1 && R <= N) {
// cout << L << ' ' << R << ' ' << ans << ' ' << cur << '\n';
// cout << abs(cur - X[L]) << ' ' << abs(X[R] - cur) << '\n';
if (abs(cur - X[L]) <= abs(X[R] - cur)) {
i64 dis = abs(X[R] - cur);
l = 0, r = N + 1, res = -1;
while (l <= r) {
int mid = (l + r) >> 1;
if (X[mid] >= cur - dis) {
res = mid;
r = mid - 1;
}
else l = mid + 1;
}
if (res == 0) res = 1;
ans += abs(cur - X[res]);
cur = X[res];
L = res - 1;
}
else {
i64 dis = abs(X[L] - cur);
l = 0, r = N + 1, res = -1;
while (l <= r) {
int mid = (l + r) >> 1;
if (X[mid] <= cur + dis) {
res = mid;
l = mid + 1;
}
else r = mid - 1;
}
if (res == N + 1) res = N;
ans += abs(cur - X[res]);
cur = X[res];
R = res + 1;
}
if (L == 0 || R == N + 1) break;
}
ans += abs(cur - X[1]);
ans += abs(cur - X[N]);
cout << ans << '\n';
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin >> N;
for (int i = 1; i <= N; i++) cin >> X[i];
X[0] = -1E18; X[N + 1] = 1E18;
cin >> Q;
while (Q--) {
int o; cin >> o;
solve(o);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
500 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
500 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
16 ms |
3968 KB |
Output is correct |
18 |
Correct |
16 ms |
3932 KB |
Output is correct |
19 |
Correct |
15 ms |
4108 KB |
Output is correct |
20 |
Correct |
25 ms |
3780 KB |
Output is correct |
21 |
Correct |
20 ms |
3920 KB |
Output is correct |
22 |
Correct |
15 ms |
3852 KB |
Output is correct |
23 |
Correct |
25 ms |
3904 KB |
Output is correct |
24 |
Correct |
18 ms |
3716 KB |
Output is correct |
25 |
Correct |
21 ms |
3796 KB |
Output is correct |
26 |
Correct |
15 ms |
3720 KB |
Output is correct |
27 |
Correct |
24 ms |
3932 KB |
Output is correct |
28 |
Correct |
22 ms |
3932 KB |
Output is correct |
29 |
Correct |
16 ms |
3640 KB |
Output is correct |
30 |
Correct |
22 ms |
3676 KB |
Output is correct |
31 |
Correct |
21 ms |
3696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
364 ms |
5932 KB |
Output is correct |
8 |
Correct |
379 ms |
6048 KB |
Output is correct |
9 |
Correct |
379 ms |
5956 KB |
Output is correct |
10 |
Correct |
390 ms |
5808 KB |
Output is correct |
11 |
Correct |
392 ms |
5712 KB |
Output is correct |
12 |
Correct |
367 ms |
5972 KB |
Output is correct |
13 |
Correct |
37 ms |
4180 KB |
Output is correct |
14 |
Correct |
35 ms |
1516 KB |
Output is correct |
15 |
Correct |
261 ms |
7008 KB |
Output is correct |
16 |
Correct |
247 ms |
6996 KB |
Output is correct |
17 |
Correct |
256 ms |
6944 KB |
Output is correct |
18 |
Correct |
48 ms |
4140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
500 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
16 ms |
3968 KB |
Output is correct |
18 |
Correct |
16 ms |
3932 KB |
Output is correct |
19 |
Correct |
15 ms |
4108 KB |
Output is correct |
20 |
Correct |
25 ms |
3780 KB |
Output is correct |
21 |
Correct |
20 ms |
3920 KB |
Output is correct |
22 |
Correct |
15 ms |
3852 KB |
Output is correct |
23 |
Correct |
25 ms |
3904 KB |
Output is correct |
24 |
Correct |
18 ms |
3716 KB |
Output is correct |
25 |
Correct |
21 ms |
3796 KB |
Output is correct |
26 |
Correct |
15 ms |
3720 KB |
Output is correct |
27 |
Correct |
24 ms |
3932 KB |
Output is correct |
28 |
Correct |
22 ms |
3932 KB |
Output is correct |
29 |
Correct |
16 ms |
3640 KB |
Output is correct |
30 |
Correct |
22 ms |
3676 KB |
Output is correct |
31 |
Correct |
21 ms |
3696 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
0 ms |
344 KB |
Output is correct |
38 |
Correct |
364 ms |
5932 KB |
Output is correct |
39 |
Correct |
379 ms |
6048 KB |
Output is correct |
40 |
Correct |
379 ms |
5956 KB |
Output is correct |
41 |
Correct |
390 ms |
5808 KB |
Output is correct |
42 |
Correct |
392 ms |
5712 KB |
Output is correct |
43 |
Correct |
367 ms |
5972 KB |
Output is correct |
44 |
Correct |
37 ms |
4180 KB |
Output is correct |
45 |
Correct |
35 ms |
1516 KB |
Output is correct |
46 |
Correct |
261 ms |
7008 KB |
Output is correct |
47 |
Correct |
247 ms |
6996 KB |
Output is correct |
48 |
Correct |
256 ms |
6944 KB |
Output is correct |
49 |
Correct |
48 ms |
4140 KB |
Output is correct |
50 |
Correct |
360 ms |
8020 KB |
Output is correct |
51 |
Correct |
279 ms |
7916 KB |
Output is correct |
52 |
Correct |
276 ms |
8100 KB |
Output is correct |
53 |
Correct |
446 ms |
7868 KB |
Output is correct |
54 |
Correct |
438 ms |
8072 KB |
Output is correct |
55 |
Correct |
426 ms |
8048 KB |
Output is correct |
56 |
Correct |
62 ms |
7760 KB |
Output is correct |
57 |
Correct |
59 ms |
7924 KB |
Output is correct |
58 |
Correct |
63 ms |
7896 KB |
Output is correct |