이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "jumps.h"
#include <bits/stdc++.h>
using namespace std;
constexpr int LOGMAX = 20;
constexpr int NMAX = 2e5 + 5;
int st[LOGMAX][NMAX];
int dr[LOGMAX][NMAX];
int bst[LOGMAX][NMAX];
int Lg[NMAX];
int vf;
void init(int N, std::vector<int> H) {
deque <int> D;
Lg[1] = 0;
for (int i = 2; i <= N; ++ i )
Lg[i] = Lg[i/2] + 1;
vf = Lg[N];
for (int i = 0; i <= vf; ++ i )
for (int j = 0; j < N; ++ j )
st[i][j] = dr[i][j] = bst[i][j] = -1;
for (int i = 0; i < N; ++ i ) {
while (!D.empty() && H[D.back()] < H[i]) {
dr[0][D.back()] = i;
D.pop_back();
}
if (!D.empty()) st[0][i] = D.back();
D.push_back(i);
}
for (int i = 0; i < N; ++ i ) {
bst[0][i] = -1;
if (st[0][i] == -1) bst[0][i] = dr[0][i];
else if (dr[0][i] == -1) bst[0][i] = st[0][i];
else {
bst[0][i] = (H[st[0][i]] > H[dr[0][i]] ? st[0][i] : dr[0][i]);
}
}
for (int i = 1; i <= Lg[N]; ++ i ) {
for (int j = 0; j + (1<<i) - 1 < N; ++ j ) {
if (st[i-1][j] == -1) st[i][j] = -1;
else st[i][j] = st[i-1][st[i-1][j]];
if (dr[i-1][j] == -1) dr[i][j] = -1;
else dr[i][j] = dr[i-1][dr[i-1][j]];
if (bst[i-1][j] == -1) bst[i][j] = -1;
else bst[i][j] = bst[i-1][bst[i-1][j]];
}
}
}
int Can (int pos, int C, int D) {
int ans = 0;
if (C <= pos && pos <= D) return 0;
for (int i = vf; i >= 0; -- i ) {
if (dr[i][pos] == -1) continue;
if (dr[i][pos] >= C) continue;
pos = dr[i][pos];
ans += (1<<i);
}
pos = dr[0][pos];
++ ans;
return (pos <= D ? ans : -1);
}
int minimum_jumps(int A, int B, int C, int D) {
int best_pos_AB = B;
if (Can(B, C, D) == -1) return -1;
for (int i = vf; i >= 0; -- i ) {
if (st[i][best_pos_AB] == -1) continue;
if (st[i][best_pos_AB] < A) continue;
int new_pos = st[i][best_pos_AB];
if (Can(new_pos, C, D) >= 0) best_pos_AB = new_pos;
}
int answer = Can(best_pos_AB, C, D);
for (int i = vf; i >= 0; -- i ) {
if (bst[i][best_pos_AB] == -1) continue;
if (bst[i][best_pos_AB] >= C) continue;
int new_pos = bst[i][best_pos_AB];
int x;
if ((x = Can(new_pos, C, D)) >= 0) {
answer = x + (1<<i);
best_pos_AB = bst[i][best_pos_AB];
}
}
return answer;
}
# | 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... |