#include "jumps.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
const int B = 20;
int n;
vector<vector<int>> build(vector<vector<int>> g,vector<int>rt){
vector<vector<int>> up(n+1);
function<void(int,int )> dfs = [&](int v,int pr) {
up[v].resize(B);
up[v][0] = pr;
for(int i = 1;i < B;i++){
up[v][i] = up[up[v][i - 1]][i - 1];
}
for(int to:g[v]) {
dfs(to,v);
}
};
for(int i:rt) {
dfs(i,i);
}
return up;
}
vector<vector<int>> up,up1;
const int maxn = (int)2e5 + 12;
int l[maxn],r[maxn];
vector<int> h;
void init(int N, vector<int> H) {
h = H;
n = N;
vector<int> st,rt;
vector<vector<int>> G(n);
for(int i = 0;i < N;i++) {
while(!(int)st.empty() && H[st.back()] < H[i]) {
st.pop_back();
}
if(st.empty()){
rt.push_back(i);
l[i] = -1;
}else {
G[st.back()].push_back(i);
l[i] = st.back();
}
st.push_back(i);
}
up = build(G,rt);
for(int i = 0;i < n;i++){
G[i].clear();
}
rt.clear();
st.clear();
for(int i = N - 1;i >= 0;i--){
while(!st.empty() && H[st.back()] < H[i]){
st.pop_back();
}
if(st.empty()){
rt.push_back(i);
r[i] = -1;
}else {
G[st.back()].push_back(i);
r[i] = st.back();
}
st.push_back(i);
}
up1 = build(G,rt);
}
bool check(int ver,int C,int D){
for(int i = B - 1;i >= 0;i--){
if(up1[ver][i] < C){
ver = up1[ver][i];
}
}
ver = up1[ver][0];
return (ver >= C && ver <= D);
}
int go(int ver,int C,int D) {
if(ver >= C && ver <= D) return 0;
if(r[ver] >= C && r[ver] <= D) return 1;
if(l[ver] == -1 || h[r[ver]] > h[l[ver]] || !check(l[ver],C,D)){
return go(r[ver],C,D) + 1;
}
return go(l[ver],C,D) + 1;
}
int minimum_jumps(int A, int B, int C, int D) {
// int res = 1e9;
int ver = B;
for(int i = 19;i >= 0;i--){
if(up[ver][i] >= A && check(ver,C,D) != 0){
ver = up[ver][i];
}
}
if(!check(ver,C,D)){
return -1;
}
return go(ver,C,D);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1066 ms |
72084 KB |
Output is correct |
4 |
Execution timed out |
4098 ms |
90324 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
87 ms |
54932 KB |
Output is correct |
6 |
Correct |
126 ms |
68096 KB |
Output is correct |
7 |
Correct |
57 ms |
35128 KB |
Output is correct |
8 |
Correct |
104 ms |
68092 KB |
Output is correct |
9 |
Correct |
16 ms |
10428 KB |
Output is correct |
10 |
Correct |
105 ms |
68092 KB |
Output is correct |
11 |
Correct |
115 ms |
90204 KB |
Output is correct |
12 |
Incorrect |
111 ms |
85988 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
196 ms |
31216 KB |
Output is correct |
5 |
Correct |
813 ms |
68088 KB |
Output is correct |
6 |
Correct |
491 ms |
11600 KB |
Output is correct |
7 |
Correct |
792 ms |
68216 KB |
Output is correct |
8 |
Correct |
432 ms |
23552 KB |
Output is correct |
9 |
Correct |
785 ms |
68092 KB |
Output is correct |
10 |
Execution timed out |
4034 ms |
90460 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
196 ms |
31216 KB |
Output is correct |
5 |
Correct |
813 ms |
68088 KB |
Output is correct |
6 |
Correct |
491 ms |
11600 KB |
Output is correct |
7 |
Correct |
792 ms |
68216 KB |
Output is correct |
8 |
Correct |
432 ms |
23552 KB |
Output is correct |
9 |
Correct |
785 ms |
68092 KB |
Output is correct |
10 |
Execution timed out |
4034 ms |
90460 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1066 ms |
72084 KB |
Output is correct |
4 |
Execution timed out |
4098 ms |
90324 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |