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 <bits/stdc++.h>
#include "jumps.h"
//#define int long long
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
const int N=2e5+5;
int jmp_high[N][20],jmp_low[N][20];
vector <int> h;
int n;
void init(int N,vector<int> H) {
stack <int> st;
n=N;
for(int i=0;i<N;i++)h.pb(H[i]);
h.pb(1e9);H.pb(1e9);
vector <int> l(N+1,N),r(N+1,N);
for(int i=0;i<N;i++){
while(!st.empty() && H[st.top()]<H[i]){
r[st.top()]=i;
st.pop();
}
st.push(i);
}
while(!st.empty())st.pop();
for(int i=N-1;i>=0;i--){
while(!st.empty() && H[st.top()]<H[i]){
l[st.top()]=i;
st.pop();
}
st.push(i);
}
for(int i=0;i<=N;i++){
if(H[l[i]]>H[r[i]]){
jmp_high[i][0]=l[i];
jmp_low[i][0]=r[i];
}
else{
jmp_high[i][0]=r[i];
jmp_low[i][0]=l[i];
}
}
for(int k=1;k<20;k++){
for(int i=0;i<=N;i++){
jmp_high[i][k]=jmp_high[jmp_high[i][k-1]][k-1];
jmp_low[i][k]=jmp_low[jmp_low[i][k-1]][k-1];
}
}
}
int minimum_jumps(int A, int B, int C, int D) {
int ans=0;
for(int k=19;k>=0;k--){
if(h[jmp_high[A][k]]<=h[C]){
A=jmp_high[A][k];
ans+=(1<<k);
}
}
for(int k=19;k>=0;k--){
if(h[jmp_low[A][k]]<=h[C]){
A=jmp_low[A][k];
ans+=(1<<k);
}
}
if(A==C)return ans;
return -1;
}
/*signed main(){
ios_base::sync_with_stdio();
cin.tie(0);cout.tie(0);
int n,q;
cin>>n>>q;
vector <int> a(n);
for(int i=0;i<n;i++)cin>>a[i];
init(n,a);
for(int i=0;i<q;i++){
int a,b,c,d;
cin>>a>>b>>c>>d;
cout<<minimum_jumps(a,b,c,d)<<"\n";
}
}*/
/*
7 10
3 2 1 6 4 5 7
*/
# | 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... |