#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], 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 - 1;
int ans = 1;
while (L <= R){
int mid = L + R >> 1;
if (getmax(mid, l - 1) > x[r]){
ans = mid;
L = mid + 1;
}
else R = mid - 1;
}
return calc(ans - 1, x[ans], r) + p - x[ans];
}
// 2 * x[k] - x[k + 1] <= x[l]
int L = r + 1;
int R = n - 1;
int ans = n;
while (L <= R){
int mid = L + R >> 1;
if (getmin(r + 1, mid) <= x[l]){
R = mid - 1;
ans = mid;
}
else L = mid + 1;
}
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';
}
}
int 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:51:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
51 | int mid = L + R >> 1;
| ~~^~~
travel.cpp:69:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
69 | int mid = L + R >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6624 KB |
Output is correct |
7 |
Correct |
2 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 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6624 KB |
Output is correct |
7 |
Correct |
2 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 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 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 |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4564 KB |
Output is correct |
7 |
Incorrect |
104 ms |
37100 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6624 KB |
Output is correct |
7 |
Correct |
2 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 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |