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 "jumps.h"
#include <bits/stdc++.h>
using namespace std;
const int maxN = 2e5 + 20;
pair<int, int> range[maxN];
int par[maxN];
int pos[maxN];
int ch[maxN][2];
int H[maxN];
int N;
int node_cnt;
int A, B, C, D;
int start_u;
int time_in[maxN];
int time_out[maxN];
int jump[maxN];
int T;
int dfs1(int lt, int rt) {
if (lt > rt) {
return -1;
}
int u = node_cnt++;
range[u] = {lt, rt};
pair<int, int> mx = {H[lt], lt};
for (int i = lt; i <= rt; i++) {
mx = max(mx, make_pair(H[i], i));
}
int mt = mx.second;
pos[u] = mt;
ch[u][0] = dfs1(lt, mt - 1);
ch[u][1] = dfs1(mt + 1, rt);
if (ch[u][0] != -1) {
par[ch[u][0]] = u;
}
if (ch[u][1] != -1) {
par[ch[u][1]] = u;
}
return u;
}
void dfs4(int u, int cur_0, int cur_1) {
time_in[u] = ++T;
if (ch[u][0] != -1) {
jump[ch[u][0]] = cur_1;
dfs4(ch[u][0], u, cur_1);
}
if (ch[u][1] != -1) {
jump[ch[u][1]] = cur_0;
dfs4(ch[u][1], cur_0, u);
}
time_out[u] = ++T;
}
bool anc(int u, int v) {
return (u != -1 && v != -1) && (time_in[u] <= time_in[v] && time_out[v] <= time_out[u]);
}
void init(int _N, vector<int> _H) {
N = _N;
for (int i = 0; i < N; i++) {
H[i] = _H[i];
}
dfs1(0, N - 1);
dfs4(0, -1, -1);
}
void dfs3(int u) {
if (u == -1) {
return;
}
int mt = pos[u];
if (B < mt) {
dfs3(ch[u][0]);
}
else if (A > mt) {
dfs3(ch[u][1]);
}
else {
start_u = u;
return;
}
}
void dfs2(int u) {
if (u == -1) {
return;
}
int mt = pos[u];
if (D < mt) {
dfs2(ch[u][0]);
}
else if (C > mt) {
dfs2(ch[u][1]);
}
else {
dfs3(u);
}
}
int minimum_jumps(int _A, int _B, int _C, int _D) {
A = _A;
B = _B;
C = _C;
D = _D;
start_u = -1;
dfs2(0);
if (start_u == -1) {
return -1;
}
int prv = -1;
int streak = 0;
int cnt = 0;
int fin = start_u;
while (D < pos[fin] || C > pos[fin]) {
fin = par[fin];
}
int cur = start_u;
int res = 0;
while (cur != fin) {
if (anc(fin, jump[cur])) {
cur = jump[cur];
}
else {
cur = par[cur];
}
res++;
}
return res;
}
Compilation message (stderr)
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:113:6: warning: unused variable 'prv' [-Wunused-variable]
113 | int prv = -1;
| ^~~
jumps.cpp:114:6: warning: unused variable 'streak' [-Wunused-variable]
114 | int streak = 0;
| ^~~~~~
jumps.cpp:115:6: warning: unused variable 'cnt' [-Wunused-variable]
115 | int cnt = 0;
| ^~~
# | 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... |