#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
struct Sp{
vector <vector<int>> T;
int _s;
Sp(auto &a, int s){
_s = s;
T.resize(20);
int n = a.size();
for ( auto &x: T ){
x.resize(n);
}
T[0] = a;
for ( auto &u: T[0] ) u *= _s;
for ( int i = 1; i < 20; i++ ){
int k = 1 << i - 1;
for ( int j = 0; j < n; j++ ){
T[i][j] = min(T[i - 1][j], T[i - 1][min(n - 1, j + k)]);
}
}
}
int get(int l, int r){
int lg = __lg(r - l + 1), k = 1 << lg;
return min(T[lg][l], T[lg][r - k + 1]) * _s;
}
};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
vector <int> X(n);
for ( auto &u: X ) cin >> u;
int q; cin >> q;
vector <int> S(q);
for ( auto &u: S ) cin >> u;
vector <int> df(n - 1), df2(n - 1);
for ( int i = 0; i + 1 < n; i++ ){
df[i] = X[i + 1] * 2 - X[i];
df2[i] = X[i + 1] - X[i] * 2;
}
Sp sp(df, -1), ap(df2, -1);
for ( auto &xx: S ){
int it = lower_bound(all(X), xx) - X.begin();
if ( it - 1 >= 0 && (it == n || X[it] - xx >= xx - X[it - 1]) ) --it;
int ans = abs(X[it] - xx), l = it - 1, r = it + 1, x = X[it];
while ( l >= 0 || r < n ){
// cout << l + 1 << " " << r + 1 << " -> " << x << ", " << ans << ln;
if ( l < 0 ){
ans += abs(X.back() - x);
break;
} else if ( r >= n ){
ans += abs(X[0] - x);
break;
} else{
if ( abs(X[l] - x) <= abs(X[r] - x) ){
int tl = 0, tr = l;
while ( tl + 1 < tr ){
int md = (tl + tr) >> 1;
if ( sp.get(md, l - 1) - X[l] <= abs(X[r] - x) ){
tr = md;
} else tl = md;
}
ans += abs(X[tr] - x);
x = X[tr--]; l = tr;
} else{
int tl = r - 1, tr = n - 1;
while ( tl + 1 < tr ){
int md = (tl + tr) >> 1;
if ( ap.get(r, md) + X[r] < abs(X[l] - x) ){
tl = md;
} else tr = md;
} tl++;
ans += abs(X[tr] - x);
x = X[tr++]; r = tr;
}
}
}
cout << ans << ln;
}
cout << '\n';
}
Compilation message
travel.cpp:34:8: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
34 | Sp(auto &a, int s){
| ^~~~
travel.cpp: In instantiation of 'Sp::Sp(auto:23&, long long int) [with auto:23 = std::vector<long long int>]':
travel.cpp:71:17: required from here
travel.cpp:44:28: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
44 | int k = 1 << i - 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
46 ms |
67668 KB |
Output is correct |
18 |
Correct |
47 ms |
67664 KB |
Output is correct |
19 |
Correct |
47 ms |
67664 KB |
Output is correct |
20 |
Correct |
47 ms |
67668 KB |
Output is correct |
21 |
Correct |
46 ms |
67676 KB |
Output is correct |
22 |
Correct |
45 ms |
67828 KB |
Output is correct |
23 |
Correct |
46 ms |
67676 KB |
Output is correct |
24 |
Correct |
46 ms |
67632 KB |
Output is correct |
25 |
Correct |
47 ms |
67664 KB |
Output is correct |
26 |
Correct |
46 ms |
67816 KB |
Output is correct |
27 |
Correct |
46 ms |
67668 KB |
Output is correct |
28 |
Correct |
47 ms |
67664 KB |
Output is correct |
29 |
Correct |
46 ms |
67668 KB |
Output is correct |
30 |
Correct |
47 ms |
67664 KB |
Output is correct |
31 |
Correct |
47 ms |
67668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
106 ms |
70640 KB |
Output is correct |
8 |
Correct |
114 ms |
70640 KB |
Output is correct |
9 |
Correct |
112 ms |
70836 KB |
Output is correct |
10 |
Correct |
105 ms |
70740 KB |
Output is correct |
11 |
Correct |
106 ms |
70740 KB |
Output is correct |
12 |
Correct |
107 ms |
70736 KB |
Output is correct |
13 |
Correct |
29 ms |
3932 KB |
Output is correct |
14 |
Correct |
22 ms |
2548 KB |
Output is correct |
15 |
Correct |
130 ms |
71080 KB |
Output is correct |
16 |
Correct |
128 ms |
71052 KB |
Output is correct |
17 |
Correct |
136 ms |
71212 KB |
Output is correct |
18 |
Correct |
30 ms |
3928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
46 ms |
67668 KB |
Output is correct |
18 |
Correct |
47 ms |
67664 KB |
Output is correct |
19 |
Correct |
47 ms |
67664 KB |
Output is correct |
20 |
Correct |
47 ms |
67668 KB |
Output is correct |
21 |
Correct |
46 ms |
67676 KB |
Output is correct |
22 |
Correct |
45 ms |
67828 KB |
Output is correct |
23 |
Correct |
46 ms |
67676 KB |
Output is correct |
24 |
Correct |
46 ms |
67632 KB |
Output is correct |
25 |
Correct |
47 ms |
67664 KB |
Output is correct |
26 |
Correct |
46 ms |
67816 KB |
Output is correct |
27 |
Correct |
46 ms |
67668 KB |
Output is correct |
28 |
Correct |
47 ms |
67664 KB |
Output is correct |
29 |
Correct |
46 ms |
67668 KB |
Output is correct |
30 |
Correct |
47 ms |
67664 KB |
Output is correct |
31 |
Correct |
47 ms |
67668 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
600 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
106 ms |
70640 KB |
Output is correct |
39 |
Correct |
114 ms |
70640 KB |
Output is correct |
40 |
Correct |
112 ms |
70836 KB |
Output is correct |
41 |
Correct |
105 ms |
70740 KB |
Output is correct |
42 |
Correct |
106 ms |
70740 KB |
Output is correct |
43 |
Correct |
107 ms |
70736 KB |
Output is correct |
44 |
Correct |
29 ms |
3932 KB |
Output is correct |
45 |
Correct |
22 ms |
2548 KB |
Output is correct |
46 |
Correct |
130 ms |
71080 KB |
Output is correct |
47 |
Correct |
128 ms |
71052 KB |
Output is correct |
48 |
Correct |
136 ms |
71212 KB |
Output is correct |
49 |
Correct |
30 ms |
3928 KB |
Output is correct |
50 |
Correct |
344 ms |
71544 KB |
Output is correct |
51 |
Correct |
308 ms |
75244 KB |
Output is correct |
52 |
Correct |
277 ms |
75356 KB |
Output is correct |
53 |
Correct |
198 ms |
75344 KB |
Output is correct |
54 |
Correct |
216 ms |
75256 KB |
Output is correct |
55 |
Correct |
194 ms |
75348 KB |
Output is correct |
56 |
Correct |
100 ms |
75084 KB |
Output is correct |
57 |
Correct |
100 ms |
75092 KB |
Output is correct |
58 |
Correct |
100 ms |
75328 KB |
Output is correct |