이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
int L[200001];
int arr[200001];
int rarr[200001];
int lift[200001][18];
int n;
void init(int32_t N, vector<int32_t> H) {
n=N;
for(int i=0;i<N;i++)arr[i]=H[i];
for(int i=0;i<N;i++)rarr[arr[i]]=i;
stack<int> q;
for(int i=0;i<N;i++){
while(!q.empty() and q.top()<arr[i])q.pop();
if(!q.empty())L[arr[i]]=q.top();
else L[arr[i]]=-1;
q.emplace(arr[i]);
}
while(!q.empty())q.pop();
for(int i=N-1;i>=0;i--){
while(!q.empty() and q.top()<arr[i])q.pop();
if(!q.empty())lift[arr[i]][0]=q.top();
else lift[arr[i]][0]=-1;
q.emplace(arr[i]);
}
lift[n][0]=n;
for(int i=1;i<=N;i++)if(L[i]>lift[i][0])swap(L[i],lift[i][0]);
for(int bit=1;bit<=17;bit++){
for(int i=1;i<=N;i++){
lift[i][bit] = lift[lift[i][bit-1]][bit-1];
}
}
}
int getans(int base,int tar){
if(base>tar or base==-1)return INT32_MAX;
int ans = 0;
for(int jump=17;jump>=0;jump--){
if(lift[base][jump]<tar) { base = lift[base][jump]; ans|=(1<<jump); }
}
if(L[base]==tar or lift[base][0]==tar)return ans+1;
return ans+1+getans(L[base],tar);
}
int32_t minimum_jumps(int32_t A, int32_t B, int32_t C, int32_t D) {
int base = arr[A];
int tar = arr[C];
return getans(base,tar)>=INT32_MAX ? -1 : getans(base,tar);
}
# | 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... |