#include "jumps.h"
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
const int logn = 19, N = 200001;
int h[N], jump[logn][N], R[N], L[N], spt[logn][N], n;
const int infI = 1e9 + 7;
bool okk = true;
int Min(int i, int j) {
return h[i] < h[j] ? i : j;
}
int Max(int i, int j) {
return h[i] > h[j] ? i : j;
}
int get_max(int l, int r) {
if (r <= l) return -1;
int sz = __lg(r - l);
return Max(spt[sz][l], spt[sz][r - (1 << sz)]);
}
void init(int nn, vector<int> H) {
n = nn;
for (int i = 0; i < n; ++i) {
h[i] = H[i];
okk &= (H[i] == i + 1);
}
vector<int> st;
for (int i = 0; i < n; ++i) {
while (!st.empty() && h[st.back()] < h[i]) {
R[st.back()] = i;
st.pop_back();
}
if (st.empty()) {
L[i] = -1;
} else {
L[i] = st.back();
}
st.push_back(i);
}
for (int i: st) {
R[i] = n;
}
st.clear();
for (int i = 0; i < n; ++i) {
if (R[i] == n)
jump[0][i] = L[i];
else if (L[i] == -1)
jump[0][i] = R[i];
else
jump[0][i] = Max(L[i], R[i]);
spt[0][i] = i;
}
for (int j = 1; j < logn; ++j) {
for (int i = 0; i < n; ++i) {
if (jump[j - 1][i] != -1)
jump[j][i] = jump[j - 1][jump[j - 1][i]];
else
jump[j][i] = -1;
}
}
for (int j = 1; j < logn; ++j) {
for (int i = 0; i + (1 << j) <= n; ++i) {
spt[j][i] = Max(spt[j - 1][i], spt[j - 1][i + (1 << (j - 1))]);
}
}
}
int minimum_jumps(int A, int B, int C, int D) {
if (okk) {
return C - B;
}
++D, ++B;
int mxcd = get_max(C, D);
int lx = A - 1, rx = C;
while (lx + 1 < rx) {
int m = (lx + rx) >> 1;
if (h[get_max(m, C)] < h[mxcd]) rx = m;
else lx = m;
}
if (rx >= B) {
return -1;
}
int i = get_max(rx, B);
int ans = 0;
for (int j = logn - 1; j > -1; --j) {
if (jump[j][i] == -1 || h[jump[j][i]] >= h[C]) continue;
ans += (1 << j);
i = jump[j][i];
}
assert(L[i] != C);
assert(i < C);
assert(R[i] == C);
return ans + 1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
113 ms |
25504 KB |
Output is correct |
4 |
Correct |
699 ms |
32148 KB |
Output is correct |
5 |
Correct |
686 ms |
16032 KB |
Output is correct |
6 |
Correct |
672 ms |
32072 KB |
Output is correct |
7 |
Correct |
639 ms |
21840 KB |
Output is correct |
8 |
Correct |
765 ms |
32072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
720 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
720 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
720 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Runtime error |
144 ms |
28752 KB |
Execution killed with signal 6 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Runtime error |
144 ms |
28752 KB |
Execution killed with signal 6 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
113 ms |
25504 KB |
Output is correct |
4 |
Correct |
699 ms |
32148 KB |
Output is correct |
5 |
Correct |
686 ms |
16032 KB |
Output is correct |
6 |
Correct |
672 ms |
32072 KB |
Output is correct |
7 |
Correct |
639 ms |
21840 KB |
Output is correct |
8 |
Correct |
765 ms |
32072 KB |
Output is correct |
9 |
Runtime error |
2 ms |
720 KB |
Execution killed with signal 6 |
10 |
Halted |
0 ms |
0 KB |
- |