이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "jumps.h"
const int MAX_N = 200010, L = 18;
int N;
int h[MAX_N], left[MAX_N][L], right[MAX_N][L], gr[MAX_N][L];
void init(int _N, std::vector<int> _H) {
N = _N;
std::copy(_H.begin(), _H.end(), h + 1);
static int stack[MAX_N], ord[MAX_N];
int cnt = 0;
for (int i = 1; i <= N; ++i) {
while (cnt && h[i] > h[stack[cnt]])
right[stack[cnt--]][0] = i;
left[i][0] = stack[cnt];
stack[++cnt] = i;
}
for (int i = 1; i <= N; ++i) for (int j = 1; j != L; ++j)
left[i][j] = left[left[i][j - 1]][j - 1];
std::fill(right[N + 1], right[N + 1] + L, N + 1);
for (int i = N; i; --i) {
if (!right[i][0]) right[i][0] = N + 1;
for (int j = 1; j != L; ++j)
right[i][j] = right[right[i][j - 1]][j - 1];
}
for (int i = 1; i <= N; ++i) ord[h[i]] = i;
std::fill(gr[N + 1], gr[N + 1] + L, N + 1);
for (int i = N; i; --i) {
int j = ord[i];
gr[j][0] = (h[left[j][0]] > h[right[j][0]] ? left : right)[j][0];
for (int k = 1; k != L; ++k) gr[j][k] = gr[gr[j][k - 1]][k - 1];
}
}
int minimum_jumps(int A, int B, int C, int D) {
++A; ++B; ++C; ++D;
for (int i = L - 1; i >= 0; --i) if (left[D][i] >= C)
D = left[D][i];
for (int i = L - 1; i >= 0; --i) if (left[B][i] >= A && h[left[B][i]] < h[D])
B = left[B][i];
int X = B;
for (int i = L - 1; i >= 0; --i) if (right[X][i] < C)
X = right[X][i];
int ret = 1;
for (int i = L - 1; i >= 0; --i) if (gr[B][i] <= N && h[gr[B][i]] <= h[X]) {
ret += 1 << i;
B = gr[B][i];
}
if (right[B][0] >= C && right[B][0] <= D) return ret;
if (gr[B][0] <= N && h[gr[B][0]] <= h[D]) {
++ret;
B = gr[B][0];
}
for (int i = L - 1; i >= 0; --i) if (right[B][i] < C) {
ret += 1 << i;
B = right[B][i];
}
if (h[B] > h[D]) return -1;
return ret;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |