이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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];
int qryY(int l, int r, int k) {
for (int i = l; i <= r; i++) {
if (Y[i] >= k) return i;
}
return N + 1;
}
int qryZ(int l, int r, int k) {
for (int i = r; i >= l; i--) {
if (Z[i] > k) return i;
}
return 0;
}
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];
}
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]
//~ int j = root[0]->qry(1, l - 1, y + X[S]) + 1;
int j = qryZ(1, l - 1, y + X[S]) + 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 = qryY(r + 1, N, x - X[S]) - 1;
// r --> j
cur += X[j] - X[S];
r = S = j;
}
//~ cout << "@ " << S << ' ' << l << ' ' << r << ' ' << cur << '\n';
}
cout << cur << '\n';
}
}
컴파일 시 표준 에러 (stderr) 메시지
travel.cpp:28:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
28 | 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... |