#include <iostream>
#include <array>
#define ll long long
using namespace std;
bool dir;
ll n, q, a, f, l, r, x, mid, A[200000], L[200000], R[200000];
struct SegTree{
array <ll, 2> st[800000];
void build(ll id, ll l, ll r) {
if (l == r) {
st[id] = {L[l], R[l]};
return;
}
ll mid = (l+r)/2;
build(id*2, l, mid);
build(id*2+1, mid+1, r);
st[id][0] = max(st[id*2][0], st[id*2+1][0]);
st[id][1] = min(st[id*2][1], st[id*2+1][1]);
}
ll queryL(ll id, ll l, ll r, ll ql, ll qr, ll w) {
if (qr < l || r < ql) return -1e18;
ll mid = (l+r)/2;
if (ql <= l && r <= qr) {
if (st[id][0] <= w) return -1e18;
else if (l == r) return l;
ll res = queryL(id*2+1, mid+1, r, ql, qr, w);
if (res == -1e18) res = queryL(id*2, l, mid, ql, qr, w);
return res;
}
ll res = queryL(id*2+1, mid+1, r, ql, qr, w);
if (res == -1e18) res = queryL(id*2, l, mid, ql, qr, w);
return res;
}
ll queryR(ll id, ll l, ll r, ll ql, ll qr, ll w) {
if (qr < l || r < ql) return 1e18;
ll mid = (l+r)/2;
if (ql <= l && r <= qr) {
if (st[id][1] >= w) return 1e18;
else if (l == r) return l;
ll res = queryR(id*2, l, mid, ql, qr, w);
if (res == 1e18) res = queryR(id*2+1, mid+1, r, ql, qr, w);
return res;
}
ll res = queryR(id*2, l, mid, ql, qr, w);
if (res == 1e18) res = queryR(id*2+1, mid+1, r, ql, qr, w);
return res;
}
}ST;
int main() {
cin >> n;
for (int i=0; i<n; ++i) {
cin >> A[i];
R[i] = 1e18;
L[i] = -1e18;
}
for (int i=1; i<n-1; ++i) {
l = 0, r = i-1;
while (l < r) {
mid = (l+r)/2;
if (A[i+1]-A[i] >= A[i]-A[mid]) r = mid;
else l = mid+1;
}
if (A[i+1]-A[i] >= A[i]-A[l]) {
R[i] = l;
}
l = i+1, r = n-1;
while (l < r) {
mid = (l+r+1)/2;
if (A[i]-A[i-1] > A[mid]-A[i]) l = mid;
else r = mid-1;
}
if (A[i]-A[i-1] > A[l]-A[i]) {
L[i] = l;
}
}
ST.build(1, 0, n-1);
cin >> q;
while (q--) {
cin >> a;
f = 0;
auto it = lower_bound(A, A+n, a);
if (it == A+n) {
f += a - *prev(it);
a = n-1;
}
else if (it == A) {
f += *it-a;
a = 0;
}
else if (a-*prev(it) <= *it-a) {
f += a - *prev(it);
a = it-A-1;
}
else {
f += *it-a;
a = it-A;
}
dir = 0;
if (!a || (a != n-1 && A[a+1]-A[a] < A[a]-A[a-1])) dir = 1;
l = r = a;
while (l != 0 && r != n-1) {
if (!dir) {
x = ST.queryL(1, 0, n-1, 0, l-1, r);
if (x == -1e18) {
f += A[r]-A[0];
l = 0;
}
else {
f += A[r]-A[x];
l = x;
}
}
else {
x = ST.queryR(1, 0, n-1, r+1, n-1, l);
if (x == 1e18) {
f += A[n-1]-A[l];
r = n-1;
}
else {
f += A[x]-A[l];
r = x;
}
}
dir ^= 1;
}
if (l != 0 || r != n-1) f += A[n-1]-A[0];
cout << f << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
2 ms |
4444 KB |
Output is correct |
16 |
Correct |
3 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
2 ms |
4444 KB |
Output is correct |
16 |
Correct |
3 ms |
4444 KB |
Output is correct |
17 |
Correct |
66 ms |
13492 KB |
Output is correct |
18 |
Correct |
64 ms |
13496 KB |
Output is correct |
19 |
Correct |
62 ms |
13296 KB |
Output is correct |
20 |
Correct |
76 ms |
13492 KB |
Output is correct |
21 |
Correct |
63 ms |
13396 KB |
Output is correct |
22 |
Correct |
78 ms |
13340 KB |
Output is correct |
23 |
Correct |
62 ms |
13336 KB |
Output is correct |
24 |
Correct |
63 ms |
13396 KB |
Output is correct |
25 |
Correct |
79 ms |
13492 KB |
Output is correct |
26 |
Correct |
62 ms |
13392 KB |
Output is correct |
27 |
Correct |
63 ms |
13396 KB |
Output is correct |
28 |
Correct |
64 ms |
13396 KB |
Output is correct |
29 |
Correct |
62 ms |
13392 KB |
Output is correct |
30 |
Correct |
72 ms |
13392 KB |
Output is correct |
31 |
Correct |
62 ms |
13360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
407 ms |
14768 KB |
Output is correct |
8 |
Correct |
388 ms |
14876 KB |
Output is correct |
9 |
Correct |
378 ms |
14772 KB |
Output is correct |
10 |
Correct |
399 ms |
14932 KB |
Output is correct |
11 |
Correct |
390 ms |
14776 KB |
Output is correct |
12 |
Correct |
396 ms |
14872 KB |
Output is correct |
13 |
Correct |
300 ms |
6352 KB |
Output is correct |
14 |
Correct |
276 ms |
5212 KB |
Output is correct |
15 |
Correct |
392 ms |
15188 KB |
Output is correct |
16 |
Correct |
405 ms |
18544 KB |
Output is correct |
17 |
Correct |
410 ms |
18568 KB |
Output is correct |
18 |
Correct |
305 ms |
8124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
2 ms |
4444 KB |
Output is correct |
16 |
Correct |
3 ms |
4444 KB |
Output is correct |
17 |
Correct |
66 ms |
13492 KB |
Output is correct |
18 |
Correct |
64 ms |
13496 KB |
Output is correct |
19 |
Correct |
62 ms |
13296 KB |
Output is correct |
20 |
Correct |
76 ms |
13492 KB |
Output is correct |
21 |
Correct |
63 ms |
13396 KB |
Output is correct |
22 |
Correct |
78 ms |
13340 KB |
Output is correct |
23 |
Correct |
62 ms |
13336 KB |
Output is correct |
24 |
Correct |
63 ms |
13396 KB |
Output is correct |
25 |
Correct |
79 ms |
13492 KB |
Output is correct |
26 |
Correct |
62 ms |
13392 KB |
Output is correct |
27 |
Correct |
63 ms |
13396 KB |
Output is correct |
28 |
Correct |
64 ms |
13396 KB |
Output is correct |
29 |
Correct |
62 ms |
13392 KB |
Output is correct |
30 |
Correct |
72 ms |
13392 KB |
Output is correct |
31 |
Correct |
62 ms |
13360 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
1 ms |
4444 KB |
Output is correct |
35 |
Correct |
1 ms |
4444 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
407 ms |
14768 KB |
Output is correct |
39 |
Correct |
388 ms |
14876 KB |
Output is correct |
40 |
Correct |
378 ms |
14772 KB |
Output is correct |
41 |
Correct |
399 ms |
14932 KB |
Output is correct |
42 |
Correct |
390 ms |
14776 KB |
Output is correct |
43 |
Correct |
396 ms |
14872 KB |
Output is correct |
44 |
Correct |
300 ms |
6352 KB |
Output is correct |
45 |
Correct |
276 ms |
5212 KB |
Output is correct |
46 |
Correct |
392 ms |
15188 KB |
Output is correct |
47 |
Correct |
405 ms |
18544 KB |
Output is correct |
48 |
Correct |
410 ms |
18568 KB |
Output is correct |
49 |
Correct |
305 ms |
8124 KB |
Output is correct |
50 |
Correct |
527 ms |
19796 KB |
Output is correct |
51 |
Correct |
565 ms |
19480 KB |
Output is correct |
52 |
Correct |
549 ms |
19412 KB |
Output is correct |
53 |
Correct |
450 ms |
19488 KB |
Output is correct |
54 |
Correct |
459 ms |
19880 KB |
Output is correct |
55 |
Correct |
456 ms |
19444 KB |
Output is correct |
56 |
Correct |
401 ms |
19508 KB |
Output is correct |
57 |
Correct |
399 ms |
19388 KB |
Output is correct |
58 |
Correct |
395 ms |
19436 KB |
Output is correct |