This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//1L1YA
#include "jumps.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define Pb push_back
#define dokme(x) cout << x << endl, exit(0)
#define pii pair<int,int>
#define F first
#define S second
#define endl '\n'
#define sep ' '
#define all(x) x.begin(),x.end()
#define FastIO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define lc id<<1
#define rc lc|1
const int N=2e5+5;
const int lg=23;
int n,a[N],lst[N],nxt[N],seg[N<<2],sp1[lg][N],sp2[lg][N];
void modify(int pos,int val,int id=1,int l=1,int r=n+1){
if(r-l==1){
seg[id]=val;
return;
}
int mid=l+r>>1;
if(pos<mid) modify(pos,val,lc,l,mid);
else modify(pos,val,rc,mid,r);
seg[id]=max(seg[lc],seg[rc]);
}
int get(int ql,int qr,int id=1,int l=1,int r=n+1){
if(qr<=l || ql>=r) return 0;
if(ql<=l && r<=qr) return seg[id];
int mid=l+r>>1;
return max(get(ql,qr,lc,l,mid),get(ql,qr,rc,mid,r));
}
void init(int m, std::vector<int> h) {
n=m;
for(int i=1;i<=n;i++)
a[i]=h[i-1];
a[0]=a[n+1]=n+1;
vector<int> st;
st.Pb(0);
for(int i=1;i<=n;i++){
while(a[st.back()]<a[i])
st.pop_back();
lst[i]=st.back();
st.Pb(i);
}
st.clear();
st.Pb(n+1);
for(int i=n;i;i--){
while(a[st.back()]<a[i])
st.pop_back();
nxt[i]=st.back();
st.Pb(i);
}
for(int i=1;i<=n;i++){
sp1[0][a[i]]=min(a[lst[i]],a[nxt[i]]);
sp2[0][a[i]]=max(a[lst[i]],a[nxt[i]]);
}
sp1[0][n+1]=sp2[0][n+1]=n+1;
for(int i=n+1;i;i--)
for(int j=1;j<lg;j++){
sp1[j][i]=sp1[j-1][sp1[j-1][i]];
sp2[j][i]=sp2[j-1][sp2[j-1][i]];
}
for(int i=1;i<=n;i++)
modify(i,a[i]);
}
int dist(int s,int e){
if(s==e)
return 0;
int ans=0;
for(int i=lg-1;~i;i--)
if(sp2[i][s]<e)
s=sp2[i][s],ans+=1<<i;
if(sp2[0][s]==e)
return ans+1;
for(int i=lg-1;~i;i--)
if(sp1[i][s]<e)
s=sp1[i][s],ans+=1<<i;
return ans+1;
}
int minimum_jumps(int l1, int r1, int l2, int r2){
l1++;r1++;l2++;r2++;
int mx=get(l2,r2+1);
if(get(r1,l2)>mx)
return -1;
int l=0,r=r1;
while(r-l>1){
int mid=l+r>>1;
if(get(mid,r1+1)<mx) r=mid;
else l=mid;
}
int k=r,x=get(max(l1,k),r1+1);
mx=get(max(l1,k),l2);
int ans=dist(x,mx)+1;
if(k<=l1){
l=1,r=l1;
while(r-l>1){
int mid=l+r>>1;
if(get(mid,l1)<mx) r=mid;
else l=mid;
}
if(l>k)
ans=min(ans,dist(x,a[l])+1);
}
return ans;
}
/*int main() {
int N, Q;
assert(2 == scanf("%d %d", &N, &Q));
std::vector<int> H(N);
for (int i = 0; i < N; ++i) {
assert(1 == scanf("%d", &H[i]));
}
init(N, H);
for (int i = 0; i < Q; ++i) {
int 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;
}*/
Compilation message (stderr)
jumps.cpp: In function 'void modify(int, int, int, int, int)':
jumps.cpp:28:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
28 | int mid=l+r>>1;
| ~^~
jumps.cpp: In function 'int get(int, int, int, int, int)':
jumps.cpp:37:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
37 | int mid=l+r>>1;
| ~^~
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:98:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
98 | int mid=l+r>>1;
| ~^~
jumps.cpp:108:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
108 | int mid=l+r>>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... |