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"
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const int N = 2e5 + 5;
const int L = 18;
int prv[N], nxt[N], n, h[N], sparse[N][L], mx[4 * N];
vector<int> H;
int argmax(int x, int y) {
return H[x] > H[y] ? x : y;
}
void build_tree(int v, int l, int r) {
if (l + 1 == r) {
mx[v] = l;
return;
}
int m = l + r >> 1;
build_tree(v << 1, l, m), build_tree(v << 1 | 1, m, r);
mx[v] = argmax(mx[v << 1], mx[v << 1 | 1]);
}
int get_mx(int v, int l, int r, int L, int R) {
if (R <= l || r <= L || R <= L) return n;
if (L <= l && r <= R) return mx[v];
int m = l + r >> 1;
return argmax(get_mx(v << 1, l, m, L, R), get_mx(v << 1 | 1, m, r, L, R));
}
void init(int _n, std::vector<int> _H) {
n = _n;
H.swap(_H);
build_tree(1, 0, n);
H.push_back(-1);
vector<int> st;
FOR(i, 0, n) {
while (st.size() && H[st.back()] <= H[i]) st.pop_back();
prv[i] = (st.empty() ? -1 : st.back());
st.push_back(i);
}
st.clear();
for (int i = n - 1; i >= 0; i--) {
while (st.size() && H[st.back()] <= H[i]) st.pop_back();
nxt[i] = (st.empty() ? n : st.back());
st.push_back(i);
}
for (int i = n - 1; i >= 0; i--) h[i] = h[nxt[i]] + 1;
for (int i = 0; i < n; i++) {
int x = (prv[i] == -1 ? -1 : H[prv[i]]), y = (nxt[i] == n ? -1 : H[nxt[i]]);
if (y == -1 && x == -1) sparse[i][0] = i;
else sparse[i][0] = (x > y ? prv[i] : nxt[i]);
}
FOR(j, 1, L) FOR(i, 0, n) sparse[i][j] = sparse[sparse[i][j - 1]][j - 1];
}
int minimum_jumps(int A, int B, int C, int D) {
assert(C == D);
int x = get_mx(1, 0, n, max(A, prv[C] + 1), B + 1);
if (x == n || prv[C] >= x) return -1;
int t = 0;
for (int i = L - 1; i >= 0; i--) if (H[sparse[x][i]] < H[C]) {
x = sparse[x][i];
t |= 1 << i;
}
return t + h[x] - h[C];
}
Compilation message (stderr)
jumps.cpp: In function 'void build_tree(int, int, int)':
jumps.cpp:19:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
19 | int m = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'int get_mx(int, int, int, int, int)':
jumps.cpp:26:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
26 | int m = l + r >> 1;
| ~~^~~
# | 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... |