#include "jumps.h"
#include <bits/stdc++.h>
#include <vector>
using namespace std;
const int MAXN = 2e5 + 5;
int left_jump[19][MAXN], right_jump[18][MAXN], higher[19][MAXN];
vector<int> H;
int N;
void init(int n, std::vector<int> h)
{
stack<int> s;
N = n;
H.push_back(1e9);
for(int i = 0; i < h.size(); i++)
{
H.push_back(h[i]);
}
H.push_back(1e9);
N += 2;
for(int i = 0; i < N; i++)
{
while(!s.empty() && H[s.top()] < H[i])
{
s.pop();
}
if(s.empty())
{
left_jump[0][i] = i;
}
else
{
left_jump[0][i] = s.top();
}
s.push(i);
}
while(!s.empty())
{
s.pop();
}
for(int i = N - 1; i >= 0; i--)
{
while(!s.empty() && H[s.top()] < H[i])
{
s.pop();
}
if(s.empty())
{
right_jump[0][i] = i;
}
else
{
right_jump[0][i] = s.top();
}
s.push(i);
higher[0][i] = (H[right_jump[0][i]] >= H[left_jump[0][i]]) ? right_jump[0][i] : left_jump[0][i];
}
for(int i = 1; i < 19; i++)
{
for(int j = 0; j < N; j++)
{
left_jump[i][j] = left_jump[i - 1][left_jump[i - 1][j]];
right_jump[i][j] = right_jump[i - 1][right_jump[i - 1][j]];
higher[i][j] = higher[i - 1][higher[i - 1][j]];
}
}
}
int max_height(int l, int r)
{
int cur = l;
for(int i = 18; i >= 0; i--)
{
if(right_jump[i][cur] <= r)
{
cur = right_jump[i][cur];
}
}
return cur;
}
int minimum_jumps(int A, int B, int C, int D)
{
A++, B++, C++, D++;
if(B == C - 1)
{
// no middle stuff
return (right_jump[0][B] <= D) ? 1 : -1;
}
int middle_tallest_index = max_height(B + 1, C - 1);
if(H[B] > H[middle_tallest_index])
{
return (right_jump[0][B] <= D) ? 1 : -1;
}
int current = B;
for(int i = 18; i >= 0; i--)
{
if(A <= left_jump[i][current] && H[left_jump[i][current]] <= H[middle_tallest_index])
{
current = left_jump[i][current];
}
}
int ans = 0;
if(A <= left_jump[0][current])
{
if(right_jump[0][left_jump[0][current]] <= D)
{
return 1;
}
}
else
{
for(int i = 18; i >= 0; i--)
{
if(H[higher[i][current]] <= H[middle_tallest_index])
{
ans += (1 << i);
current = higher[i][current];
}
}
if(current == middle_tallest_index)
{
return (right_jump[0][current] <= D) ? ans + 1 : -1;
}
if(left_jump[0][current] > 0 && right_jump[0][left_jump[0][current]] <= D)
{
return ans + 2;
}
}
for(int i = 18; i >= 0; i--)
{
if(right_jump[i][current] < C)
{
ans += (1 << i);
current = right_jump[i][current];
}
}
ans++;
current = right_jump[0][current];
return (C <= current && current <= D) ? ans : -1;
}
Compilation message
jumps.cpp: In function 'void init(int, std::vector<int>)':
jumps.cpp:17:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for(int i = 0; i < h.size(); i++)
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
135 ms |
37884 KB |
Output is correct |
4 |
Correct |
1112 ms |
47260 KB |
Output is correct |
5 |
Correct |
1016 ms |
24152 KB |
Output is correct |
6 |
Correct |
1135 ms |
47292 KB |
Output is correct |
7 |
Correct |
918 ms |
32664 KB |
Output is correct |
8 |
Correct |
1018 ms |
47192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
0 ms |
592 KB |
Output is correct |
5 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
0 ms |
592 KB |
Output is correct |
5 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
0 ms |
592 KB |
Output is correct |
5 |
Correct |
41 ms |
37760 KB |
Output is correct |
6 |
Correct |
65 ms |
46524 KB |
Output is correct |
7 |
Correct |
25 ms |
24256 KB |
Output is correct |
8 |
Incorrect |
48 ms |
46524 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Incorrect |
265 ms |
21696 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Incorrect |
265 ms |
21696 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
135 ms |
37884 KB |
Output is correct |
4 |
Correct |
1112 ms |
47260 KB |
Output is correct |
5 |
Correct |
1016 ms |
24152 KB |
Output is correct |
6 |
Correct |
1135 ms |
47292 KB |
Output is correct |
7 |
Correct |
918 ms |
32664 KB |
Output is correct |
8 |
Correct |
1018 ms |
47192 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
0 ms |
592 KB |
Output is correct |
13 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |