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>
using namespace std;
#define int long long
int L[200001];
int R[200001];
int arr[200001];
int rarr[200001];
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())R[arr[i]]=q.top();
else R[arr[i]]=-1;
q.emplace(arr[i]);
}
for(int i=0;i<N;i++)if(L[i]<R[i])swap(L[i],R[i]);
}
int32_t minimum_jumps(int32_t A, int32_t B, int32_t C, int32_t D) {
function<int (int,int)> dfs = [&](int x,int dist){
if(C<=rarr[x] and rarr[x]<=D)return dist;
if(L[x]==-1)return (int)INT32_MAX;
if(R[x]==-1)return dfs(L[x],dist+1);
if(C<=rarr[R[x]] and rarr[R[x]]<=D)return dist+1;
return dfs(L[x],dist+1);
};
int ans = INT32_MAX;
for(int i=A;i<=B;i++)ans = min(ans, dfs(arr[i], 0));
return ans>=INT32_MAX ? -1 : ans;
}
//int32_t main() {
// int32_t N, Q;
// assert(2 == scanf("%d %d", &N, &Q));
// std::vector<int32_t> H(N);
// for (int32_t i = 0; i < N; ++i) {
// assert(1 == scanf("%d", &H[i]));
// }
// init(N, H);
//
// for (int32_t i = 0; i < Q; ++i) {
// int32_t A, B, C, D;
// assert(4 == scanf("%d %d %d %d", &A, &B, &C, &D));
// printf("%d\n", minimum_jumps(A, B, C, D));
// }
// return 0;
//}
# | 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... |