#include <bits/stdc++.h>
using namespace std;
const int mxN = 2e5+2, LOG = 20;
long long mx[LOG][mxN], mn[LOG][mxN], lg[mxN];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
lg[1] = 0;
for (int i = 2; i < mxN; ++i) {
lg[i] = lg[i/2]+1;
}
int n;
cin >> n;
vector<long long> a(n+2);
a[0] = -1e15, a[n+1] = 1e15;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= n; ++i) {
mx[0][i] = 2*a[i] - a[i-1];
mn[0][i] = 2*a[i] - a[i+1];
}
for (int i = 1; i < LOG; ++i) {
for (int j = 1; j + (1 << i) <= n; ++j) {
mx[i][j] = max(mx[i-1][j], mx[i-1][j + (1 << (i - 1))]);
mn[i][j] = min(mn[i-1][j], mn[i-1][j + (1 << (i - 1))]);
}
}
auto mnq = [&](int l, int r) {return min(mn[lg[r-l+1]][l], mn[lg[r-l+1]][r-(1 << lg[r-l+1]) + 1]);};
auto mxq = [&](int l, int r) {return max(mx[lg[r-l+1]][l], mx[lg[r-l+1]][r-(1 << lg[r-l+1]) + 1]);};
//
int q;
cin >> q;
while (q--) {
int x;
cin >> x;
int b = lower_bound(a.begin(), a.end(), x) - a.begin();
if (b == n+1) {
b = n;
}
else if (x - a[b-1] <= a[b] - x) b--;
long long ans = abs(x-a[b]) + a[n] - a[1]; // verovatno staje u int
int l = b, r = b; // dir = 1 => idi desno, dir = 0 => idi levo.
bool dir = (b == 1 ? 1 : (b == n ? 0 : (a[b] - a[b-1] <= a[b+1] - a[b] ? 0 : 1)));
while (l != 1 && r != n) {
if (dir) {
if (r == n-1 || mnq(r+1, n-1) > a[l-1]) {
ans += a[n] - a[l];
break;
}
int le = r+1, ri = n-1, t = 0;
while (le <= ri) {
int mid = (le + ri)/2;
if (mnq(r+1, mid) <= a[l-1]) {
t = mid;
ri = mid-1;
}
else {
le = mid+1;
}
}
ans += a[t] - a[l];
r = t;
}
else {
if (l == 2 || mxq(2, l-1) <= a[r+1]) {
ans += a[r] - a[1];
break;
}
int le = 2, ri = l-1, t = 0;
while (le <= ri) {
int mid = (le + ri)/2;
if (mxq(mid, l-1) > a[r+1]) {
t = mid;
le = mid+1;
}
else {
ri = mid-1;
}
}
ans += a[r] - a[t];
l = t;
}
dir ^= 1;
}
cout << ans << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12936 KB |
Output is correct |
2 |
Correct |
5 ms |
37468 KB |
Output is correct |
3 |
Correct |
4 ms |
37544 KB |
Output is correct |
4 |
Correct |
5 ms |
37464 KB |
Output is correct |
5 |
Correct |
4 ms |
37464 KB |
Output is correct |
6 |
Correct |
4 ms |
37524 KB |
Output is correct |
7 |
Correct |
5 ms |
37468 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4748 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4952 KB |
Output is correct |
12 |
Correct |
1 ms |
4740 KB |
Output is correct |
13 |
Correct |
2 ms |
14936 KB |
Output is correct |
14 |
Correct |
6 ms |
37468 KB |
Output is correct |
15 |
Correct |
4 ms |
37464 KB |
Output is correct |
16 |
Correct |
5 ms |
37468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12936 KB |
Output is correct |
2 |
Correct |
5 ms |
37468 KB |
Output is correct |
3 |
Correct |
4 ms |
37544 KB |
Output is correct |
4 |
Correct |
5 ms |
37464 KB |
Output is correct |
5 |
Correct |
4 ms |
37464 KB |
Output is correct |
6 |
Correct |
4 ms |
37524 KB |
Output is correct |
7 |
Correct |
5 ms |
37468 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4748 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4952 KB |
Output is correct |
12 |
Correct |
1 ms |
4740 KB |
Output is correct |
13 |
Correct |
2 ms |
14936 KB |
Output is correct |
14 |
Correct |
6 ms |
37468 KB |
Output is correct |
15 |
Correct |
4 ms |
37464 KB |
Output is correct |
16 |
Correct |
5 ms |
37468 KB |
Output is correct |
17 |
Correct |
27 ms |
63636 KB |
Output is correct |
18 |
Correct |
25 ms |
63580 KB |
Output is correct |
19 |
Correct |
27 ms |
63580 KB |
Output is correct |
20 |
Correct |
25 ms |
63592 KB |
Output is correct |
21 |
Correct |
26 ms |
63568 KB |
Output is correct |
22 |
Correct |
25 ms |
63572 KB |
Output is correct |
23 |
Correct |
26 ms |
63580 KB |
Output is correct |
24 |
Correct |
25 ms |
63516 KB |
Output is correct |
25 |
Correct |
25 ms |
63572 KB |
Output is correct |
26 |
Correct |
26 ms |
63580 KB |
Output is correct |
27 |
Correct |
25 ms |
63568 KB |
Output is correct |
28 |
Correct |
25 ms |
63572 KB |
Output is correct |
29 |
Correct |
25 ms |
63580 KB |
Output is correct |
30 |
Correct |
25 ms |
63384 KB |
Output is correct |
31 |
Correct |
28 ms |
63528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
13144 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4696 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4736 KB |
Output is correct |
7 |
Correct |
73 ms |
65432 KB |
Output is correct |
8 |
Correct |
75 ms |
65860 KB |
Output is correct |
9 |
Correct |
74 ms |
65616 KB |
Output is correct |
10 |
Correct |
74 ms |
65620 KB |
Output is correct |
11 |
Correct |
76 ms |
65620 KB |
Output is correct |
12 |
Correct |
74 ms |
65620 KB |
Output is correct |
13 |
Correct |
35 ms |
8576 KB |
Output is correct |
14 |
Correct |
23 ms |
5720 KB |
Output is correct |
15 |
Correct |
93 ms |
66572 KB |
Output is correct |
16 |
Correct |
79 ms |
66936 KB |
Output is correct |
17 |
Correct |
86 ms |
66908 KB |
Output is correct |
18 |
Correct |
34 ms |
8528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12936 KB |
Output is correct |
2 |
Correct |
5 ms |
37468 KB |
Output is correct |
3 |
Correct |
4 ms |
37544 KB |
Output is correct |
4 |
Correct |
5 ms |
37464 KB |
Output is correct |
5 |
Correct |
4 ms |
37464 KB |
Output is correct |
6 |
Correct |
4 ms |
37524 KB |
Output is correct |
7 |
Correct |
5 ms |
37468 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4748 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4952 KB |
Output is correct |
12 |
Correct |
1 ms |
4740 KB |
Output is correct |
13 |
Correct |
2 ms |
14936 KB |
Output is correct |
14 |
Correct |
6 ms |
37468 KB |
Output is correct |
15 |
Correct |
4 ms |
37464 KB |
Output is correct |
16 |
Correct |
5 ms |
37468 KB |
Output is correct |
17 |
Correct |
27 ms |
63636 KB |
Output is correct |
18 |
Correct |
25 ms |
63580 KB |
Output is correct |
19 |
Correct |
27 ms |
63580 KB |
Output is correct |
20 |
Correct |
25 ms |
63592 KB |
Output is correct |
21 |
Correct |
26 ms |
63568 KB |
Output is correct |
22 |
Correct |
25 ms |
63572 KB |
Output is correct |
23 |
Correct |
26 ms |
63580 KB |
Output is correct |
24 |
Correct |
25 ms |
63516 KB |
Output is correct |
25 |
Correct |
25 ms |
63572 KB |
Output is correct |
26 |
Correct |
26 ms |
63580 KB |
Output is correct |
27 |
Correct |
25 ms |
63568 KB |
Output is correct |
28 |
Correct |
25 ms |
63572 KB |
Output is correct |
29 |
Correct |
25 ms |
63580 KB |
Output is correct |
30 |
Correct |
25 ms |
63384 KB |
Output is correct |
31 |
Correct |
28 ms |
63528 KB |
Output is correct |
32 |
Correct |
2 ms |
13144 KB |
Output is correct |
33 |
Correct |
2 ms |
14940 KB |
Output is correct |
34 |
Correct |
1 ms |
4700 KB |
Output is correct |
35 |
Correct |
1 ms |
4696 KB |
Output is correct |
36 |
Correct |
1 ms |
4700 KB |
Output is correct |
37 |
Correct |
1 ms |
4736 KB |
Output is correct |
38 |
Correct |
73 ms |
65432 KB |
Output is correct |
39 |
Correct |
75 ms |
65860 KB |
Output is correct |
40 |
Correct |
74 ms |
65616 KB |
Output is correct |
41 |
Correct |
74 ms |
65620 KB |
Output is correct |
42 |
Correct |
76 ms |
65620 KB |
Output is correct |
43 |
Correct |
74 ms |
65620 KB |
Output is correct |
44 |
Correct |
35 ms |
8576 KB |
Output is correct |
45 |
Correct |
23 ms |
5720 KB |
Output is correct |
46 |
Correct |
93 ms |
66572 KB |
Output is correct |
47 |
Correct |
79 ms |
66936 KB |
Output is correct |
48 |
Correct |
86 ms |
66908 KB |
Output is correct |
49 |
Correct |
34 ms |
8528 KB |
Output is correct |
50 |
Correct |
251 ms |
67724 KB |
Output is correct |
51 |
Correct |
246 ms |
67676 KB |
Output is correct |
52 |
Correct |
195 ms |
67748 KB |
Output is correct |
53 |
Correct |
131 ms |
67664 KB |
Output is correct |
54 |
Correct |
122 ms |
67608 KB |
Output is correct |
55 |
Correct |
144 ms |
67632 KB |
Output is correct |
56 |
Correct |
70 ms |
67592 KB |
Output is correct |
57 |
Correct |
74 ms |
67464 KB |
Output is correct |
58 |
Correct |
68 ms |
67408 KB |
Output is correct |