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"
#include<bits/stdc++.h>
#include <vector>
using namespace std;
int n;
int l[200005];
int r[200005];
int in[200005];
int out[200005];
int tme=0;
int h[200005];
int jl[200005];
int jr[200005];
int sp[200005];
int pa[20][200005];
int dis[20][200005];
void dfs(int u,int pl=0,int pr=0,int p=0){
in[u]=++tme;
int x=h[pl]<h[pr]?pl:pr;
//cerr<<"BINJUMP:"<<u<<" "<<x<<"\n";
pa[0][u]=x;
dis[0][u]=1;
for(int i=1;i<20;i++)pa[i][u]=pa[i-1][pa[i-1][u]];
for(int i=1;i<20;i++)dis[i][u]=dis[i-1][pa[i-1][u]]+dis[i-1][u];
//cerr<<u<<":"<<l[u]<<" "<<r[u]<<"\n";
h[u]=h[p]+1;
if(l[u])dfs(l[u],pl,u,u);
if(r[u])dfs(r[u],u,pr,u);
out[u]=tme;
}
void init(int N, std::vector<int> H) {
n=N;
stack<int>s;
int rt=0;
for(int i=1;i<=N;i++){
while(!s.empty()&&H[i-1]>H[s.top()-1])l[i]=s.top(),s.pop();
if(s.empty())rt=i;
else r[s.top()]=i;
s.push(i);
}
dfs(rt);
for(int i=1;i<=N;i++)assert(in[i]),assert(out[i]);
//cerr<<"\n\n";
}
int minimum_jumps(int A, int B, int C, int D) {
A++,B++,C++,D++;
//cerr<<"IN ANS:"<<A<<' '<<C<<"\n";
//cerr<<in[A]<<" "<<out[A]<<", "<<in[C]<<" "<<out[C]<<"\n";
int ans=0;
if(in[A]>=in[C]&&in[A]<=out[C]){
for(int i=5;i>=0;i--){
//cerr<<i<<"possible:"<<A<<" "<<pa[A][i]<<"\n";
if(h[pa[i][A]]>=h[C])A=pa[i][A],ans+=dis[i][A];
}
ans+=h[A]-h[C];
return ans;
}
return -1;
}
# | 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... |