#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 2e5;
int N, A[MAXN+10], Q;
struct SEG1
{
int tree[MAXN*4+10];
void init(int node, int tl, int tr)
{
if(tl==tr)
{
tree[node]=A[tl]+A[tl]-A[tl+1];
return;
}
int mid=tl+tr>>1;
init(node*2, tl, mid);
init(node*2+1, mid+1, tr);
tree[node]=min(tree[node*2], tree[node*2+1]);
}
int query(int node, int tl, int tr, int l, int r, int k)
{
if(r<tl || tr<l) return -1;
if(l<=tl && tr<=r && tree[node]>k) return -1;
if(tl==tr) return tl;
int mid=tl+tr>>1, ret;
ret=query(node*2, tl, mid, l, r, k);
if(ret!=-1) return ret;
ret=query(node*2+1, mid+1, tr, l, r, k);
return ret;
}
}seg1;
struct SEG2
{
int tree[MAXN*4+10];
void init(int node, int tl, int tr)
{
if(tl==tr)
{
tree[node]=A[tl]+A[tl]-A[tl-1];
return;
}
int mid=tl+tr>>1;
init(node*2, tl, mid);
init(node*2+1, mid+1, tr);
tree[node]=max(tree[node*2], tree[node*2+1]);
}
int query(int node, int tl, int tr, int l, int r, int k)
{
if(r<tl || tr<l) return -1;
if(l<=tl && tr<=r && tree[node]<=k) return -1;
if(tl==tr) return tl;
int mid=tl+tr>>1, ret;
ret=query(node*2+1, mid+1, tr, l, r, k);
if(ret!=-1) return ret;
ret=query(node*2, tl, mid, l, r, k);
return ret;
}
}seg2;
int main()
{
scanf("%d", &N);
for(int i=1; i<=N; i++) scanf("%d", &A[i]);
A[0]=-1e9+100; A[N+1]=2e9+100;
seg1.init(1, 1, N);
seg2.init(1, 1, N);
scanf("%d", &Q);
while(Q--)
{
int x;
ll ans=0;
scanf("%d", &x);
int t=lower_bound(A+1, A+N+1, x)-A;
int l, r;
if(x-A[t-1]<=A[t]-x) l=r=t-1, ans+=x-A[t-1];
else l=r=t, ans+=A[t]-x;
int d=0;
while(!(l==1 && r==N))
{
//printf("%d %d : %lld\n", l, r, ans);
if(l==1 || r==N) { ans+=A[N]-A[1]; break; }
else if(d==0)
{
int t=seg1.query(1, 1, N, r, N, A[l-1]);
if(t!=-1)
{
r=t;
ans+=A[r]-A[l];
}
}
else
{
int t=seg2.query(1, 1, N, 1, l, A[r+1]);
if(t!=-1)
{
l=t;
ans+=A[r]-A[l];
}
}
d=1-d;
}
printf("%lld\n", ans);
}
}
Compilation message
travel.cpp: In member function 'void SEG1::init(int, int, int)':
travel.cpp:22:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
22 | int mid=tl+tr>>1;
| ~~^~~
travel.cpp: In member function 'int SEG1::query(int, int, int, int, int, int)':
travel.cpp:32:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
32 | int mid=tl+tr>>1, ret;
| ~~^~~
travel.cpp: In member function 'void SEG2::init(int, int, int)':
travel.cpp:50:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
50 | int mid=tl+tr>>1;
| ~~^~~
travel.cpp: In member function 'int SEG2::query(int, int, int, int, int, int)':
travel.cpp:60:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
60 | int mid=tl+tr>>1, ret;
| ~~^~~
travel.cpp: In function 'int main()':
travel.cpp:70:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
travel.cpp:71:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | for(int i=1; i<=N; i++) scanf("%d", &A[i]);
| ~~~~~^~~~~~~~~~~~~
travel.cpp:77:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | scanf("%d", &Q);
| ~~~~~^~~~~~~~~~
travel.cpp:82:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | scanf("%d", &x);
| ~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
308 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
308 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
26 ms |
6284 KB |
Output is correct |
18 |
Correct |
26 ms |
6572 KB |
Output is correct |
19 |
Correct |
35 ms |
6712 KB |
Output is correct |
20 |
Correct |
27 ms |
6636 KB |
Output is correct |
21 |
Correct |
26 ms |
6676 KB |
Output is correct |
22 |
Correct |
30 ms |
6876 KB |
Output is correct |
23 |
Correct |
25 ms |
6412 KB |
Output is correct |
24 |
Correct |
32 ms |
6476 KB |
Output is correct |
25 |
Correct |
26 ms |
6688 KB |
Output is correct |
26 |
Correct |
28 ms |
6732 KB |
Output is correct |
27 |
Correct |
26 ms |
6636 KB |
Output is correct |
28 |
Correct |
26 ms |
6732 KB |
Output is correct |
29 |
Correct |
26 ms |
6460 KB |
Output is correct |
30 |
Correct |
25 ms |
6568 KB |
Output is correct |
31 |
Correct |
24 ms |
6620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
161 ms |
8848 KB |
Output is correct |
8 |
Correct |
159 ms |
8804 KB |
Output is correct |
9 |
Correct |
170 ms |
8508 KB |
Output is correct |
10 |
Correct |
155 ms |
8848 KB |
Output is correct |
11 |
Correct |
187 ms |
8872 KB |
Output is correct |
12 |
Correct |
164 ms |
8752 KB |
Output is correct |
13 |
Correct |
57 ms |
3988 KB |
Output is correct |
14 |
Correct |
38 ms |
1356 KB |
Output is correct |
15 |
Correct |
133 ms |
9704 KB |
Output is correct |
16 |
Correct |
150 ms |
9664 KB |
Output is correct |
17 |
Correct |
130 ms |
9804 KB |
Output is correct |
18 |
Correct |
57 ms |
3676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
308 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
26 ms |
6284 KB |
Output is correct |
18 |
Correct |
26 ms |
6572 KB |
Output is correct |
19 |
Correct |
35 ms |
6712 KB |
Output is correct |
20 |
Correct |
27 ms |
6636 KB |
Output is correct |
21 |
Correct |
26 ms |
6676 KB |
Output is correct |
22 |
Correct |
30 ms |
6876 KB |
Output is correct |
23 |
Correct |
25 ms |
6412 KB |
Output is correct |
24 |
Correct |
32 ms |
6476 KB |
Output is correct |
25 |
Correct |
26 ms |
6688 KB |
Output is correct |
26 |
Correct |
28 ms |
6732 KB |
Output is correct |
27 |
Correct |
26 ms |
6636 KB |
Output is correct |
28 |
Correct |
26 ms |
6732 KB |
Output is correct |
29 |
Correct |
26 ms |
6460 KB |
Output is correct |
30 |
Correct |
25 ms |
6568 KB |
Output is correct |
31 |
Correct |
24 ms |
6620 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
304 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
161 ms |
8848 KB |
Output is correct |
39 |
Correct |
159 ms |
8804 KB |
Output is correct |
40 |
Correct |
170 ms |
8508 KB |
Output is correct |
41 |
Correct |
155 ms |
8848 KB |
Output is correct |
42 |
Correct |
187 ms |
8872 KB |
Output is correct |
43 |
Correct |
164 ms |
8752 KB |
Output is correct |
44 |
Correct |
57 ms |
3988 KB |
Output is correct |
45 |
Correct |
38 ms |
1356 KB |
Output is correct |
46 |
Correct |
133 ms |
9704 KB |
Output is correct |
47 |
Correct |
150 ms |
9664 KB |
Output is correct |
48 |
Correct |
130 ms |
9804 KB |
Output is correct |
49 |
Correct |
57 ms |
3676 KB |
Output is correct |
50 |
Correct |
260 ms |
11212 KB |
Output is correct |
51 |
Correct |
283 ms |
10480 KB |
Output is correct |
52 |
Correct |
242 ms |
11188 KB |
Output is correct |
53 |
Correct |
198 ms |
10752 KB |
Output is correct |
54 |
Correct |
194 ms |
11220 KB |
Output is correct |
55 |
Correct |
232 ms |
10644 KB |
Output is correct |
56 |
Correct |
129 ms |
10424 KB |
Output is correct |
57 |
Correct |
131 ms |
10796 KB |
Output is correct |
58 |
Correct |
124 ms |
10724 KB |
Output is correct |