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 N = 2e5;
const int K = 20;
int v[N + 2];
int l[N + 2][K];
int r[N + 2][K];
int p[N + 2][K];
stack <int> stk;
int en;
void init(int n, std::vector<int> H) {
int i,j;en = n;
for(i = 1;i <= n;i++){
v[i] = H[i - 1];
}
r[n + 1][0] = n + 1;
l[0][0] = 0;
for(i = 1;i <= n;i++){
while(!stk.empty() && v[stk.top()] < v[i]){
r[stk.top()][0] = i;stk.pop();
}
stk.push(i);
}
while(!stk.empty()){
r[stk.top()][0] = n + 1;
stk.pop();
}
for(i = n;i >= 1;i--){
while(!stk.empty() && v[stk.top()] < v[i]){
l[stk.top()][0] = i;stk.pop();
}
stk.push(i);
}
while(!stk.empty()){
l[stk.top()][0] = 0;
stk.pop();
}
v[0] = v[n + 1] = -2e9;
for(i = 0;i <= n + 1;i++){
if(v[l[i][0]] > v[r[i][0]]){
p[i][0] = l[i][0];
}else p[i][0] = r[i][0];
}
for(j = 1;j <= 19;j++){
for(i = 0;i <= n + 1;i++){
if(i > 0 && i < n + 1)p[i][j] = p[p[i][j - 1]][j - 1];
l[i][j] = l[l[i][j - 1]][j - 1];
r[i][j] = r[r[i][j - 1]][j - 1];
}
}
/*for(j = 0;j <= 19;j++){
for(i = 0;i <= n + 1;i++){
cout<<p[i][j]<<' '<<r[i][j]<<' '<<l[i][j]<<'\n';
}
}*/
}
int minimum_jumps(int a, int b, int c, int d){
a++;b++;c++;d++;
int x = b,i,r2 = 0;
int n = en;
for(i = 19;i >= 0;i--){
if(l[x][i] >= a && r[l[x][i]][0] <= d){
x = l[x][i];
}
}
//cout<<x<<'\n';
for(i = 19;i >= 0;i--){
if(1 <= p[x][i] && p[x][i] <= n && p[x][i] < c && r[p[x][i]][0] <= d){
r2+=(1<<i);
x = p[x][i];
}
}
//cout<<x<<'\n';
for(i = 19;i >= 0;i--){
if(r[x][i] < c){
x = r[x][i];
r2+=(1<<i);
}
}
//cout<<x<<'\n';
if(x < c){
x = r[x][0];
r2++;
}
//cout<<x<<'\n';
if(c <= x && x <= d)return r2;
return -1;
}
/**
7 3
3 2 1 6 4 5 7
4 4 6 6
1 3 5 6
0 1 2 2
**/
# | 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... |