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>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 2;
const int inf = 2e9;
int low[N][18], high[N][18], rjmp[N][18], mxlow[N][18], mxhigh[N][18], lef[N], rig[N], inv[N], logs[N], a[N];
pair<int, int> rmq[N][18];
auto Query(int l, int r) {
int o = logs[r - l + 1];
return max(rmq[l][o], rmq[r - (1 << o) + 1][o]);
}
void init(int n, vector<int> A) {
for (int i = 1; i <= n; i++) {
a[i] = A[i - 1];
rmq[i][0] = {a[i], i};
inv[a[i]] = i;
}
for (int j = 1; j < 18; j++) {
for (int i = 1; i <= n - (1 << j) + 1; i++) {
rmq[i][j] = max(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
}
}
int z = 0;
for (int i = 1; i <= n; i++) {
if (i == (1 << (z + 1))) z += 1;
logs[i] = z;
}
stack<int> s;
for (int i = 1; i <= n; i++) {
while (!s.empty() && a[s.top()] < a[i]) s.pop();
if (!s.empty()) lef[i] = s.top();
s.push(i);
}
while (!s.empty()) s.pop();
for (int i = n; i >= 1; i--) {
while (!s.empty() && a[s.top()] < a[i]) s.pop();
if (!s.empty()) rig[i] = s.top();
s.push(i);
}
for (int i = 1; i <= n; i++) {
if (a[lef[i]] > a[rig[i]]) {
high[i][0] = mxhigh[i][0] = lef[i];
low[i][0] = mxlow[i][0] = rig[i];
}
else {
high[i][0] = mxhigh[i][0] = rig[i];
low[i][0] = mxlow[i][0] = lef[i];
}
rjmp[i][0] = rig[i];
}
for (int ii = n; ii >= 1; ii--) {
int i = inv[ii];
for (int j = 1; j < 18; j++) {
high[i][j] = high[high[i][j - 1]][j - 1];
mxhigh[i][j] = max(mxhigh[i][j - 1], mxhigh[high[i][j - 1]][j - 1]);
low[i][j] = low[low[i][j - 1]][j - 1];
mxlow[i][j] = max(mxlow[i][j - 1], mxlow[low[i][j - 1]][j - 1]);
}
}
for (int j = 1; j < 18; j++) {
for (int i = 1; i <= n - (1 << j) + 1; i++) {
rjmp[i][j] = rjmp[rjmp[i][j - 1]][j - 1];
}
}
}
int minimum_jumps(int A, int b, int c, int d) {
A += 1; b += 1; c += 1; d += 1;
int koji = Query(c, d).second;
int l = A, r = b, gde = 0;
while (l <= r) {
int mid = l + r >> 1;
if (Query(mid, b).first < a[koji]) {
gde = mid;
r = mid - 1;
}
else l = mid + 1;
}
if (gde == 0) return -1;
gde = Query(gde, b).second;
int ans = 0;
for (int i = 0; ; ) {
int x = low[gde][i]; int y = high[gde][i];
if (y && mxlow[gde][i] < c && mxhigh[gde][i] < c && a[y] < a[koji]) {
ans += (1 << i);
gde = y;
}
else break;
}
for (int i = 17; i >= 0; i--) {
if (rjmp[gde][i] && rjmp[gde][i] < c) {
ans += (1 << i);
gde = rjmp[gde][i];
}
}
gde = rjmp[gde][0];
ans += 1;
if (gde < c || gde > d) return -1;
return ans;
}
Compilation message (stderr)
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:76:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
76 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp:87:13: warning: unused variable 'x' [-Wunused-variable]
87 | int x = low[gde][i]; int y = high[gde][i];
| ^
# | 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... |