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>
//#include "jumps.h"
#define FIXED_FLOAT(x) std::fixed <<std::setprecision(20) << (x)
#define all(v) (v).begin(), (v).end()
using namespace std;
#define forn(i,n) for (int i = 0; i < (n); ++i)
#define rforn(i, n) for(int i = (n) - 1;i >= 0;--i)
#define sz(x) (int)x.size()
#define ff first
#define se second
#define mp make_pair
using ll = long long;
int mod = (ll)1e9 + 7;
const int INF = 1e9 + 1;
const double eps = 1e-7;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template<class T, size_t SZ> using AR = array<T, SZ>;
template<class T> using PR = pair<T, T>;
template <typename XPAX>
bool ckma(XPAX &x, XPAX y) {
return (x < y ? x = y, 1 : 0);
}
template <typename XPAX>
bool ckmi(XPAX &x, XPAX y) {
return (x > y ? x = y, 1 : 0);
}
const int mxn = 2e5+2;
const int G = 20;
int F[mxn][G], L[mxn][G], R[mxn][G], hi[mxn][G];
V<int> H;
int n;
void init(int N, V<int> a) {
H = a;
H.insert(H.begin(), INF);
H.push_back(INF);
n = N;
H[N] = N;
stack<int> st;
forn(i, N+2) {
while(sz(st) && H[st.top()] <= H[i])
st.pop();
L[i][0] = sz(st) ? st.top() : i;
st.push(i);
}
while(sz(st))st.pop();
rforn(i, N+2) {
while(sz(st) && H[st.top()] <= H[i])
st.pop();
R[i][0] = sz(st) ? st.top() : i;
st.push(i);
}
forn(i,n+2) F[i][0] = (H[L[i][0]] > H[R[i][0]] ? L[i][0] : R[i][0]);
for(int j = 1;j < G;++j) forn(i,n+2) {
L[i][j] = L[L[i][j-1]][j-1];
R[i][j] = R[R[i][j-1]][j-1];
F[i][j] = F[F[i][j-1]][j-1];
}
}
int minimum_jumps(int A, int B, int C, int D) {
++A, ++B, ++C, ++D;
if(B + 1 == C) {
return R[B][0] <= D ? 1 : -1;
}
int md = B + 1;
// md is biggest element in (B,C)
// we have to cross through md eventually
rforn(i, G) if(R[md][i] < C)md = R[md][i];
if(H[B] > H[md])
return R[md][0] <= D ? 1 : -1;
int go = B;
// the optimal is where the H[index] in greatest integer < H[mid] in range [A, B]
// go is where we start from
rforn(i, G) if(L[go][i] >= A && H[L[go][i]] < H[md]) go = L[go][i];
int ret = 0;
if(L[go][0] >= A) {
if(R[L[go][0]][0] <= D)return 1;
}
else {
rforn(i, G) if(H[hi[go][i]] <= H[md]) {
go = hi[go][i];
ret += 1 << i;
}
if(go == md)return ret+1;
else if(L[go][0] && R[L[go][0]][0] <= D)return ret+2;
}
rforn(i, G) if(R[go][i] < C) {
ret += 1 << i;
go = R[go][i];
}
go = R[go][0];
if(C <= go && go <= D)return ret+1;
else return -1;
}
/*
int main() {
init(7, {3, 2, 1, 6, 4, 5, 7});
cout << minimum_jumps(3, 4, 6, 6) << '\n';
}
*/
# | 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... |