#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
using namespace std ;
const ll N = 2e5 ;
ll n, q, x[N + 2], abu[N + 2] ;
bool flag ;
signed main()
{
ios_base::sync_with_stdio( 0 ) ;
cin.tie( 0 ) ;
cout.tie( 0 ) ;
cin >> n ;
x[0] = -1e18 ;
x[n + 1] = 1e18 ;
for(ll i = 1 ; i <= n ; i++)
cin >> x[i] ;
for(ll i = 1 ; i < n ; i++)
if(x[i + 1] - x[i] > 100)
flag = 1 ;
cin >> q ;
if(q == 1)
{
ll ans = 0, pos ;
cin >> pos ;
set<ll> s_neg, s_pos ;
for(ll i = 1 ; i <= n ; i++)
s_pos.insert(x[i]), s_neg.insert(-x[i]) ;
while(s_pos.size())
{
ll l = -1e18, r = 1e18 ;
if(s_pos.lower_bound(pos) != s_pos.end())
r = *s_pos.lower_bound(pos) ;
if(s_neg.lower_bound(-pos) != s_neg.end())
l = -(*s_neg.lower_bound(-pos)) ;
if(pos - l <= r - pos)
{
ans += pos - l ;
pos = l ;
s_neg.erase(-l) ;
s_pos.erase(l) ;
}
else
{
ans += r - pos ;
pos = r ;
s_neg.erase(-r) ;
s_pos.erase(r) ;
}
}
cout << ans ;
return 0 ;
}
// if(!flag)
// {
// for(ll i = 1 ; i <= n ; i++)
// {
// ll l = i - 1, r = i + 1, now = i ;
// while(x[now] - x[l] <= 100ll && x[r] - x[now] <= 100ll)
// if(x[now] - x[l] <= x[r] - x[now])
// {
// abu[i] += x[now] - x[l] ;
// now = l ;
// l-- ;
// }
// else
// {
// abu[i] += x[r] - x[now] ;
// now = r ;
// r++ ;
// }
// if(l && r <= n)
// {
// if(x[now] - x[l] <= 100)
// abu[i] += x[now] - x[1] ;
// else
// abu[i] += x[n] - x[now] ;
// }
// abu[i] += x[n] - x[1] ;
// }
// while(q--)
// {
// ll pos, l = 0, r = n + 1 ;
// cin >> pos ;
// while(l + 1 < r)
// {
// ll mid = (l + r) >> 1 ;
// if(x[mid] <= pos)l = mid ;
// else r = mid ;
// }
// if(pos - x[l] <= x[r] - pos)
// cout << abu[l] + pos - x[l] ;
// else
// cout << abu[r] + x[r] - pos ;
// cout << '\n' ;
// }
// }
// else
// {
for(ll i = 1 ; i <= n ; i++)
{
ll l = i - 1, r = i + 1, now = i ;
while(l && r <= n)
if(x[now] - x[l] <= x[r] - x[now])
{
ll l1 = 0, r1 = l ;
while(l1 + 1 < r1)
{
ll mid = (l1 + r1) >> 1 ;
if(x[now] - x[mid] <= x[r] - x[now])r1 = mid ;
else l1 = mid ;
}
abu[i] += x[now] - x[r1] ;
now = r1 ;
l = r1 - 1 ;
}
else
{
ll l1 = r, r1 = n + 1 ;
while(l1 + 1 < r1)
{
ll mid = (l1 + r1) >> 1 ;
if(x[now] - x[l] > x[mid] - x[now])l1 = mid ;
else r1 = mid ;
}
abu[i] += x[l1] - x[now] ;
now = l1 ;
r = l1 + 1 ;
}
abu[i] += x[n] - x[1] ;
}
while(q--)
{
ll pos, l = 0, r = n + 1 ;
cin >> pos ;
while(l + 1 < r)
{
ll mid = (l + r) >> 1 ;
if(x[mid] <= pos)l = mid ;
else r = mid ;
}
if(pos - x[l] <= x[r] - pos)
cout << abu[l] + pos - x[l] ;
else
cout << abu[r] + x[r] - pos ;
cout << '\n' ;
}
// }
return 0 ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
552 KB |
Output is correct |
6 |
Correct |
1 ms |
472 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
552 KB |
Output is correct |
6 |
Correct |
1 ms |
472 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
120 ms |
22560 KB |
Output is correct |
18 |
Correct |
144 ms |
22596 KB |
Output is correct |
19 |
Correct |
147 ms |
22584 KB |
Output is correct |
20 |
Correct |
151 ms |
22596 KB |
Output is correct |
21 |
Correct |
175 ms |
22576 KB |
Output is correct |
22 |
Correct |
153 ms |
22484 KB |
Output is correct |
23 |
Correct |
119 ms |
22524 KB |
Output is correct |
24 |
Correct |
131 ms |
22588 KB |
Output is correct |
25 |
Correct |
149 ms |
22596 KB |
Output is correct |
26 |
Correct |
130 ms |
22596 KB |
Output is correct |
27 |
Correct |
125 ms |
22500 KB |
Output is correct |
28 |
Correct |
177 ms |
22552 KB |
Output is correct |
29 |
Correct |
125 ms |
22540 KB |
Output is correct |
30 |
Correct |
127 ms |
22540 KB |
Output is correct |
31 |
Correct |
134 ms |
22544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
232 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
182 ms |
7296 KB |
Output is correct |
8 |
Correct |
190 ms |
7320 KB |
Output is correct |
9 |
Correct |
161 ms |
7372 KB |
Output is correct |
10 |
Correct |
161 ms |
7256 KB |
Output is correct |
11 |
Correct |
151 ms |
7356 KB |
Output is correct |
12 |
Correct |
153 ms |
7460 KB |
Output is correct |
13 |
Correct |
33 ms |
4136 KB |
Output is correct |
14 |
Correct |
22 ms |
1468 KB |
Output is correct |
15 |
Correct |
281 ms |
7892 KB |
Output is correct |
16 |
Correct |
289 ms |
7856 KB |
Output is correct |
17 |
Correct |
331 ms |
7924 KB |
Output is correct |
18 |
Correct |
32 ms |
4064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
552 KB |
Output is correct |
6 |
Correct |
1 ms |
472 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
120 ms |
22560 KB |
Output is correct |
18 |
Correct |
144 ms |
22596 KB |
Output is correct |
19 |
Correct |
147 ms |
22584 KB |
Output is correct |
20 |
Correct |
151 ms |
22596 KB |
Output is correct |
21 |
Correct |
175 ms |
22576 KB |
Output is correct |
22 |
Correct |
153 ms |
22484 KB |
Output is correct |
23 |
Correct |
119 ms |
22524 KB |
Output is correct |
24 |
Correct |
131 ms |
22588 KB |
Output is correct |
25 |
Correct |
149 ms |
22596 KB |
Output is correct |
26 |
Correct |
130 ms |
22596 KB |
Output is correct |
27 |
Correct |
125 ms |
22500 KB |
Output is correct |
28 |
Correct |
177 ms |
22552 KB |
Output is correct |
29 |
Correct |
125 ms |
22540 KB |
Output is correct |
30 |
Correct |
127 ms |
22540 KB |
Output is correct |
31 |
Correct |
134 ms |
22544 KB |
Output is correct |
32 |
Correct |
1 ms |
232 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
328 KB |
Output is correct |
35 |
Correct |
1 ms |
324 KB |
Output is correct |
36 |
Correct |
0 ms |
328 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
182 ms |
7296 KB |
Output is correct |
39 |
Correct |
190 ms |
7320 KB |
Output is correct |
40 |
Correct |
161 ms |
7372 KB |
Output is correct |
41 |
Correct |
161 ms |
7256 KB |
Output is correct |
42 |
Correct |
151 ms |
7356 KB |
Output is correct |
43 |
Correct |
153 ms |
7460 KB |
Output is correct |
44 |
Correct |
33 ms |
4136 KB |
Output is correct |
45 |
Correct |
22 ms |
1468 KB |
Output is correct |
46 |
Correct |
281 ms |
7892 KB |
Output is correct |
47 |
Correct |
289 ms |
7856 KB |
Output is correct |
48 |
Correct |
331 ms |
7924 KB |
Output is correct |
49 |
Correct |
32 ms |
4064 KB |
Output is correct |
50 |
Correct |
182 ms |
9392 KB |
Output is correct |
51 |
Correct |
206 ms |
9448 KB |
Output is correct |
52 |
Correct |
240 ms |
9436 KB |
Output is correct |
53 |
Correct |
266 ms |
9472 KB |
Output is correct |
54 |
Correct |
280 ms |
9384 KB |
Output is correct |
55 |
Correct |
269 ms |
9480 KB |
Output is correct |
56 |
Correct |
136 ms |
9344 KB |
Output is correct |
57 |
Correct |
128 ms |
9276 KB |
Output is correct |
58 |
Correct |
129 ms |
9376 KB |
Output is correct |