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"
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define N 200005
int t[4*N],arr[N],rev[N],up[20][N],dow[20][N],ma[20][N],nxt[20][N],n;
void cre(int x,int l,int r){
if(l==r){t[x]=arr[l];return ;}
int mid=(l+r)/2;
cre(x*2,l,mid);
cre(x*2+1,mid+1,r);
t[x]=max(t[x*2],t[x*2+1]);
}
int sol(int x,int l,int r,int ll,int rr){
if(l>rr||ll>r||ll>rr)return 0;
if(ll<=l&&r<=rr)return t[x];
int mid=(l+r)/2;
return max(sol(x*2,l,mid,ll,rr),sol(x*2+1,mid+1,r,ll,rr));
}
void init(int nn,vector<int> h){
int i,j,l,r,mid,val;
n=nn;
for(i=0;i<n;i++)arr[i+1]=h[i],rev[h[i]]=i+1;
cre(1,1,n);
rev[n+1]=n+1;
for(i=0;i<20;i++)for(j=0;j<=n+1;j++)up[i][j]=dow[i][j]=nxt[i][j]=n+1;
for(i=1;i<=n;i++){
l=1,r=i-1;
while(l<=r){
mid=(l+r)/2;
if(sol(1,1,n,mid,i-1)>arr[i])dow[0][arr[i]]=arr[mid],l=mid+1;
else r=mid-1;
}
l=i+1,r=n;
while(l<=r){
mid=(l+r)/2;
if(sol(1,1,n,i+1,mid)>arr[i])up[0][arr[i]]=arr[mid],r=mid-1;
else l=mid+1;
}
nxt[0][arr[i]]=up[0][arr[i]];
if(up[0][arr[i]]<dow[0][arr[i]])swap(up[0][arr[i]],dow[0][arr[i]]);
ma[0][arr[i]]=max(rev[up[0][arr[i]]],rev[dow[0][arr[i]]]);
}
for(i=1;i<20;i++)for(j=1;j<=n;j++){
up[i][j]=up[i-1][up[i-1][j]];
dow[i][j]=dow[i-1][dow[i-1][j]];
nxt[i][j]=nxt[i-1][nxt[i-1][j]];
ma[i][j]=max(ma[i-1][j],ma[i-1][up[i-1][j]]);
}
return ;
}
int minimum_jumps(int a,int b,int c,int d){
a++,b++,c++,d++;
int i,l,r,mid,ml,mm,mr,now,val,ans=0;
mm=sol(1,1,n,b+1,c-1);
mr=sol(1,1,n,c,d);
if(mm>mr)return -1;
l=c,r=d;
while(l<=r){
mid=(l+r)/2;
if(sol(1,1,n,c,mid)>mm)ml=arr[mid],r=mid-1;
else l=mid+1;
}
l=a,r=b;
now=n+1;
while(l<=r){
mid=(l+r)/2;
val=sol(1,1,n,mid,b);
if(val<mr)now=val,r=mid-1;
else l=mid+1;
}
if(now>mr)return -1;
for(i=19;i>=0;i--)if(up[i][now]<=mr&&ma[i][now]<c)ans+=1<<i,now=up[i][now];
for(i=19;i>=0;i--)if(rev[nxt[i][now]]<c)ans+=1<<i,now=nxt[i][now];
if(rev[nxt[0][now]]<=d)return ans+1;
return -1;
}
Compilation message (stderr)
jumps.cpp: In function 'void init(int, std::vector<int>)':
jumps.cpp:26:21: warning: unused variable 'val' [-Wunused-variable]
26 | int i,j,l,r,mid,val;
| ^~~
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:60:19: warning: variable 'ml' set but not used [-Wunused-but-set-variable]
60 | int i,l,r,mid,ml,mm,mr,now,val,ans=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... |