이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
h.pb(1e9);n=N;
for(int i=0;i<N;i++)h.pb(H[i]);
vector <int> l(N+1),r(N+1);
for(int i=0;i<N;i++){
while(!st.empty() && H[st.top()]<H[i]){
r[st.top()+1]=i+1;
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()+1]=i+1;
st.pop();
}
st.push(i);
}
/*for(int i=1;i<=N;i++){
cout<<l[i]<<" ";
}
cout<<"\n";
for(int i=1;i<=N;i++){
cout<<r[i]<<" ";
}
cout<<"\n";*/
for(int i=1;i<=N;i++){
if(H[l[i]]>H[r[i]])swap(l[i],r[i]);
if(l[i] && r[i]){
jmp_high[i][0]=r[i];
jmp_low[i][0]=l[i];
}
else if(l[i])jmp_high[i][0]=l[i];
else if(r[i])jmp_high[i][0]=r[i];
}
for(int k=1;k<20;k++){
for(int i=1;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) {
A++;B++;C++;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... |