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>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const int N = 2e5 + 5;
const int L = 20;
int prv[N], nxt[N], n, h[N], sparse[N][L];
vector<int> H;
void init(int _n, std::vector<int> _H) {
n = _n;
H.swap(_H);
vector<int> st;
FOR(i, 0, n) {
while (st.size() && H[st.back()] <= H[i]) st.pop_back();
prv[i] = (st.empty() ? -1 : st.back());
st.push_back(i);
}
st.clear();
for (int i = n - 1; i >= 0; i--) {
while (st.size() && H[st.back()] <= H[i]) st.pop_back();
nxt[i] = (st.empty() ? n : st.back());
st.push_back(i);
}
for (int i = n - 1; i >= 0; i--) h[i] = h[nxt[i]] + 1;
for (int i = 0; i < n; i++) {
int x = (prv[i] == -1 ? -1 : H[prv[i]]), y = (nxt[i] == n ? -1 : H[nxt[i]]);
if (y == -1 && x == -1) sparse[i][0] = i;
else sparse[i][0] = (x > y ? prv[i] : nxt[i]);
}
FOR(j, 1, L) FOR(i, 0, n) sparse[i][j] = sparse[sparse[i][j - 1]][j - 1];
}
int minimum_jumps(int A, int B, int C, int D) {
assert(A == B && C == D);
if (prv[C] >= A) return -1;
int t = 0;
for (int i = L - 1; i >= 0; i--) if (H[sparse[A][i]] < H[C]) {
A = sparse[A][i];
t |= 1 << i;
}
return t + h[A] - h[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... |