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"
#ifndef EVAL
#include "stub.cpp"
#endif
#include "bits/stdc++.h"
using namespace std;
const int B = 205;
const int N = 2e5 + 5;
int mx[N][20], mn[N][20], h[N];
int lx[N], rx[N];
void init(int n, vector<int> H){
for(int i=0;i<n;i++){
h[i] = H[i];
}
vector<int> ss;
for(int i=0;i<n;i++){
while(!ss.empty() && h[ss.back()] < h[i]) ss.pop_back();
if(!ss.empty()) lx[i] = ss.back();
else lx[i] = n;
ss.push_back(i);
} ss.clear();
for(int i=n-1;~i;i--){
while(!ss.empty() && h[ss.back()] < h[i]) ss.pop_back();
if(!ss.empty()) rx[i] = ss.back();
else rx[i] = n;
ss.push_back(i);
} ss.clear();
h[n] = N;
for(int i=0;i<n;i++){
if(h[lx[i]] < h[rx[i]]){
swap(lx[i], rx[i]);
}
mn[i][0] = rx[i];
mx[i][0] = lx[i];
}
mn[n][0] = mx[n][0] = n;
for(int i=0;i<n;i++) cout<<mn[i][0]<<" ";
cout<<"\n";
for(int i=0;i<n;i++) cout<<mx[i][0]<<" ";
cout<<"\n";
for(int j=1;j<20;j++){
for(int i=0;i<=n;i++){
mn[i][j] = mn[mn[i][j-1]][j-1];
mx[i][j] = mx[mx[i][j-1]][j-1];
}
}
}
int minimum_jumps(int a, int b, int c, int d) {
int res = 0;
for(int i=19;~i;i--){
if(h[mx[a][i]] <= h[c]){
//~ cout<<mx[a][i]<<" "<<h[mx[a][i]]<<" "<<h[c]<<"\n";
//~ cout<<i<<"\n";
a = mx[a][i], res += (1 << i);
}
}
for(int i=19;~i;i--){
if(h[mn[a][i]] <= h[c]){
//~ cout<<mx[a][i]<<" "<<h[mx[a][i]]<<" "<<h[c]<<"\n";
a = mn[a][i], res += (1 << i);
}
}
if(a != c) return -1;
return res;
}
/*
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... |