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>
using namespace std;
const int LOGN = 18;
int n;
vector<int> h;
vector<vector<int>> l, r, best;
void init(int _n, vector<int> _h) {
n = _n;
h = _h;
l.assign(n, vector<int>(LOGN, 0));
r.assign(n, vector<int>(LOGN, 0));
best.assign(n, vector<int>(LOGN, 0));
l[0][0] = -1;
for (int i = 1; i < n; ++i) {
l[i][0] = i - 1;
while (l[i][0] != -1 && h[i] > h[l[i][0]]) {
l[i][0] = l[l[i][0]][0];
}
}
r[n - 1][0] = -1;
for (int i = n - 2; i >= 0; --i) {
r[i][0] = i + 1;
while (r[i][0] != -1 && h[i] > h[r[i][0]]) {
r[i][0] = r[r[i][0]][0];
}
}
for (int i = 0; i < n; ++i) {
if (l[i][0] == -1 || r[i][0] == -1) {
best[i][0] = max(l[i][0], r[i][0]);
} else if (h[l[i][0]] > h[r[i][0]]) {
best[i][0] = l[i][0];
} else {
best[i][0] = r[i][0];
}
}
for (int i = 0; i < n; ++i) {
for (int j = 1; j < LOGN; ++j) {
l[i][j] = (l[i][j - 1] == -1? -1 : l[l[i][j - 1]][j - 1]);
}
}
for (int i = n - 1; i >= 0; --i) {
for (int j = 1; j < LOGN; ++j) {
r[i][j] = (r[i][j - 1] == -1? -1 : r[r[i][j - 1]][j - 1]);
}
}
for (int j = 1; j < LOGN; ++j) {
for (int i = 0; i < n; ++i) {
best[i][j] = (best[i][j - 1] == -1? -1 : best[best[i][j - 1]][j - 1]);
}
}
}
int getMax(int u, int v) {
if (u > v) {
return 0;
}
int p = u;
for (int i = LOGN - 1; i >= 0; --i) {
if (r[p][i] != -1 && r[p][i] <= v) {
p = r[p][i];
}
}
return h[p];
}
int minimum_jumps(int a, int b, int c, int d) {
int maxR = getMax(c, d);
int maxC = getMax(b + 1, c - 1);
int s = b;
// find optimal s
for (int i = LOGN - 1; i >= 0; --i) {
if (l[s][i] != -1 && l[s][i] >= a && h[l[s][i]] < maxR) {
s = l[s][i];
}
}
int res = 0;
for (int i = LOGN - 1; i >= 0; --i) {
if (best[s][i] != -1 && h[best[s][i]] < maxC) {
res += (1 << i);
s = best[s][i];
}
}
// l-r
if (l[s][0] != -1 && c <= r[l[s][0]][0] && r[l[s][0]][0] <= d) {
return res + 2;
}
// r-r-r...
for (int i = LOGN - 1; i >= 0; --i) {
if (r[s][i] != -1 && h[r[s][i]] <= maxC) {
res += (1 << i);
s = r[s][i];
}
}
if (c <= r[s][0] && r[s][0] <= d) {
return res + 1;
}
return -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... |