#include<bits/stdc++.h>
using namespace std;
using I=int;
using Lli=long long int;
const I N=200000;
const Lli MAX=1e18+1;
const Lli MIN=-MAX;
Lli x_arr[N];
I main(){
cin.tie(0)->sync_with_stdio(0);
I n;cin>>n;
for(I i=0;i<n;i++)cin>>x_arr[i];
I q;cin>>q;
while(q--){
I s;cin>>s;
auto geq=[&](Lli x){
return lower_bound(x_arr,x_arr+n,x)-x_arr;
};
auto gtn=[&](Lli x){
return upper_bound(x_arr,x_arr+n,x)-x_arr;
};
auto ltn=[&](Lli x){
return geq(x)-1;
};
auto leq=[&](Lli x){
return gtn(x)-1;
};
auto val=[&](I i){
if(i==-1){
return MIN;
}
if(i==n){
return MAX;
}
return x_arr[i];
};
Lli res=0,l1=s,r1=s,t=s;
while(1){
Lli l2=val(ltn(l1)),r2=val(gtn(r1));
if(l2==MIN&&r2==MAX){
break;
}
if(t==l1){
if(l1-l2<=r2-l1){
I u=val(geq(l1-(r2-l1)));
res+=t-u;
t=u;
l1=u;
}else{
I u=val(ltn(l1+(l1-l2)));
res+=u-t;
t=u;
r1=u;
}
}else{
if(r1-l2<=r2-r1){
I u=val(geq(r1-(r2-r1)));
res+=t-u;
t=u;
l1=u;
}else{
I u=val(ltn(r1+(r1-l2)));
res+=u-t;
t=u;
r1=u;
}
}
}
printf("%lli\n",res);
}
}
Compilation message
travel.cpp: In function 'I main()':
travel.cpp:25:10: warning: variable 'leq' set but not used [-Wunused-but-set-variable]
25 | auto leq=[&](Lli x){
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
540 KB |
Output is correct |
10 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
540 KB |
Output is correct |
10 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
504 KB |
Output is correct |
17 |
Correct |
16 ms |
3944 KB |
Output is correct |
18 |
Correct |
18 ms |
3972 KB |
Output is correct |
19 |
Correct |
15 ms |
3728 KB |
Output is correct |
20 |
Correct |
15 ms |
3932 KB |
Output is correct |
21 |
Correct |
15 ms |
3932 KB |
Output is correct |
22 |
Correct |
16 ms |
3920 KB |
Output is correct |
23 |
Correct |
15 ms |
3924 KB |
Output is correct |
24 |
Correct |
16 ms |
3924 KB |
Output is correct |
25 |
Correct |
17 ms |
3924 KB |
Output is correct |
26 |
Correct |
15 ms |
3928 KB |
Output is correct |
27 |
Correct |
16 ms |
3924 KB |
Output is correct |
28 |
Correct |
15 ms |
3828 KB |
Output is correct |
29 |
Correct |
15 ms |
3676 KB |
Output is correct |
30 |
Correct |
15 ms |
3676 KB |
Output is correct |
31 |
Correct |
15 ms |
3732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
713 ms |
5932 KB |
Output is correct |
8 |
Correct |
724 ms |
5900 KB |
Output is correct |
9 |
Correct |
718 ms |
5740 KB |
Output is correct |
10 |
Correct |
720 ms |
5716 KB |
Output is correct |
11 |
Correct |
719 ms |
5972 KB |
Output is correct |
12 |
Correct |
714 ms |
5724 KB |
Output is correct |
13 |
Correct |
37 ms |
4180 KB |
Output is correct |
14 |
Correct |
34 ms |
1636 KB |
Output is correct |
15 |
Correct |
466 ms |
6992 KB |
Output is correct |
16 |
Correct |
471 ms |
6792 KB |
Output is correct |
17 |
Correct |
474 ms |
6996 KB |
Output is correct |
18 |
Correct |
39 ms |
4176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
540 KB |
Output is correct |
10 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
504 KB |
Output is correct |
17 |
Correct |
16 ms |
3944 KB |
Output is correct |
18 |
Correct |
18 ms |
3972 KB |
Output is correct |
19 |
Correct |
15 ms |
3728 KB |
Output is correct |
20 |
Correct |
15 ms |
3932 KB |
Output is correct |
21 |
Correct |
15 ms |
3932 KB |
Output is correct |
22 |
Correct |
16 ms |
3920 KB |
Output is correct |
23 |
Correct |
15 ms |
3924 KB |
Output is correct |
24 |
Correct |
16 ms |
3924 KB |
Output is correct |
25 |
Correct |
17 ms |
3924 KB |
Output is correct |
26 |
Correct |
15 ms |
3928 KB |
Output is correct |
27 |
Correct |
16 ms |
3924 KB |
Output is correct |
28 |
Correct |
15 ms |
3828 KB |
Output is correct |
29 |
Correct |
15 ms |
3676 KB |
Output is correct |
30 |
Correct |
15 ms |
3676 KB |
Output is correct |
31 |
Correct |
15 ms |
3732 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
713 ms |
5932 KB |
Output is correct |
39 |
Correct |
724 ms |
5900 KB |
Output is correct |
40 |
Correct |
718 ms |
5740 KB |
Output is correct |
41 |
Correct |
720 ms |
5716 KB |
Output is correct |
42 |
Correct |
719 ms |
5972 KB |
Output is correct |
43 |
Correct |
714 ms |
5724 KB |
Output is correct |
44 |
Correct |
37 ms |
4180 KB |
Output is correct |
45 |
Correct |
34 ms |
1636 KB |
Output is correct |
46 |
Correct |
466 ms |
6992 KB |
Output is correct |
47 |
Correct |
471 ms |
6792 KB |
Output is correct |
48 |
Correct |
474 ms |
6996 KB |
Output is correct |
49 |
Correct |
39 ms |
4176 KB |
Output is correct |
50 |
Correct |
736 ms |
8016 KB |
Output is correct |
51 |
Correct |
614 ms |
8040 KB |
Output is correct |
52 |
Correct |
567 ms |
8016 KB |
Output is correct |
53 |
Correct |
866 ms |
8004 KB |
Output is correct |
54 |
Correct |
888 ms |
8040 KB |
Output is correct |
55 |
Correct |
889 ms |
7896 KB |
Output is correct |
56 |
Correct |
92 ms |
7764 KB |
Output is correct |
57 |
Correct |
112 ms |
7764 KB |
Output is correct |
58 |
Correct |
91 ms |
7764 KB |
Output is correct |