#include "bits/stdc++.h"
#include "jumps.h"
// #include "stub.cpp"
using namespace std;
#define SZ(s) (int)s.size()
const int N = 2e5 + 5;
int n, x[N][30], m[N][30], sp[N][30];
vector <int> h, v[N];
bool tr;
void init(int N1, vector<int> H) {
n = N1, h = H;
vector <int> v1;
for(int i = 0; i < n; i++) {
while(SZ(v1) and h[v1.back()] < h[i]) {
v1.pop_back();
}
if(SZ(v1)) v[i].push_back(v1.back());
v1.push_back(i);
}
v1.clear();
for(int i = n-1; i >= 0; i--) {
while(SZ(v1) and h[v1.back()] < h[i]) {
v1.pop_back();
}
if(SZ(v1)) v[i].push_back(v1.back());
v1.push_back(i);
}
h.push_back(2*n);
x[n][0] = n, m[n][0] = n;
for(int i = 0; i < n; i++) {
// sort(v[i].begin(), v[i].end());
if(SZ(v[i]) == 2) x[i][0] = (h[v[i][1]] > h[v[i][0]] ? v[i][1] : v[i][0]), m[i][0] = (h[v[i][1]] > h[v[i][0]] ? v[i][0] : v[i][1]);
else if(SZ(v[i]) == 1) x[i][0] = v[i][0], m[i][0] = v[i][0];
else x[i][0] = n, m[i][0] = n;
}
for(int j = 1; j <= 20; j++) {
for(int i = 0; i <= n; i++) {
x[i][j] = x[x[i][j-1]][j-1];
m[i][j] = m[m[i][j-1]][j-1];
}
}
for(int i = 0; i < n; i++) {
sp[i][0] = i;
}
for(int j = 1; j <= 20; j++) {
for(int i = 0; i < n; i++) {
if(i - (1<<j) + 1 < 0) continue;
sp[i][j] = (h[sp[i][j-1]] > h[sp[i-(1<<j-1)][j-1]] ? sp[i][j-1] : sp[i-(1<<j-1)][j-1]);
}
}
return;
}
int minimum_jumps(int a, int b, int c, int d) {
int ans = 0, ind = -1;
for(int i = 20; i >= 0; i--) {
if(b - (1<<i) + 1 < a) continue;
if(h[sp[b][i]] < h[c]) (~ind ? (h[sp[b][i]] > h[ind] ? ind = sp[b][i] : ind) : ind = sp[b][i]), b -= (1<<i);
}
if(ind == -1) return ind;
a = ind;
for(int i = 20; i >= 0; i--) {
if(h[x[a][i]] <= h[c]) a = x[a][i], ans += (1<<i);
}
for(int i = 20; i >= 0; i--) {
if(h[m[a][i]] <= h[c]) a = m[a][i], ans += (1<<i);
}
return (a != c ? -1 : ans);
}
# | 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... |