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], lef[N], rig[N], inv[N], logs[N], a[N], NN, S1;
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) {
NN = n;
S1 = 1;
for (int i = 1; i <= n; i++) {
a[i] = A[i - 1];
S1 &= (a[i] == i);
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] = lef[i];
low[i][0] = rig[i];
}
else {
high[i][0] = rig[i];
low[i][0] = lef[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];
low[i][j] = low[low[i][j - 1]][j - 1];
}
}
}
int Dist(int p, int q) {
int d = 0;
for (int i = 17; i >= 0; i--) {
if (high[p][i] && a[high[p][i]] <= a[q]) {
d += (1 << i);
p = high[p][i];
}
}
for (int i = 17; i >= 0; i--) {
if (low[p][i] && a[low[p][i]] <= a[q]) {
d += (1 << i);
p = low[p][i];
}
}
if (p != q) return -1;
return d;
}
int minimum_jumps(int a, int b, int c, int d) {
a += 1; b += 1; c += 1; d += 1;
if (S1) return c - b;
if (c != d) {
queue<int> q;
vector<int> dist(NN + 1, inf);
for (int i = a; i <= b; i++) {
q.push(i);
dist[i] = 0;
}
while (!q.empty()) {
int i = q.front();
q.pop();
if (lef[i] > 0 && dist[lef[i]] > dist[i] + 1) {
q.push(lef[i]);
dist[lef[i]] = dist[i] + 1;
}
if (rig[i] > 0 && dist[rig[i]] > dist[i] + 1) {
q.push(rig[i]);
dist[rig[i]] = dist[i] + 1;
}
}
int ans = inf;
for (int i = c; i <= d; i++) smin(ans, dist[i]);
if (ans == inf) ans = -1;
return ans;
}
int koji = Query(c, d).first;
int l = a, r = b, gde = 0;
while (l <= r) {
int mid = l + r >> 1;
if (Query(mid, b).first < koji) {
gde = mid;
r = mid - 1;
}
else l = mid + 1;
}
if (gde == 0) return -1;
gde = Query(gde, b).second;
l = c, r = d;
int ret = -1;
while (l <= r) {
int mid = l + r >> 1;
int x = Dist(gde, Query(c, mid).second);
if (x != -1) {
ret = x;
r = mid - 1;
}
else l = mid + 1;
}
return ret;
}
Compilation message (stderr)
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:113:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
113 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp:125:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
125 | int mid = 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... |