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 N = 200200;
const int LG = 18;
#define db(x) cerr << #x << " = " << x << endl
int n, l[N], r[N], toL[N][LG], toR[N][LG], maxH[N][LG];
vector<int> h;
void initEdges()
{
vector<pair<int, int>> sortedH;
for (int i = 0; i < n; i++)
sortedH.push_back({h[i], i});
sort(sortedH.begin(), sortedH.end(), greater<pair<int, int>>());
set<int> s;
s.insert(-1);
s.insert(n);
for (auto [_, i] : sortedH)
{
auto it = s.lower_bound(i);
r[i] = *it;
l[i] = *(--it);
s.insert(i);
}
}
void constructTable()
{
memset(toL, -1, sizeof toL);
memset(toR, -1, sizeof toR);
for (int i = 0; i < n; i++)
{
toL[i][0] = l[i];
toR[i][0] = r[i];
maxH[i][0] = h[i];
}
for (int j = 0; j + 1 < LG; j++)
for (int i = 0; i < n; i++)
{
if (toL[i][j] >= 0)
toL[i][j + 1] = toL[toL[i][j]][j];
if (toR[i][j] >= 0)
toR[i][j + 1] = toR[toR[i][j]][j];
if (i + (1 << j) < n)
maxH[i][j + 1] = max(maxH[i][j], maxH[i + (1 << j)][j]);
}
}
void init(int N, vector<int> H) {
n = N;
h = H;
h[n] = 0;
initEdges();
constructTable();
}
int getMaxH(int l, int r)
{
int bit = 31 - __builtin_clz(r - l + 1);
return max(maxH[l][bit], maxH[r - (1 << bit) + 1][bit]);
}
int between(int x, int y, int z)
{
return y <= x && x <= z;
}
int calcDist(int s, int from, int maxDestH)
{
int dist = 0;
for (int i = LG - 1; i >= 0; i--)
{
int x = toR[s][i];
if (x >= 0 && x < from && h[x] < maxDestH)
{
dist |= 1 << i;
s = x;
}
}
return h[s] <= maxDestH ? dist + 1 : n;
}
int minimum_jumps(int A, int B, int C, int D) {
int s = B, maxDestH = getMaxH(C, D);
for (int i = LG - 1; i >= 0; i--)
{
int x = toL[s][i];
if (x >= A && x < n && h[x] < maxDestH)
s = x;
}
if (h[s] > maxDestH)
return -1;
if (between(l[s], C, D) || between(r[s], C, D))
return 1;
int ans = calcDist(s, C, maxDestH);
if (l[s] >= 0 && l[s] < n)
ans = min(ans, calcDist(l[s], C, maxDestH) + 1);
return ans < n ? ans : -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... |