#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("Ofast")
using namespace std;
using namespace __gnu_pbds;
#define lg long long
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int main()
{
fastio;
lg n;
cin >> n;
vector<lg> v(n+2);
for(int i = 1; i <= n; i++) cin >> v[i];
v[0] = -1e15, v[n+1] = 1e15;
lg q;
cin >> q;
while(q--)
{
lg x;
cin >> x;
lg idx = 1;
lg l1 = 1, r1 = n;
while(l1 <= r1)
{
lg mid = (l1+r1)/2;
if(v[mid] >= x)
{
idx = mid;
r1 = mid-1;
}
else l1 = mid+1;
}
lg idx2 = 1;
l1 = 1, r1 = n;
while(l1 <= r1)
{
lg mid = (l1+r1)/2;
if(v[mid] <= x)
{
idx2 = mid;
l1= mid+1;
}
else r1 = mid-1;
}
if(abs(v[idx2]-x) <= abs(v[idx]-x)) idx = idx2;
lg ans = abs(v[idx]-x);
lg l = idx, r = idx;
while(l > 1 || r < n)
{
if(v[r+1]-v[idx] < v[idx]-v[l-1])
{
l1 = r+1, r1 = n;
lg cur = 1;
while(l1 <= r1)
{
lg mid1 = (l1+r1)/2;
if(v[mid1] < 2*v[idx]-v[l-1])
{
l1 = mid1+1;
cur = mid1;
}
else r1 = mid1-1;
}
ans += abs(v[cur]-v[idx]);
idx = cur;
l = min(l, idx);
r = max(r, idx);
}
else{
l1 = 1, r1 = l-1;
lg cur = 1;
while(l1 <= r1)
{
lg mid1 = (l1+r1)/2;
if(v[mid1] >= 2*v[idx]-v[r+1])
{
r1 = mid1-1;
cur = mid1;
}
else l1 = mid1+1;
}
ans += abs(v[cur]-v[idx]);
idx = cur;
l = min(l, idx);
r = max(r, idx);
}
}
cout << ans << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 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 |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 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 |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
20 ms |
3660 KB |
Output is correct |
18 |
Correct |
19 ms |
3612 KB |
Output is correct |
19 |
Correct |
19 ms |
3532 KB |
Output is correct |
20 |
Correct |
20 ms |
3664 KB |
Output is correct |
21 |
Correct |
19 ms |
3748 KB |
Output is correct |
22 |
Correct |
20 ms |
3636 KB |
Output is correct |
23 |
Correct |
20 ms |
3760 KB |
Output is correct |
24 |
Correct |
19 ms |
3568 KB |
Output is correct |
25 |
Correct |
21 ms |
3568 KB |
Output is correct |
26 |
Correct |
19 ms |
3632 KB |
Output is correct |
27 |
Correct |
19 ms |
3656 KB |
Output is correct |
28 |
Correct |
21 ms |
3592 KB |
Output is correct |
29 |
Correct |
19 ms |
3632 KB |
Output is correct |
30 |
Correct |
20 ms |
3660 KB |
Output is correct |
31 |
Correct |
19 ms |
3624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
325 ms |
5596 KB |
Output is correct |
8 |
Correct |
366 ms |
5620 KB |
Output is correct |
9 |
Correct |
350 ms |
5540 KB |
Output is correct |
10 |
Correct |
360 ms |
5500 KB |
Output is correct |
11 |
Correct |
321 ms |
5612 KB |
Output is correct |
12 |
Correct |
331 ms |
5724 KB |
Output is correct |
13 |
Correct |
50 ms |
4044 KB |
Output is correct |
14 |
Correct |
29 ms |
1460 KB |
Output is correct |
15 |
Correct |
241 ms |
6872 KB |
Output is correct |
16 |
Correct |
240 ms |
6476 KB |
Output is correct |
17 |
Correct |
256 ms |
6672 KB |
Output is correct |
18 |
Correct |
46 ms |
3880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 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 |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
20 ms |
3660 KB |
Output is correct |
18 |
Correct |
19 ms |
3612 KB |
Output is correct |
19 |
Correct |
19 ms |
3532 KB |
Output is correct |
20 |
Correct |
20 ms |
3664 KB |
Output is correct |
21 |
Correct |
19 ms |
3748 KB |
Output is correct |
22 |
Correct |
20 ms |
3636 KB |
Output is correct |
23 |
Correct |
20 ms |
3760 KB |
Output is correct |
24 |
Correct |
19 ms |
3568 KB |
Output is correct |
25 |
Correct |
21 ms |
3568 KB |
Output is correct |
26 |
Correct |
19 ms |
3632 KB |
Output is correct |
27 |
Correct |
19 ms |
3656 KB |
Output is correct |
28 |
Correct |
21 ms |
3592 KB |
Output is correct |
29 |
Correct |
19 ms |
3632 KB |
Output is correct |
30 |
Correct |
20 ms |
3660 KB |
Output is correct |
31 |
Correct |
19 ms |
3624 KB |
Output is correct |
32 |
Correct |
1 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 |
1 ms |
320 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
325 ms |
5596 KB |
Output is correct |
39 |
Correct |
366 ms |
5620 KB |
Output is correct |
40 |
Correct |
350 ms |
5540 KB |
Output is correct |
41 |
Correct |
360 ms |
5500 KB |
Output is correct |
42 |
Correct |
321 ms |
5612 KB |
Output is correct |
43 |
Correct |
331 ms |
5724 KB |
Output is correct |
44 |
Correct |
50 ms |
4044 KB |
Output is correct |
45 |
Correct |
29 ms |
1460 KB |
Output is correct |
46 |
Correct |
241 ms |
6872 KB |
Output is correct |
47 |
Correct |
240 ms |
6476 KB |
Output is correct |
48 |
Correct |
256 ms |
6672 KB |
Output is correct |
49 |
Correct |
46 ms |
3880 KB |
Output is correct |
50 |
Correct |
380 ms |
7880 KB |
Output is correct |
51 |
Correct |
325 ms |
7428 KB |
Output is correct |
52 |
Correct |
307 ms |
7904 KB |
Output is correct |
53 |
Correct |
448 ms |
7916 KB |
Output is correct |
54 |
Correct |
435 ms |
8056 KB |
Output is correct |
55 |
Correct |
448 ms |
7708 KB |
Output is correct |
56 |
Correct |
81 ms |
7704 KB |
Output is correct |
57 |
Correct |
81 ms |
7740 KB |
Output is correct |
58 |
Correct |
80 ms |
7420 KB |
Output is correct |