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>
#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 (stderr)
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 |
---|
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... |