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"
//OP
#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define pi pair<int, int >
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define eb emplace_back
#define mset multiset
#define F first
#define S second
#define meta int M = (L + R) / 2, x = 2 * id + 1, y = x + 1
using namespace std;
bool one;
struct tree {
int n;
int lj[200005];
int rj[200005];
int J[200005][20];
int J2[200005][20];
vector<int> h;
pi t[800005];
void build(int id, int L, int R) {
if (L == R) {t[id] = {h[L], L}; return ;}
meta;
build(x, L, M);
build(y, M + 1, R);
t[id] = max(t[x], t[y]);
} void build() {build(0, 0, n-1); return ;}
pi query(int id, int L, int R, int l, int r) {
if (R < l || r < L) return {-2e9, -1};
if (l <= L && r >= R) return t[id];
meta;
return max(query(x, L, M, l, r), query(y, M + 1, R, l, r));
} pi query(int L, int R) {return query(0, 0, n-1, L, R);}
void take_J(vector<int> H) {
stack<int> st;
for (int i = 0; i < n; i++) {
while (!st.empty() && H[st.top()] < H[i]) st.pop();
if (st.empty()) lj[i] = -1;
else lj[i] = st.top();
st.push(i);
}
while (!st.empty()) st.pop();
for (int i = n-1; i >= 0; i--) {
while (!st.empty() && H[st.top()] < H[i]) st.pop();
if (st.empty()) rj[i] = n;
else rj[i] = st.top();
st.push(i);
}
}
void set_J() {
for (int i = 0; i < n; i++) {
int mx = lj[i], mx2 = rj[i];
if (mx == -1) mx = n;
if (h[mx] < h[mx2]) swap(mx, mx2);
J[i][0] = mx;
J2[i][0] = mx2;
}
J[n][0] = J2[n][0] = n;
for (int j = 1; j < 20; j++)
for (int i = 0; i <= n; i++)
J[i][j] = J[J[i][j-1]][j-1];
for (int j = 1; j < 20; j++)
for (int i = 0; i <= n; i++)
J2[i][j] = J2[J2[i][j-1]][j-1];
}
int count(int A, int B, int C) {
A = query(max(A, lj[C] + 1), B).S;
if (A == -1) return -1;
int ans = 0;
for (int i = 19; i >= 0; i--)
if (h[J[A][i]] < h[C]) {A = J[A][i]; ans += (1 << i);}
if (J[A][0] == C) return ans + 1;
for (int i = 19; i >= 0; i--)
if (h[J2[A][i]] < h[C]) {A = J2[A][i]; ans += (1 << i);}
if (J2[A][0] == C) return ans + 1;
return -1;
}
int count(int A, int B, int C, int D) {
A = query(max(A, lj[query(C, D).S] + 1), B).S;
if (A == -1) return -1;
int ans = 0;
for (int i = 19; i >= 0; i--)
if (h[J[A][i]] < h[C]) {A = J[A][i]; ans += (1 << i);}
if (J[A][0] >= C) return ans + 1;
for (int i = 19; i >= 0; i--)
if (h[J2[A][i]] < h[C]) {A = J2[A][i]; ans += (1 << i);}
if (J2[A][0] >= C) return ans + 1;
return -1;
}
} T;
void init(int N, std::vector<int> H) {
T.n = N; T.h = H;
T.take_J(H);
T.build();
T.h.pb(N + 1);
T.set_J();
one = 1;
for (int i = 0; i < N; i++) if (H[i] != i+1) {one = 0; break;}
}
int minimum_jumps(int A, int B, int C, int D) {
if (C == D) return T.count(A, B, C);
if (one) return C-B;
return T.count(A, B, C, D);
}
//ED
# | 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... |