#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 7;
const int L = 20;
#define int ll
int n, q;
int a[N], x[N], y[N];
struct SparseTable {
vector<int> lg;
vector<int> mn[L];
vector<int> mx[L];
SparseTable(int n, int a[]) {
lg.resize(n + 1);
for (int i = 2; i <= n; ++i) lg[i] = lg[i / 2] + 1;
for (int i = 0; i < L; ++i) mn[i].resize(n + 1);
for (int i = 0; i < L; ++i) mx[i].resize(n + 1);
for (int i = 1; i <= n; ++i) mn[0][i] = a[i];
for (int i = 1; i <= n; ++i) mx[0][i] = a[i];
for (int h = 1; h < L; ++h) {
for (int i = 1; i + (1 << h) - 1 <= n; ++i) {
mx[h][i] = max(mx[h - 1][i], mx[h - 1][i + (1 << (h - 1))]);
mn[h][i] = min(mn[h - 1][i], mn[h - 1][i + (1 << (h - 1))]);
}
}
}
int get_max(int l, int r) {
int p = lg[r - l + 1];
return max(mx[p][l], mx[p][r - (1 << p) + 1]);
}
int get_min(int l, int r) {
int p = lg[r - l + 1];
return min(mn[p][l], mn[p][r - (1 << p) + 1]);
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i < n; ++i) x[i] = 2 * a[i] - a[i + 1];
for (int i = 1; i <= n; ++i) y[i] = 2 * a[i] - a[i - 1];
SparseTable t1(n - 1, x), t2(n, y);
cin >> q;
while (q--) {
int x;
cin >> x;
int low = 1, high = n, pos = 0;
while (low <= high) {
int mid = (low + high) / 2;
if (a[mid] <= x) {
low = mid + 1;
pos = mid;
} else {
high = mid - 1;
}
}
int l = pos, r = pos + 1;
int cord = x;
ll ans = 0;
while (l != 0 || r != n + 1) {
if (l == 0) {
ans += a[n] - cord;
r = n + 1;
break;
}
if (r == n + 1) {
ans += cord - a[1];
l = 0;
break;
}
if (abs(cord - a[l]) <= abs(cord - a[r])) { // merge in stanga
if (l == 1) {
ans += abs(cord - a[1]);
cord = a[1];
l = 0;
continue;
}
int low = 2, high = l, sol = l + 1;
while (low <= high) {
int mid = (low + high) / 2;
// 2 * a[mid] - a[mid - 1]
if (t2.get_max(mid, l) <= a[r]) {
high = mid - 1;
sol = mid;
} else {
low = mid + 1;
}
}
ans += abs(cord - a[sol - 1]);
cord = a[sol - 1];
l = sol - 2;
} else { // merge in dreapta
if (r == n) {
ans += abs(cord - a[n]);
cord = a[n];
r = n + 1;
continue;
}
int low = r, high = n - 1, sol = r - 1;
while (low <= high) {
int mid = (low + high) / 2;
// 2 * a[mid] - a[mid + 1]
if (t1.get_min(r, mid) > a[l]) {
low = mid + 1;
sol = mid;
} else {
high = mid - 1;
}
}
ans += abs(cord - a[sol + 1]);
cord = a[sol + 1];
r = sol + 2;
}
}
cout << ans << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
1780 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
2 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
1628 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
1628 KB |
Output is correct |
15 |
Correct |
1 ms |
1628 KB |
Output is correct |
16 |
Correct |
1 ms |
1624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
1780 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
2 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
1628 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
1628 KB |
Output is correct |
15 |
Correct |
1 ms |
1628 KB |
Output is correct |
16 |
Correct |
1 ms |
1624 KB |
Output is correct |
17 |
Correct |
77 ms |
135540 KB |
Output is correct |
18 |
Correct |
82 ms |
135508 KB |
Output is correct |
19 |
Correct |
84 ms |
135456 KB |
Output is correct |
20 |
Correct |
81 ms |
135404 KB |
Output is correct |
21 |
Correct |
75 ms |
135496 KB |
Output is correct |
22 |
Correct |
84 ms |
135544 KB |
Output is correct |
23 |
Correct |
76 ms |
135344 KB |
Output is correct |
24 |
Correct |
93 ms |
135444 KB |
Output is correct |
25 |
Correct |
88 ms |
135508 KB |
Output is correct |
26 |
Correct |
76 ms |
135508 KB |
Output is correct |
27 |
Correct |
78 ms |
135508 KB |
Output is correct |
28 |
Correct |
68 ms |
135580 KB |
Output is correct |
29 |
Correct |
72 ms |
135472 KB |
Output is correct |
30 |
Correct |
69 ms |
135380 KB |
Output is correct |
31 |
Correct |
72 ms |
135508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
130 ms |
137556 KB |
Output is correct |
8 |
Correct |
130 ms |
137348 KB |
Output is correct |
9 |
Correct |
124 ms |
137552 KB |
Output is correct |
10 |
Correct |
124 ms |
137552 KB |
Output is correct |
11 |
Correct |
124 ms |
137588 KB |
Output is correct |
12 |
Correct |
149 ms |
137340 KB |
Output is correct |
13 |
Correct |
33 ms |
4380 KB |
Output is correct |
14 |
Correct |
18 ms |
1628 KB |
Output is correct |
15 |
Correct |
137 ms |
138576 KB |
Output is correct |
16 |
Correct |
140 ms |
138576 KB |
Output is correct |
17 |
Correct |
148 ms |
138576 KB |
Output is correct |
18 |
Correct |
26 ms |
4184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
1780 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
2 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
1628 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
1628 KB |
Output is correct |
15 |
Correct |
1 ms |
1628 KB |
Output is correct |
16 |
Correct |
1 ms |
1624 KB |
Output is correct |
17 |
Correct |
77 ms |
135540 KB |
Output is correct |
18 |
Correct |
82 ms |
135508 KB |
Output is correct |
19 |
Correct |
84 ms |
135456 KB |
Output is correct |
20 |
Correct |
81 ms |
135404 KB |
Output is correct |
21 |
Correct |
75 ms |
135496 KB |
Output is correct |
22 |
Correct |
84 ms |
135544 KB |
Output is correct |
23 |
Correct |
76 ms |
135344 KB |
Output is correct |
24 |
Correct |
93 ms |
135444 KB |
Output is correct |
25 |
Correct |
88 ms |
135508 KB |
Output is correct |
26 |
Correct |
76 ms |
135508 KB |
Output is correct |
27 |
Correct |
78 ms |
135508 KB |
Output is correct |
28 |
Correct |
68 ms |
135580 KB |
Output is correct |
29 |
Correct |
72 ms |
135472 KB |
Output is correct |
30 |
Correct |
69 ms |
135380 KB |
Output is correct |
31 |
Correct |
72 ms |
135508 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
130 ms |
137556 KB |
Output is correct |
39 |
Correct |
130 ms |
137348 KB |
Output is correct |
40 |
Correct |
124 ms |
137552 KB |
Output is correct |
41 |
Correct |
124 ms |
137552 KB |
Output is correct |
42 |
Correct |
124 ms |
137588 KB |
Output is correct |
43 |
Correct |
149 ms |
137340 KB |
Output is correct |
44 |
Correct |
33 ms |
4380 KB |
Output is correct |
45 |
Correct |
18 ms |
1628 KB |
Output is correct |
46 |
Correct |
137 ms |
138576 KB |
Output is correct |
47 |
Correct |
140 ms |
138576 KB |
Output is correct |
48 |
Correct |
148 ms |
138576 KB |
Output is correct |
49 |
Correct |
26 ms |
4184 KB |
Output is correct |
50 |
Correct |
256 ms |
139652 KB |
Output is correct |
51 |
Correct |
287 ms |
139604 KB |
Output is correct |
52 |
Correct |
238 ms |
139644 KB |
Output is correct |
53 |
Correct |
249 ms |
139680 KB |
Output is correct |
54 |
Correct |
193 ms |
139604 KB |
Output is correct |
55 |
Correct |
207 ms |
139928 KB |
Output is correct |
56 |
Correct |
113 ms |
139436 KB |
Output is correct |
57 |
Correct |
109 ms |
139344 KB |
Output is correct |
58 |
Correct |
150 ms |
139348 KB |
Output is correct |