#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 5;
int n, x[N];
int q;
int Max[N][20];
int Min[N][20];
int LG[N];
void prepare(){
for (int i = n; i >= 1; -- i){
Max[i][0] = 2 * x[i] - x[i - 1];
Min[i][0] = 2 * x[i] - x[i + 1];
for (int j = 1; j < 20; ++ j){
int u = i + (1 << (j - 1));
if (u > n) break;
Max[i][j] = max(Max[i][j - 1], Max[u][j - 1]);
Min[i][j] = min(Min[i][j - 1], Min[u][j - 1]);
}
LG[i] = log2(i);
}
}
int getmax(int l, int r){
int lg = LG[r - l + 1];
return max(Max[l][lg], Max[r - (1 << lg) + 1][lg]);
}
int getmin(int l, int r){
int lg = LG[r - l + 1];
return min(Min[l][lg], Min[r - (1 << lg) + 1][lg]);
}
ll calc(int l, int p, int r){
if (l == 0) return x[n] - p;
if (r == n + 1) return p - x[1];
//2 * x - x-1 > r - x
if (p - x[l] <= x[r] - p){
// 2 * x[k] - x[k - 1] > x[r];
int L = 2;
int R = l;
int ans = 1;
while (L <= R){
int mid = L + R >> 1;
if (getmax(mid, l) > x[r]){
ans = mid;
L = mid + 1;
}
else R = mid - 1;
}
//ans = l;
return calc(ans - 1, x[ans], r) + p - x[ans];
}
// 2 * x[k] - x[k + 1] <= x[l]
int L = r;
int R = n - 1;
int ans = n;
while (L <= R){
int mid = L + R >> 1;
if (getmin(r, mid) <= x[l]){
R = mid - 1;
ans = mid;
}
else L = mid + 1;
}
//ans = r;
return calc(l, x[ans], ans + 1) + x[ans] - p;
}
void solve(){
cin >> n;
for (int i = 1; i <= n; ++ i) cin >> x[i];
prepare();
cin >> q;
while (q --){
int k; cin >> k;
int r = lower_bound(x + 1, x + n + 1, k) - x;
int l = r - 1;
if (x[r] == k) ++ r;
cout << calc(l, k, r) << '\n';
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
Compilation message
travel.cpp: In function 'long long int calc(int, int, int)':
travel.cpp:53:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
53 | int mid = L + R >> 1;
| ~~^~~
travel.cpp:73:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
73 | int mid = L + R >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
37 ms |
33104 KB |
Output is correct |
18 |
Correct |
39 ms |
33196 KB |
Output is correct |
19 |
Correct |
38 ms |
33112 KB |
Output is correct |
20 |
Correct |
39 ms |
33104 KB |
Output is correct |
21 |
Correct |
49 ms |
33664 KB |
Output is correct |
22 |
Correct |
38 ms |
33360 KB |
Output is correct |
23 |
Correct |
38 ms |
33108 KB |
Output is correct |
24 |
Correct |
39 ms |
33104 KB |
Output is correct |
25 |
Correct |
41 ms |
33104 KB |
Output is correct |
26 |
Correct |
40 ms |
33200 KB |
Output is correct |
27 |
Correct |
40 ms |
33116 KB |
Output is correct |
28 |
Correct |
39 ms |
33104 KB |
Output is correct |
29 |
Correct |
39 ms |
33116 KB |
Output is correct |
30 |
Correct |
37 ms |
33236 KB |
Output is correct |
31 |
Correct |
39 ms |
33104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4576 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
130 ms |
34660 KB |
Output is correct |
8 |
Correct |
118 ms |
37184 KB |
Output is correct |
9 |
Correct |
109 ms |
37204 KB |
Output is correct |
10 |
Correct |
125 ms |
37036 KB |
Output is correct |
11 |
Correct |
127 ms |
37204 KB |
Output is correct |
12 |
Correct |
111 ms |
37208 KB |
Output is correct |
13 |
Correct |
33 ms |
8276 KB |
Output is correct |
14 |
Correct |
31 ms |
5716 KB |
Output is correct |
15 |
Correct |
146 ms |
38224 KB |
Output is correct |
16 |
Correct |
144 ms |
38224 KB |
Output is correct |
17 |
Correct |
137 ms |
38224 KB |
Output is correct |
18 |
Correct |
40 ms |
8276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
37 ms |
33104 KB |
Output is correct |
18 |
Correct |
39 ms |
33196 KB |
Output is correct |
19 |
Correct |
38 ms |
33112 KB |
Output is correct |
20 |
Correct |
39 ms |
33104 KB |
Output is correct |
21 |
Correct |
49 ms |
33664 KB |
Output is correct |
22 |
Correct |
38 ms |
33360 KB |
Output is correct |
23 |
Correct |
38 ms |
33108 KB |
Output is correct |
24 |
Correct |
39 ms |
33104 KB |
Output is correct |
25 |
Correct |
41 ms |
33104 KB |
Output is correct |
26 |
Correct |
40 ms |
33200 KB |
Output is correct |
27 |
Correct |
40 ms |
33116 KB |
Output is correct |
28 |
Correct |
39 ms |
33104 KB |
Output is correct |
29 |
Correct |
39 ms |
33116 KB |
Output is correct |
30 |
Correct |
37 ms |
33236 KB |
Output is correct |
31 |
Correct |
39 ms |
33104 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4576 KB |
Output is correct |
34 |
Correct |
1 ms |
4444 KB |
Output is correct |
35 |
Correct |
1 ms |
4444 KB |
Output is correct |
36 |
Correct |
1 ms |
4440 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
130 ms |
34660 KB |
Output is correct |
39 |
Correct |
118 ms |
37184 KB |
Output is correct |
40 |
Correct |
109 ms |
37204 KB |
Output is correct |
41 |
Correct |
125 ms |
37036 KB |
Output is correct |
42 |
Correct |
127 ms |
37204 KB |
Output is correct |
43 |
Correct |
111 ms |
37208 KB |
Output is correct |
44 |
Correct |
33 ms |
8276 KB |
Output is correct |
45 |
Correct |
31 ms |
5716 KB |
Output is correct |
46 |
Correct |
146 ms |
38224 KB |
Output is correct |
47 |
Correct |
144 ms |
38224 KB |
Output is correct |
48 |
Correct |
137 ms |
38224 KB |
Output is correct |
49 |
Correct |
40 ms |
8276 KB |
Output is correct |
50 |
Correct |
347 ms |
39256 KB |
Output is correct |
51 |
Correct |
320 ms |
39640 KB |
Output is correct |
52 |
Correct |
283 ms |
39252 KB |
Output is correct |
53 |
Correct |
229 ms |
39224 KB |
Output is correct |
54 |
Correct |
242 ms |
39376 KB |
Output is correct |
55 |
Correct |
227 ms |
39508 KB |
Output is correct |
56 |
Correct |
87 ms |
39252 KB |
Output is correct |
57 |
Correct |
105 ms |
39252 KB |
Output is correct |
58 |
Correct |
95 ms |
39500 KB |
Output is correct |