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"
using namespace std;
template<typename T> void ono_min(T &MIN, T CMP) { if(MIN > CMP) MIN = CMP; }
template<typename T> void ono_max(T &MAX, T CMP) { if(MAX < CMP) MAX = CMP; }
typedef pair<int, int> pii;
#define ff first
#define ss second
#define MP make_pair
const int mxk = 32;
const int mxn = 2e5 + 10;
int L[mxn], R[mxn], n;
vector<int> adj[mxn];
int dis[mxn], h[mxn];
int mx[mxk][mxn];
int mn[mxk][mxn];
pii ll[mxk][mxn];
bool mx_valid(int k, int i) { return 1 <= mx[k][i] && mx[k][i] <= n; }
bool mn_valid(int k, int i) { return 1 <= mn[k][i] && mn[k][i] <= n; }
int solve(int s, int f);
void init(int N, vector<int> H) {
memset(L, -1, sizeof L);
memset(R, -1, sizeof R);
n = N;
stack<int> st; st.push(0);
for(int i = 1; i < N; i++) {
while(st.size() && H[st.top()] <= H[i])
st.pop();
if(st.size())
L[i] = st.top();
st.push(i);
}
stack<int> en; en.push(0);
for(int i = N - 1; i >= 0; i--) {
while(en.size() && H[en.top()] <= H[i])
en.pop();
if(en.size())
R[i] = en.top();
en.push(i);
}
for(int i = 0; i < N; i++) {
h[i] = H[i];
if(L[i] > -1) {
adj[i].push_back(L[i]);
if(mn[0][H[i]] == 0)
mn[0][H[i]] = H[L[i]];
ono_min(mn[0][H[i]], H[L[i]]);
ono_max(mx[0][H[i]], H[L[i]]);
ll[0][i] = MP(H[L[i]], L[i]);
}
if(R[i] > -1) {
adj[i].push_back(R[i]);
if(mn[0][H[i]] == 0)
mn[0][H[i]] = H[R[i]];
ono_min(mn[0][H[i]], H[R[i]]);
ono_max(mx[0][H[i]], H[R[i]]);
}
}
for(int k = 1; k < mxk; k++)
for(int i = 1; i <= n; i++) {
int mnj = mn[k - 1][i];
int mxj = mx[k - 1][i];
if(mnj) mn[k][i] = mn[k - 1][mnj];
if(mxj) mx[k][i] = mx[k - 1][mxj];
}
for(int k = 1; k < mxk; k++)
for(int i = 0; i < n; i++) {
pii p = ll[k - 1][i];
if(p.ss) ll[k][i] = ll[k - 1][p.ss];
}
// for(int i = 0; i < n; i++) {
// for(int k = 0; (1 << k) <= n; k++)
// cout << i << ' ' << k << ": " << ll[k][i].ff << ' ' << ll[k][i].ss << endl;
// }
// cout << endl;
// for(int i = 1; i < n; i++)
// for(int j = i + 1; j <= n; j++)
// cout << i << ' ' << j << " = " << solve(i, j) << endl;
}
int solve(int s, int f) {
int ans = 0;
for(int k = mxk - 1; k >= 0; k--)
if(mx[k][s] && mx[k][s] <= f) {
s = mx[k][s];
ans += (1 << k);
}
for(int k = mxk - 1; k >= 0; k--)
if(mn[k][s] && mn[k][s] <= f) {
s = mn[k][s];
ans += (1 << k);
}
if(s == f) return ans;
return -1;
}
int minimum_jumps(int A, int B, int C, int D) {
if(C != D) exit(1);
if(h[B] > h[C]) return -1;
if(A == B) return solve(h[A], h[C]);
// cout << "sussy\n";
// cout << A << ' ' << B << endl;
int cur = B;
for(int i = mxk - 1; i >= 0; i--) {
int hi = ll[i][cur].ff;
int id = ll[i][cur].ss;
if(hi == 0) continue;
if(hi >= h[D]) continue;
if(id < A) continue;
cur = id;
// cout << cur << ' ';
}
// cout << cur << ' ' << h[cur] << endl;
if(h[cur] > h[C]) return -1;
return solve(h[cur], 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... |