This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define int long long
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
int N, Q, X[200005], Y[200005], Z[200005];
struct node0 {
node0 *left, *right;
int S, E, val;
node0(int _s, int _e) : S(_s), E(_e) {
if (S == E) {
val = Z[S];
return;
}
int M = (S + E) >> 1;
left = new node0(S, M);
right = new node0(M + 1, E);
val = max(left->val, right->val);
}
int qry(int p, int v) {
if (p < S) return 0;
if (E <= p) {
if (val < v) return 0;
if (S == E) return S;
if (right->val >= v) return right->qry(p, v);
else return left->qry(p, v);
}
auto t = right->qry(p, v);
if (t) return t;
return left->qry(p, v);
}
} *root0;
struct node1 {
node1 *left, *right;
int S, E, val;
node1(int _s, int _e) : S(_s), E(_e) {
if (S == E) {
val = Y[S];
return;
}
int M = (S + E) >> 1;
left = new node1(S, M);
right = new node1(M + 1, E);
val = max(left->val, right->val);
}
int qry(int p, int v) {
if (p > E) return N + 1;
if (p <= S) {
if (val < v) return N + 1;
if (S == E) return S;
if (left->val >= v) return left->qry(p, v);
else return right->qry(p, v);
}
auto t = left->qry(p, v);
if (t != N + 1) return t;
return right->qry(p, v);
}
} *root1;
main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> X[i];
}
for (int i = 1; i <= N; i++) {
Y[i] = X[i] - 2 * X[i - 1];
Z[i] = 2 * X[i + 1] - X[i];
}
root0 = new node0(1, N);
root1 = new node1(1, N);
cin >> Q;
for (int S; Q--; ) {
cin >> S;
int cur = 0;
int pos = lower_bound(X + 1, X + 1 + N, S) - X;
int x = (int)-1e16, y = (int)1e16;
if (pos <= N) {
y = X[pos];
}
if (pos > 1) {
x = X[pos - 1];
}
cur += min(S - x, y - S);
if (S - x <= y - S) {
S = pos - 1;
} else {
S = pos;
}
int l = S, r = S;
//~ cout << "@ " << S << '\n';
while (l > 1 || r < N) {
// decide which direction
int x = (l == 1 ? (int)1e16 : llabs(X[S] - X[l - 1]));
int y = (r == N ? (int)1e16 : llabs(X[S] - X[r + 1]));
if (x <= y) {
// left
// find maximum j < l such that
// Z[j] >= y + X[S] + 1
//~ int j = root[0]->qry(1, l - 1, y + X[S] + 1) + 1;
int j = root0->qry(l - 1, y + X[S] + 1) + 1;
// l --> j
cur += X[S] - X[j];
l = S = j;
} else {
// right
// find minimum j > r such that
// Y[j] >= x - X[S]
//~ int j = root[1]->qry(r + 1, N, x - X[S]) - 1;
int j = root1->qry(r + 1, x - X[S]) - 1;
// r --> j
cur += X[j] - X[S];
r = S = j;
}
//~ cout << "@ " << S << ' ' << l << ' ' << r << ' ' << cur << '\n';
}
cout << cur << '\n';
}
}
Compilation message (stderr)
travel.cpp:68:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
68 | main() {
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |