#include <bits/stdc++.h>
#define fi first
#define se second
const int N = 200200;
using namespace std;
int n;
long long a[N];
long long t[N][20];
long long L[N];
long long get(int l, int r)
{
int g = L[r - l + 1];
return max(t[l][g], t[r - (1 << g) + 1][g]);
}
long long go_one(int &l, int&r, int &k)
{
if (l == 1 && r == n) {
return 0;
}
long long res;
if (abs(a[k] - a[l - 1]) <= abs(a[k] - a[r + 1])) {
l--;
res = abs(a[k] - a[l]);
k = l;
} else {
r++;
res = abs(a[k] - a[r]);
k = r;
}
return res;
}
long long solve(int k)
{
int l = k, r = k;
long long res = go_one(l, r, k);
while (l > 1 || r < n) {
res += go_one(l, r, k);
}
return res;
}
int main()
{
#ifdef zxc
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
a[0] = -2e9;
a[n + 1] = 2e9;
for (int i = 1; i <= n; i++) {
t[i][0] = a[i + 1] - a[i];
}
for (int i = 2; i < N; i++) {
L[i] = L[i / 2] + 1;
}
for (int i = 1; i < 20; i++) {
for (int j = 1; j + (1 << i) - 1 <= n; j++) {
t[j][i] = max(t[j][i - 1], t[j + (1 << i)][i - 1]);
}
}
int q;
cin >> q;
while (q--) {
long long x;
cin >> x;
int i = lower_bound(a + 1, a + n + 1, x) - a;
if (i > 1 && abs(a[i - 1] - x) <= abs(a[i] - x)) {
i--;
}
cout << solve(i) + abs(a[i] - x) << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
2132 KB |
Output is correct |
3 |
Correct |
1 ms |
2132 KB |
Output is correct |
4 |
Correct |
1 ms |
2132 KB |
Output is correct |
5 |
Correct |
1 ms |
2132 KB |
Output is correct |
6 |
Correct |
1 ms |
2136 KB |
Output is correct |
7 |
Correct |
1 ms |
2132 KB |
Output is correct |
8 |
Correct |
1 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1876 KB |
Output is correct |
14 |
Correct |
2 ms |
2260 KB |
Output is correct |
15 |
Correct |
1 ms |
2132 KB |
Output is correct |
16 |
Correct |
1 ms |
2140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
2132 KB |
Output is correct |
3 |
Correct |
1 ms |
2132 KB |
Output is correct |
4 |
Correct |
1 ms |
2132 KB |
Output is correct |
5 |
Correct |
1 ms |
2132 KB |
Output is correct |
6 |
Correct |
1 ms |
2136 KB |
Output is correct |
7 |
Correct |
1 ms |
2132 KB |
Output is correct |
8 |
Correct |
1 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1876 KB |
Output is correct |
14 |
Correct |
2 ms |
2260 KB |
Output is correct |
15 |
Correct |
1 ms |
2132 KB |
Output is correct |
16 |
Correct |
1 ms |
2140 KB |
Output is correct |
17 |
Correct |
50 ms |
36684 KB |
Output is correct |
18 |
Correct |
50 ms |
36684 KB |
Output is correct |
19 |
Correct |
53 ms |
36676 KB |
Output is correct |
20 |
Correct |
54 ms |
36672 KB |
Output is correct |
21 |
Correct |
52 ms |
36684 KB |
Output is correct |
22 |
Correct |
52 ms |
36684 KB |
Output is correct |
23 |
Correct |
50 ms |
36652 KB |
Output is correct |
24 |
Correct |
51 ms |
36636 KB |
Output is correct |
25 |
Correct |
50 ms |
36692 KB |
Output is correct |
26 |
Correct |
55 ms |
36576 KB |
Output is correct |
27 |
Correct |
50 ms |
36592 KB |
Output is correct |
28 |
Correct |
52 ms |
36668 KB |
Output is correct |
29 |
Correct |
48 ms |
36576 KB |
Output is correct |
30 |
Correct |
48 ms |
36632 KB |
Output is correct |
31 |
Correct |
51 ms |
36576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
1876 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Execution timed out |
3078 ms |
34820 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
2132 KB |
Output is correct |
3 |
Correct |
1 ms |
2132 KB |
Output is correct |
4 |
Correct |
1 ms |
2132 KB |
Output is correct |
5 |
Correct |
1 ms |
2132 KB |
Output is correct |
6 |
Correct |
1 ms |
2136 KB |
Output is correct |
7 |
Correct |
1 ms |
2132 KB |
Output is correct |
8 |
Correct |
1 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1876 KB |
Output is correct |
14 |
Correct |
2 ms |
2260 KB |
Output is correct |
15 |
Correct |
1 ms |
2132 KB |
Output is correct |
16 |
Correct |
1 ms |
2140 KB |
Output is correct |
17 |
Correct |
50 ms |
36684 KB |
Output is correct |
18 |
Correct |
50 ms |
36684 KB |
Output is correct |
19 |
Correct |
53 ms |
36676 KB |
Output is correct |
20 |
Correct |
54 ms |
36672 KB |
Output is correct |
21 |
Correct |
52 ms |
36684 KB |
Output is correct |
22 |
Correct |
52 ms |
36684 KB |
Output is correct |
23 |
Correct |
50 ms |
36652 KB |
Output is correct |
24 |
Correct |
51 ms |
36636 KB |
Output is correct |
25 |
Correct |
50 ms |
36692 KB |
Output is correct |
26 |
Correct |
55 ms |
36576 KB |
Output is correct |
27 |
Correct |
50 ms |
36592 KB |
Output is correct |
28 |
Correct |
52 ms |
36668 KB |
Output is correct |
29 |
Correct |
48 ms |
36576 KB |
Output is correct |
30 |
Correct |
48 ms |
36632 KB |
Output is correct |
31 |
Correct |
51 ms |
36576 KB |
Output is correct |
32 |
Correct |
1 ms |
1876 KB |
Output is correct |
33 |
Correct |
1 ms |
1876 KB |
Output is correct |
34 |
Correct |
1 ms |
1876 KB |
Output is correct |
35 |
Correct |
1 ms |
1876 KB |
Output is correct |
36 |
Correct |
1 ms |
1876 KB |
Output is correct |
37 |
Correct |
1 ms |
1876 KB |
Output is correct |
38 |
Execution timed out |
3078 ms |
34820 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |