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>
#define ll int
#define str string
#define ins insert
#define ld long double
#define pb push_back
#define pf push_front
#define pof pop_front()
#define pob pop_back()
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
//#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz size()
#define vll vector<ll>
#define arr array
#define pll vector<pair<ll,ll>>
using namespace std;
template<class S,class T>
bool chmin(S &a,const T &b) {
return a>b?(a=b)==b:false;
}
template<class S,class T>
bool chmax(S &a,const T &b) {
return a<b?(a=b)==b:false;
}
const ll inf=1e9;
const ll mod=1e9+7;
const ll N=2e5+5;
const ld eps=1e-9;
vll h;
ll n;
struct seg{
vll t;
void create(){
t.assign(n*4,0);
}
void upd(ll pos,ll val,ll v=1,ll tl=1,ll tr=n){
if(tl>pos || tr<pos) return;
if(tl==tr){
t[v]=val;
return;
}ll tm=(tl+tr)/2;
upd(pos,val,v*2,tl,tm);
upd(pos,val,v*2+1,tm+1,tr);
t[v]=max(t[v*2],t[v*2+1]);
}
ll get(ll l,ll r,ll v=1,ll tl=1,ll tr=n){
if(tl>r || tr<l) return 0ll;
if(l<=tl && tr<=r) return t[v];
ll tm=(tl+tr)/2;
return max(get(l,r,v*2,tl,tm),get(l,r,v*2+1,tm+1,tr));
}
ll fl(ll pos,ll v=1,ll tl=1,ll tr=n){
if(t[v]<=h[pos] || tl>=pos) return 0;
if(tl==tr) return tl;
ll tm=(tl+tr)/2;
ll x=fl(pos,v*2+1,tm+1,tr);
if(x)return x;
return fl(pos,v*2,tl,tm);
}
ll fr(ll pos,ll v=1,ll tl=1,ll tr=n){
if(t[v]<=h[pos] || tr<=pos) return 0;
if(tl==tr) return tl;
ll tm=(tl+tr)/2;
ll x=fr(pos,v*2,tl,tm);
if(x) return x;
else return fr(pos,v*2+1,tm+1,tr);
}
} t;
ll up[N][20][2];
ll dp[N][20];
void init(int N,vll H) {
ll i,j;
n=N;
h.pb(inf);
for(auto i : H)h.pb(i);
t.create();
for(i=1;i<=n;i++)t.upd(i,h[i]);
for(i=1;i<=n;i++){
ll x=t.fl(i);
if(x)up[i][0][0]=x;
x=t.fr(i);
if(x)up[i][0][1]=x;
}
for(i=1;i<=n;i++){
for(j=1;j<20;j++){
up[i][j][0]=up[up[i][j-1][0]][j-1][0];
}
}
for(i=n;i>0;i--){
for(j=1;j<20;j++){
up[i][j][1]=up[up[i][j-1][1]][j-1][0];
}
}
pll v;
for(i=1;i<=n;i++){
if(h[up[i][0][0]]>=h[up[i][0][1]])dp[i][0]=up[i][0][0];
else dp[i][0]=up[i][0][1];
v.pb({h[i],i});
}
sort(rall(v));
for(i=0;i<n;i++){
for(j=1;j<20;j++){
dp[i][j]=dp[dp[i][j-1]][j-1];
}
}
}
int minimum_jumps(int a, int b, int c, int d) {
a++,b++,c++,d++;
ll x=t.get(a,b),y=(b+1==c ? 0 : t.get(b+1,c-1)),z=t.get(c,d);
if(y>z || h[b]>z)return -1;
ll p=b,i,j;
for(j=19;j>=0;j--){
ll l=up[p][j][0];
if(l<a || h[l]>z) continue;
p=l;
}
ll ans=0;
for(i=19;i>=0;i--){
if(h[dp[p][i]]<=y){
ans+=(1ll<<i);
p=dp[p][i];
}
}
ll l=up[p][0][0],xx=inf;
if(h[l]<=z && l)xx=ans+2;
for(i=19;i>=0;i--){
if(h[up[p][i][1]]<=y){
ans+=(1ll<<i);
p=up[p][i][1];
}
}
return min(xx,ans+1);
}
Compilation message (stderr)
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:116:8: warning: unused variable 'x' [-Wunused-variable]
116 | ll x=t.get(a,b),y=(b+1==c ? 0 : t.get(b+1,c-1)),z=t.get(c,d);
| ^
# | 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... |