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 jr[200005][20];
int jl[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()) jl[i][0] = lj[i] = -1;
else jl[i][0] = 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()) jr[i][0] = rj[i] = n;
else jr[i][0] = rj[i] = st.top();
st.push(i);
}
}
void set_J() {
jr[n][0] = jl[n][0] = n;
for (int j = 1; j < 20; j++)
for (int i = 0; i <= n; i++)
jr[i][j] = jr[jr[i][j-1]][j-1];
for (int j = 1; j < 20; j++)
for (int i = 0; i <= n; i++)
jl[i][j] = jl[jl[i][j-1]][j-1];
}
int count(int A, int B) {
if (h[A] > h[B]) return -1;
int ans = 0;
for (int i = 19; i >= 0; i--)
if (h[jr[A][i]] < h[B]) {A = jr[A][i]; ans += (1 << i);}
if (jr[A][0] == B) return ans + 1;
return -1;
}
int count(int A, int B, int C) {
int a = A, b = B;
A = query(max(A, lj[C] + 1), B).S;
if (A == -1) return -1;
int ans = count(A, C);
for (int i = 19; i >= 0; i--) {
if (jl[A][i] == -1) continue ;
int x = count(jl[A][i], C);
if (x != -1) ans = min(ans, (1 << i) + x);
}
return ans;
}
int count(int A, int B, int C, int D) {
int ans = 2e9;
int mn = query(B+1, C-1).F;
pi s = query(C, D);
while (s.F > mn) {
if (s.S == -1) break ;
int x = count(A, B, s.S);
if (x != -1) ans = min(ans, x);
s = query(C, s.S-1);
}
return (ans == 2e9 ? -1 : ans);
}
} 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
Compilation message (stderr)
jumps.cpp: In member function 'int tree::count(int, int, int)':
jumps.cpp:85:9: warning: unused variable 'a' [-Wunused-variable]
85 | int a = A, b = B;
| ^
jumps.cpp:85:16: warning: unused variable 'b' [-Wunused-variable]
85 | int a = A, b = B;
| ^
# | 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... |