#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128 lll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
#define MAX 9223372036854775807LL
#define MIN -9223372036854775807LL
#define INF 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed; cout.precision(10);
#define sp << " "
#define en << "\n"
#define compress(v) sort(v.begin(), v.end()), v.erase(unique(v.begin(), v.end()), v.end())
ll n;
ll a[200010];
ll q;
ll X;
ll gap1[200010], gap2[200010];
ll minn[2500010][21], maxx[2500010][21];
int main(void)
{
fastio
cin >> n;
for(ll i = 1 ; i <= n ; i++)
cin >> a[i];
for(ll i = 2 ; i <= n ; i++)
gap1[i] = 2 * a[i] - a[i - 1];
gap1[1] = INF / n;
for(ll i = 1 ; i < n ; i++)
gap2[i] = 2 * a[i] - a[i + 1];
gap2[n] = -INF / n;
for(ll i = 1 ; i <= n ; i++)
minn[i][0] = gap2[i], maxx[i][0] = gap1[i];
for(ll i = 1 ; i <= 20 ; i++)
{
for(ll j = 1 ; j <= n ; j++)
{
ll L = max(1LL, j - (1LL << (i - 1)));
ll R = min(n, j + (1LL << (i - 1)));
minn[j][i] = min(minn[j][i - 1], minn[R][i - 1]);
maxx[j][i] = max(maxx[j][i - 1], maxx[L][i - 1]);
}
}
cin >> q;
while(q--)
{
cin >> X;
if(n == 1)
{
cout << abs(X - a[1]) en;
continue;
}
ll st = 0;
ll ans = 0;
if(X <= a[1])
st = 1, ans += a[1] - X;
else if(X >= a[n])
st = n, ans += X - a[n];
else
{
ll idx = lower_bound(a + 1, a + 1 + n, X) - a;
ll idx2 = idx - 1;
if(X - a[idx2] <= a[idx] - X)
st = idx2, ans += X - a[idx2];
else
st = idx, ans += a[idx] - X;
}
ll L = st, R = st;
ll D;
if(st == 1)
D = 1;
else if(st == n)
D = -1;
else
{
if(a[st] - a[st - 1] <= a[st + 1] - a[st])
D = -1;
else
D = 1;
}
while(1)
{
if(L <= 1)
{
if(D == 1)
ans += a[n] - a[R];
else
ans += a[n] - a[1];
break;
}
if(R >= n)
{
if(D == -1)
ans += a[L] - a[1];
else
ans += a[n] - a[1];
break;
}
if(D == -1)
{
ll XX = a[R + 1];
ll now = L - 1;
for(ll i = 20 ; i >= 0 ; i--)
{
if(maxx[now][i] <= XX)
now -= (1LL << i);
}
ans += a[L] - a[now];
ans += a[R + 1] - a[now];
L = now, R = R + 1, D = 1;
}
else
{
ll XX = a[L - 1];
ll now = R + 1;
for(ll i = 20 ; i >= 0 ; i--)
{
if(minn[now][i] > XX)
now += (1LL << i);
}
ans += a[now] - a[R];
ans += a[now] - a[L - 1];
L = L - 1, R = now, D = -1;
}
}
cout << ans en;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
9048 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8660 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
9048 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8660 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
86 ms |
73808 KB |
Output is correct |
18 |
Correct |
84 ms |
73816 KB |
Output is correct |
19 |
Correct |
85 ms |
73892 KB |
Output is correct |
20 |
Correct |
90 ms |
74172 KB |
Output is correct |
21 |
Correct |
85 ms |
73960 KB |
Output is correct |
22 |
Correct |
86 ms |
73960 KB |
Output is correct |
23 |
Correct |
90 ms |
73968 KB |
Output is correct |
24 |
Correct |
87 ms |
73820 KB |
Output is correct |
25 |
Correct |
89 ms |
73820 KB |
Output is correct |
26 |
Correct |
84 ms |
73812 KB |
Output is correct |
27 |
Correct |
87 ms |
73968 KB |
Output is correct |
28 |
Correct |
83 ms |
73808 KB |
Output is correct |
29 |
Correct |
85 ms |
73808 KB |
Output is correct |
30 |
Correct |
84 ms |
73808 KB |
Output is correct |
31 |
Correct |
86 ms |
73820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6744 KB |
Output is correct |
7 |
Incorrect |
196 ms |
75496 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
9048 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8660 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
86 ms |
73808 KB |
Output is correct |
18 |
Correct |
84 ms |
73816 KB |
Output is correct |
19 |
Correct |
85 ms |
73892 KB |
Output is correct |
20 |
Correct |
90 ms |
74172 KB |
Output is correct |
21 |
Correct |
85 ms |
73960 KB |
Output is correct |
22 |
Correct |
86 ms |
73960 KB |
Output is correct |
23 |
Correct |
90 ms |
73968 KB |
Output is correct |
24 |
Correct |
87 ms |
73820 KB |
Output is correct |
25 |
Correct |
89 ms |
73820 KB |
Output is correct |
26 |
Correct |
84 ms |
73812 KB |
Output is correct |
27 |
Correct |
87 ms |
73968 KB |
Output is correct |
28 |
Correct |
83 ms |
73808 KB |
Output is correct |
29 |
Correct |
85 ms |
73808 KB |
Output is correct |
30 |
Correct |
84 ms |
73808 KB |
Output is correct |
31 |
Correct |
86 ms |
73820 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6492 KB |
Output is correct |
34 |
Correct |
1 ms |
6492 KB |
Output is correct |
35 |
Correct |
1 ms |
6492 KB |
Output is correct |
36 |
Correct |
1 ms |
6492 KB |
Output is correct |
37 |
Correct |
1 ms |
6744 KB |
Output is correct |
38 |
Incorrect |
196 ms |
75496 KB |
Output isn't correct |
39 |
Halted |
0 ms |
0 KB |
- |