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>
using namespace std;
#define mp make_pair
#define INF 10000000
#define MOD 1000000007
#define MID ((l+r)/2)
#define HASHMOD 2305843009213693951
#define ll long long
#define ull unsigned long long
#define F first
#define S second
typedef pair<ll, ll> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef map<int, int> mii;
#define EPS 1e-6
#define FOR(i,n) for(int i=0;i<((int)(n));i++)
#define FORi(i,a,b) for(int i=((int)(a));i<((int)(b));i++)
#define FOA(v, a) for(auto v : a)
int t, n;
vi a, b;
vi nl, pl;
struct nodemax{
nodemax* L=NULL, *R=NULL;
int val=-1;
void update(int x, int v, int l=0, int r=n-1){
if(l==r && l==x){
val = v;
//cout<<l<<" "<<r<<" "<<val<<endl;
}
else if(l==r || l>x || r<x) return;
else{
if(!L) L = new nodemax();
if(!R) R = new nodemax();
val = -1;
L->update(x, v, l, MID), val = max(val, L->val);
R->update(x,v, MID+1,r), val = max(val, R->val);
//cout<<l<<" "<<r<<" "<<val<<endl;
}
}
int query(int rl, int rr, int l=0, int r=n-1){
if(rl<=l && r<=rr) return val;
else if(rl>r || l>rr) return -1;
else {
if(!L) L = new nodemax();
if(!R) R = new nodemax();
return max(L->query(rl, rr, l, MID), R->query(rl,rr,MID+1,r));
}
}
};
struct node{
node* L=NULL, *R=NULL;
int val=n;
void update(int x, int v, int l=0, int r=n-1){
if(l==r && l==x){
val = v;
//cout<<l<<" "<<r<<" "<<val<<endl;
}
else if(l==r || l>x || r<x) return;
else{
if(!L) L = new node();
if(!R) R = new node();
val = n;
L->update(x, v, l, MID), val = min(val, L->val);
R->update(x,v, MID+1,r), val = min(val, R->val);
//cout<<l<<" "<<r<<" "<<val<<endl;
}
}
int query(int rl, int rr, int l=0, int r=n-1){
if(rl<=l && r<=rr) return val;
else if(rl>r || l>rr) return n;
else {
if(!L) L = new node();
if(!R) R = new node();
return min(L->query(rl, rr, l, MID), R->query(rl,rr,MID+1,r));
}
}
};
node seg;
nodemax segm;
int hi[101000][20];
int lo[101000][20];
void init(int N, std::vector<int> H) {
n = N;
a.resize(n);
nl.resize(n+1);
pl.resize(n);
FOR(i,n) a[i] = H[i];
FORi(i,1,n+1){
//cout<<endl;
nl[n-i] = seg.query(a[n-i], n-1);
seg.update(a[n-i]-1, n-i);
}
nl[n] = n;
FOR(i,n){
//cout<<endl;
pl[i] = segm.query(a[i], n-1);
segm.update(a[i]-1, i);
}
FOR(i,n){
if(nl[i]!=n && (pl[i]==-1 || a[nl[i]]>a[pl[i]])) hi[i][0] = nl[i];
else hi[i][0] = pl[i];
if(nl[i]!=n && (pl[i]==-1 || a[nl[i]]<a[pl[i]])) lo[i][0] = nl[i];
else lo[i][0] = pl[i];
if(hi[i][0] == -1) hi[i][0] = n;
if(lo[i][0] == -1) lo[i][0] = n;
}
lo[n][0] = n;
hi[n][0] = n;
//FOR(i,n) cout<<hi[i][0]<<" "; cout<<" | "; FOR(i,n) cout<<lo[i][0]<<" "; cout<<endl;
FORi(j,1,20){
FOR(i,n+1){
hi[i][j] = hi[hi[i][j-1]][j-1];
lo[i][j] = lo[lo[i][j-1]][j-1];
//cout<<hi[i][j]<<" ";
}
//cout<<endl;
}
//cout<<endl;
}
int minimum_jumps(int A, int B, int C, int D) {
int s = A;
int ans=0;
for(int i=18;i>=0;i--){
if(hi[s][i]!=-1 && nl[hi[s][i]]<=D) s = hi[s][i], ans+=(1<<i);
if(s>=C) break;
}
for(int i=18;i>=0;i--){
if(lo[s][i]<=D) s = lo[s][i], ans+=(1<<i);
if(s>=C) break;
}
//cout<<s<<" "<<ans<<endl;
if(C<=s && s<=D) return ans;
else 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... |