#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> h;
const int INF = 1e9;
struct node {
int left = -1;
int right = -1;
};
vector<node> nodes;
vector< vector<int> > binary_lifting(262144 + 1, vector<int>(30, -1)); // It tells you the 2^j ancestor of i.
void init(int N, vector<int> H) {
n = N;
h = H;
nodes = vector<node>(n); // It's a graph, has the index to the children.
for (int i = 1; i < n; i++) {
int target = i - 1;
while (H[target] <= H[i]) {
if (target == -1) {
break;
}
target = nodes[target].left;
}
nodes[i].left = target;
}
for (int i = n - 2; i >= 0; i--) {
int target = i + 1;
while (H[target] <= H[i]) {
if (target == -1) {
break;
}
target = nodes[target].right;
}
nodes[i].right = target;
}
// Create the binary_lift
// Create the base ancestors;
for (int i = 0; i < n; i++) {
int greatest = -1;
int ancestor = -1;
if (nodes[i].left != -1 && greatest < H[nodes[i].left]) {
greatest = H[nodes[i].left];
ancestor = nodes[i].left;
}
if (nodes[i].right != -1 && greatest < H[nodes[i].right]) {
greatest = H[nodes[i].right];
ancestor = nodes[i].right;
}
binary_lifting[i][0] = ancestor;
}
for (int i = 1; i < 30; i++) {
for (int j = 0; j < n; j++) {
if (binary_lifting[j][i - 1] == -1) {
binary_lifting[j][i] = -1;
}
else {
binary_lifting[j][i] = binary_lifting[binary_lifting[j][i - 1]][i - 1];
}
}
}
return;
}
// Return the index of the closest approximate of how many to jump.
int final_dist;
int closest(int source, int height) {
int high = 29;
int low = 0;
int ans = -1;
// Find the greatest ancestor that doesn't exceed or equal the height
// In theory, if the source is too large, then it should just return 0.
while (low <= high) {
int mid = (low + high) / 2;
if (binary_lifting[source][mid] != -1 && h[binary_lifting[source][mid]] < height) {
ans = mid;
low = mid + 1;
}
else {
high = mid - 1;
}
}
if (ans == -1) {
// My parent is already optimal.
return -1;
}
int child_ans = closest(binary_lifting[source][ans], height - (1<<ans));
final_dist += 1<<ans;
if (child_ans == -1) {
// My children thinks I am the optimal one.
return binary_lifting[source][ans];
}
else {
return child_ans;
}
}
int minimum_jumps(int A, int B, int C, int D) {
if (A != B || C != D) {
return -1;
}
// Find the greatest height only using the greater neighbour that does not exceed C.
final_dist = 0;
int closest_index = closest(A, h[C]);
// If closest_index is -1, it can mean that the height is equal or too big.
if (closest_index == -1) {
closest_index = A;
}
// cout << closest_index << '\n';
if (h[nodes[closest_index].left] == h[C] || h[nodes[closest_index].right] == h[C]) {
return final_dist + 1;
}
else {
return -1;
}
}
# | 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... |