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 Nmax = 2e5 + 10;
int n;
int low[Nmax][20], high[Nmax][20], nxt[Nmax][20];
int L[Nmax], R[Nmax];
int h[Nmax];
void init(int N, vector <int> H) {
n = N;
for (int i = 1; i <= n; ++i) h[i] = H[i - 1];
stack <int> stk;
for (int i = 1; i <= n; ++i) {
while (stk.size() && h[stk.top()] < h[i]) {
R[stk.top()] = i;
stk.pop();
}
stk.push(i);
}
while (stk.size()) stk.pop();
for (int i = n; i >= 1; --i) {
while (stk.size() && h[stk.top()] < h[i]) {
L[stk.top()] = i;
stk.pop();
}
stk.push(i);
}
for (int i = 1; i <= n; ++i) {
if (h[L[i]] < h[R[i]]) {
high[i][0] = R[i];
low[i][0] = L[i];
} else {
high[i][0] = L[i];
low[i][0] = R[i];
}
nxt[i][0] = L[i];
}
for (int lg = 1; (1 << lg) <= n; ++lg) {
for (int i = 1; i <= n; ++i) {
low[i][lg] = low[low[i][lg - 1]][lg - 1];
high[i][lg] = high[high[i][lg - 1]][lg - 1];
nxt[i][lg] = nxt[nxt[i][lg - 1]][lg - 1];
}
}
}
int minimum_jumps(int A, int B, int C, int D) {
++A, ++B, ++C, ++D;
// asume that C = D
int need = 0;
for (int i = 19; i >= 0; --i) {
if (nxt[B][i] >= A && h[nxt[B][i]] <= h[C]) B = nxt[B][i];
}
if (h[B] > h[C]) return -1;
for (int i = 19; i >= 0; --i) {
if (high[B][i] && h[high[B][i]] <= h[C]) {
need += (1 << i);
B = high[B][i];
}
}
if (B == C) return need;
for (int i = 19; i >= 0; --i) {
if (low[B][i] && h[low[B][i]] <= h[C]) {
need += (1 << i);
B = low[B][i];
}
}
if (B != C) return -1;
return need;
}
//
//int main() {
// int N, Q;
// assert(2 == scanf("%d %d", &N, &Q));
// std::vector<int> H(N);
// for (int i = 0; i < N; ++i) {
// assert(1 == scanf("%d", &H[i]));
// }
// init(N, H);
//
// for (int i = 0; i < Q; ++i) {
// int A, B, C, D;
// assert(4 == scanf("%d %d %d %d", &A, &B, &C, &D));
// printf("%d\n", minimum_jumps(A, B, C, D));
// }
// return 0;
//}
# | 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... |