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 "jumps.h"
#include <bits/stdc++.h>
using namespace std;
const int L = 18;
int n;
vector<int> a;
vector<vector<int>> lo, hi;
void init(int N, vector<int> H){
n = N, a = H;
lo.assign(n, vector(L, -1));
hi.assign(n, vector(L, -1));
vector<int> idx(n);
for(int i = 0; i < n; i++){
idx[--a[i]] = i;
}
set<int> s;
for(int i = n - 1; i >= 0; i--){
auto j = s.lower_bound(idx[i]);
int l = -1, r = -1;
if(j != s.end()){
r = a[*j];
}
if(j != s.begin()){
l = a[*prev(j)];
}
if(l != -1 && (r == -1 || l >= r)){
lo[i][0] = r;
hi[i][0] = l;
} else{
lo[i][0] = l;
hi[i][0] = r;
}
s.insert(idx[i]);
}
for(int j = 1; j < L; j++){
for(int i = 0; i < n; i++){
if(lo[i][j - 1] != -1){
lo[i][j] = lo[lo[i][j - 1]][j - 1];
}
if(hi[i][j - 1] != -1){
hi[i][j] = hi[hi[i][j - 1]][j - 1];
}
}
}
}
int minimum_jumps(int A, int B, int C, int D){
int H = a[A], G = a[C], c = 0;
for(int i = L - 1; i >= 0; i--){
if(hi[H][i] != -1 && hi[H][i] <= G){
H = hi[H][i];
c += 1 << i;
}
}
for(int i = L - 1; i >= 0; i--){
if(lo[H][i] != -1 && lo[H][i] <= G){
H = lo[H][i];
c += 1 << i;
}
}
if(H != G){
c = -1;
}
return c;
}
# | 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... |