# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
934629 | MinaRagy06 | Rainforest Jumps (APIO21_jumps) | C++17 | 0 ms | 0 KiB |
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 <bits/stdc++.h>
#ifdef MINA
#include "grader.cpp"
#endif
#include "jumps.h"
using namespace std;
#define ll long long
const int N = 200'005;
int nxt[N], prv[N][18];
vector<int> a;
void init(int n, vector<int> _a) {
a = _a;
stack<int> s;
s.push(n);
for (int i = n - 1; i >= 0; i--) {
while (s.top() != n && a[s.top()] < a[i]) {
s.pop();
}
nxt[i] = s.top();
s.push(i);
}
while (s.size()) s.pop();
s.push(-1);
for (int i = 0; i < n; i++) {
while (s.top() != -1 && a[s.top()] < a[i]) {
s.pop();
}
prv[i][0] = s.top();
s.push(i);
}
for (int j = 1; j < 18; j++) {
for (int i = 0; i < n; i++) {
prv[i][j] = prv[i][j - 1];
if (prv[i][j] != -1) prv[i][j] = prv[prv[i][j]][j - 1];
}
}
}
int minimum_jumps(int l, int r, int s, int e) {
if (nxt[r] > e) {
return -1;
}
int mx = -1, lst = -1;
for (int i = r; i >= l; i--) {
if (a[i] > mx) {
mx = a[i];
if (nxt[i] <= e) {
lst = i;
} else {
break;
}
}
}
if (lst == -1) return -1;
int cur = lst, ans = 0;
while (cur < s) {
while (prv[cur] != -1 && nxt[prv[cur]] <= e && nxt[prv[cur]] != nxt[cur] && nxt[cur] < s) {
cur = prv[cur];
ans++;
}
cur = nxt[cur];
ans++;
}
if (cur > e) {
return -1;
}
return ans;
}