#include <iostream>
#include <algorithm>
using namespace std;
long long int a[200005], l[200005][19], ll[200005], la[200005][19];
long long int check(int x, int y) {
int h = ll[y - x + 1];
return max(l[x][h], l[y - (1 << h) + 1][h]);
}
long long int check2(int x, int y) {
int h = ll[y - x + 1];
return min(la[x][h], la[y - (1 << h) + 1][h]);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 2; i <= n; i++) {
l[i][0] = a[i] * 2 - a[i - 1];
ll[i] = ll[i / 2] + 1;
}
for (int i = 1; i < n; i++) {
la[i][0] = a[i + 1] - a[i] * 2;
}
l[1][0] = 1e18;
la[n][0] = -1e18;
for (int i = 1; i <= 18; i++) {
for (int j = 2; j <= n - (1 << i) + 1; j++) {
l[j][i] = max(l[j][i - 1], l[j + (1 << (i - 1))][i - 1]);
}
for (int j = 1; j < n - (1 << i) + 1; j++) {
la[j][i] = min(la[j][i - 1], la[j + (1 << (i - 1))][i - 1]);
}
}
int q;
cin >> q;
for (int jj = 0; jj < q; jj++) {
long long int x, c = 0, l, r, flag = 0;
cin >> x;
if (x >= a[n]) {
c = x - a[n];
l = r = n;
}
else if (x <= a[1]) {
c = a[1] - x;
l = r = 1;
}
else {
int h = lower_bound(a + 1, a + n + 1, x) - a;
if (a[h] - x >= x - a[h - 1]) {
l = r = h - 1;
c += x - a[h - 1];
}
else {
l = r = h;
c = a[h] - x;
}
}
while (l != 1 && r != n) {
if (flag == 0) {
int ll = 1, rr = l, res = 1;
while (ll <= rr) {
int mid = (ll + rr) / 2;
if (check(mid, l) > a[r + 1]) {
res = mid;
ll = mid + 1;
}
else rr = mid - 1;
}
c += a[l] - a[res];
l = res;
c += a[r + 1] - a[l];
r++;
}
else {
int ll = r, rr = n, res = n;
while (ll <= rr) {
int mid = (ll + rr) / 2;
if (check2(r, mid) <= a[l - 1]) {
res = mid;
rr = mid - 1;
}
else ll = mid + 1;
}
c += a[res] - a[r];
r = res;
c += a[r] - a[l - 1];
l--;
}
//cout << l << " " << r << " " << c << '\n';
flag ^= 1;
}
if (flag == 1 && r == n) c += (l != 1) * (a[n] - a[1]);
else if (flag == 1 && l == 1) c += a[n] - a[r];
else if (flag == 0 && r == n) c += a[l] - a[1];
else c += a[n] - a[1];
cout << c << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
1112 KB |
Output is correct |
3 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
1112 KB |
Output is correct |
3 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Incorrect |
143 ms |
66752 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
1112 KB |
Output is correct |
3 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |