#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define int long long
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
int N, Q, X[200005], Y[200005], Z[200005];
struct node0 {
node0 *left, *right;
int S, E, val;
node0(int _s, int _e) : S(_s), E(_e) {
if (S == E) {
val = Z[S];
return;
}
int M = (S + E) >> 1;
left = new node0(S, M);
right = new node0(M + 1, E);
val = max(left->val, right->val);
}
int qry(int p, int v) {
if (p < S) return 0;
if (E <= p) {
if (val < v) return 0;
if (S == E) return S;
if (right->val >= v) return right->qry(p, v);
else return left->qry(p, v);
}
auto t = right->qry(p, v);
if (t) return t;
return left->qry(p, v);
}
} *root0;
struct node1 {
node1 *left, *right;
int S, E, val;
node1(int _s, int _e) : S(_s), E(_e) {
if (S == E) {
val = Y[S];
return;
}
int M = (S + E) >> 1;
left = new node1(S, M);
right = new node1(M + 1, E);
val = max(left->val, right->val);
}
int qry(int p, int v) {
if (p > E) return N + 1;
if (p <= S) {
if (val < v) return N + 1;
if (S == E) return S;
if (left->val >= v) return left->qry(p, v);
else return right->qry(p, v);
}
auto t = left->qry(p, v);
if (t != N + 1) return t;
return right->qry(p, v);
}
} *root1;
main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> X[i];
}
for (int i = 1; i <= N; i++) {
Y[i] = X[i] - 2 * X[i - 1];
Z[i] = 2 * X[i + 1] - X[i];
}
root0 = new node0(1, N);
root1 = new node1(1, N);
cin >> Q;
for (int S; Q--; ) {
cin >> S;
int cur = 0;
int pos = lower_bound(X + 1, X + 1 + N, S) - X;
int x = (int)-1e16, y = (int)1e16;
if (pos <= N) {
y = X[pos];
}
if (pos > 1) {
x = X[pos - 1];
}
cur += min(S - x, y - S);
if (S - x <= y - S) {
S = pos - 1;
} else {
S = pos;
}
int l = S, r = S;
//~ cout << "@ " << S << '\n';
while (l > 1 || r < N) {
// decide which direction
int x = (l == 1 ? (int)1e16 : llabs(X[S] - X[l - 1]));
int y = (r == N ? (int)1e16 : llabs(X[S] - X[r + 1]));
if (x <= y) {
// left
// find maximum j < l such that
// Z[j] >= y + X[S] + 1
//~ int j = root[0]->qry(1, l - 1, y + X[S] + 1) + 1;
int j = root0->qry(l - 1, y + X[S] + 1) + 1;
// l --> j
cur += X[S] - X[j];
l = S = j;
} else {
// right
// find minimum j > r such that
// Y[j] >= x - X[S]
//~ int j = root[1]->qry(r + 1, N, x - X[S]) - 1;
int j = root1->qry(r + 1, x - X[S]) - 1;
// r --> j
cur += X[j] - X[S];
r = S = j;
}
//~ cout << "@ " << S << ' ' << l << ' ' << r << ' ' << cur << '\n';
}
cout << cur << '\n';
}
}
Compilation message
travel.cpp:68:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
68 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
52 ms |
42572 KB |
Output is correct |
18 |
Correct |
52 ms |
42444 KB |
Output is correct |
19 |
Correct |
63 ms |
42488 KB |
Output is correct |
20 |
Correct |
53 ms |
42524 KB |
Output is correct |
21 |
Correct |
53 ms |
42464 KB |
Output is correct |
22 |
Correct |
51 ms |
42508 KB |
Output is correct |
23 |
Correct |
52 ms |
42492 KB |
Output is correct |
24 |
Correct |
55 ms |
42460 KB |
Output is correct |
25 |
Correct |
51 ms |
42480 KB |
Output is correct |
26 |
Correct |
52 ms |
42456 KB |
Output is correct |
27 |
Correct |
54 ms |
42544 KB |
Output is correct |
28 |
Correct |
53 ms |
42508 KB |
Output is correct |
29 |
Correct |
50 ms |
42560 KB |
Output is correct |
30 |
Correct |
51 ms |
42520 KB |
Output is correct |
31 |
Correct |
53 ms |
42564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
322 ms |
43888 KB |
Output is correct |
8 |
Correct |
347 ms |
43888 KB |
Output is correct |
9 |
Correct |
343 ms |
43980 KB |
Output is correct |
10 |
Correct |
326 ms |
43980 KB |
Output is correct |
11 |
Correct |
324 ms |
43980 KB |
Output is correct |
12 |
Correct |
340 ms |
43908 KB |
Output is correct |
13 |
Correct |
37 ms |
2260 KB |
Output is correct |
14 |
Correct |
26 ms |
852 KB |
Output is correct |
15 |
Correct |
247 ms |
44376 KB |
Output is correct |
16 |
Correct |
255 ms |
47148 KB |
Output is correct |
17 |
Correct |
254 ms |
47292 KB |
Output is correct |
18 |
Correct |
40 ms |
3740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
52 ms |
42572 KB |
Output is correct |
18 |
Correct |
52 ms |
42444 KB |
Output is correct |
19 |
Correct |
63 ms |
42488 KB |
Output is correct |
20 |
Correct |
53 ms |
42524 KB |
Output is correct |
21 |
Correct |
53 ms |
42464 KB |
Output is correct |
22 |
Correct |
51 ms |
42508 KB |
Output is correct |
23 |
Correct |
52 ms |
42492 KB |
Output is correct |
24 |
Correct |
55 ms |
42460 KB |
Output is correct |
25 |
Correct |
51 ms |
42480 KB |
Output is correct |
26 |
Correct |
52 ms |
42456 KB |
Output is correct |
27 |
Correct |
54 ms |
42544 KB |
Output is correct |
28 |
Correct |
53 ms |
42508 KB |
Output is correct |
29 |
Correct |
50 ms |
42560 KB |
Output is correct |
30 |
Correct |
51 ms |
42520 KB |
Output is correct |
31 |
Correct |
53 ms |
42564 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
322 ms |
43888 KB |
Output is correct |
39 |
Correct |
347 ms |
43888 KB |
Output is correct |
40 |
Correct |
343 ms |
43980 KB |
Output is correct |
41 |
Correct |
326 ms |
43980 KB |
Output is correct |
42 |
Correct |
324 ms |
43980 KB |
Output is correct |
43 |
Correct |
340 ms |
43908 KB |
Output is correct |
44 |
Correct |
37 ms |
2260 KB |
Output is correct |
45 |
Correct |
26 ms |
852 KB |
Output is correct |
46 |
Correct |
247 ms |
44376 KB |
Output is correct |
47 |
Correct |
255 ms |
47148 KB |
Output is correct |
48 |
Correct |
254 ms |
47292 KB |
Output is correct |
49 |
Correct |
40 ms |
3740 KB |
Output is correct |
50 |
Correct |
498 ms |
48848 KB |
Output is correct |
51 |
Correct |
410 ms |
47896 KB |
Output is correct |
52 |
Correct |
376 ms |
48620 KB |
Output is correct |
53 |
Correct |
403 ms |
48196 KB |
Output is correct |
54 |
Correct |
390 ms |
48548 KB |
Output is correct |
55 |
Correct |
413 ms |
48056 KB |
Output is correct |
56 |
Correct |
129 ms |
47932 KB |
Output is correct |
57 |
Correct |
130 ms |
48424 KB |
Output is correct |
58 |
Correct |
131 ms |
48120 KB |
Output is correct |