This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//viewed solution
#include "jumps.h"
#include <bits/stdc++.h>
using namespace std;
vector<int>adj[200100];
int vis[200100],d[200100],n,h[200100],rbj[200100][20],obj[200100][20],st[200100][20],loc[200100];
vector<int> v;
int query(int a,int b){
int x=31-__builtin_clz(b-a+1);
return max(st[a][x],st[b-(1<<x)+1][x]);
}
void init(int N, std::vector<int> H) {
stack<int> stk;
for(int i=0;i<N;i++)
loc[st[i+1][0]=h[i+1]=H[i]]=i+1;
h[0]=1e9,n=N;
for(int j=1;j<20;j++) for(int i=1;i+(1<<j)-2<n;i++)
st[i][j]=max(st[i][j-1],st[i+(1<<j-1)][j-1]);
for(int i=0;i<N;i++){
while(stk.size()&&H[stk.top()]<=H[i])
stk.pop();
if(stk.size())
adj[i+1].push_back(stk.top()+1);
stk.push(i);
}
while(stk.size())stk.pop();
for(int i=N;i--;){
while(stk.size()&&H[stk.top()]<=H[i])
stk.pop();
if(stk.size())
adj[i+1].push_back(rbj[i+1][0]=stk.top()+1);
stk.push(i);
}
for(int i=1;i<=N;i++)if(adj[i].size()>1)
obj[i][0]=adj[i][h[adj[i][0]]<=h[adj[i][1]]];
else if(adj[i].size())
obj[i][0]=adj[i][0];
for(int j=1;j<20;j++)
for(int i=1;i<=N;i++)
rbj[i][j]=rbj[rbj[i][j-1]][j-1],
obj[i][j]=obj[obj[i][j-1]][j-1];
}
int calc(int a,int b,int c,int d){
int ans=0;
for(int i=20;i--;) if(h[obj[a][i]]<=c)
a=obj[a][i],ans+=1<<i;
if(h[obj[a][0]]<=d)
return ans+1+(obj[a][0]<b&&rbj[a][0]<b);
for(int i=20;i--;)
if(rbj[a][i]<b&&rbj[a][i])
a=rbj[a][i],ans+=1<<i;
return ans+1;
}
int minimum_jumps(int A, int B, int C, int D) {
A++,B++,C++,D++;
int m1=query(B,C-1),m2=query(C,D);
if(m1>m2)
return -1;
int l=A,r=B;
while(l<=r){
int mid=l+r>>1;
if(query(mid,B)<m2)
A=mid,r=mid-1;
else l=mid+1;
}
return calc(loc[query(A,B)],C,m1,m2);
}
Compilation message (stderr)
jumps.cpp: In function 'void init(int, std::vector<int>)':
jumps.cpp:18:43: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
18 | st[i][j]=max(st[i][j-1],st[i+(1<<j-1)][j-1]);
| ~^~
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:61:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
61 | 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... |