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 F(a,b) for(int a=0;a<b;a++)
#define M 1<<18
vector<int>adj[M];
int vis[M],d[M],n,h[M],e[M][20],f[M][20],st[M][20],loc[M];
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> S;
F(i,N) loc[st[i+1][0]=h[i+1]=H[i]]=i+1;
h[0]=1e9,n=N;
F(j,19)for(int i=1;i+(1<<j+1)-2<n;i++)
st[i][j+1]=max(st[i][j],st[i+(1<<j)][j]);
F(i,N){
while(S.size()&&H[S.top()]<=H[i]) S.pop();
if(S.size()) adj[i+1].push_back(S.top()+1);
S.push(i);
}
while(S.size())S.pop();
for(int i=N;i--;){
while(S.size()&&H[S.top()]<=H[i]) S.pop();
if(S.size()) adj[i+1].push_back(e[i+1][0]=S.top()+1);
S.push(i);
}
for(int i=1;i<=N;i++)if(adj[i].size()>1)
f[i][0]=adj[i][h[adj[i][0]]<=h[adj[i][1]]];
else if(adj[i].size())
f[i][0]=adj[i][0];
F(j,19)for(int i=1;i<=N;i++)
e[i][j+1]=e[e[i][j]][j],f[i][j+1]=f[f[i][j]][j];
}
int calc(int a,int b,int c,int d){
int ans=0;
for(int i=20;i--;) if(h[f[a][i]]<=c)
a=f[a][i],ans+=1<<i;
if(h[f[a][0]]<=d)
return ans+1+(f[a][0]<b&&e[a][0]<b);
for(int i=20;i--;)
if(e[a][i]<b&&e[a][i])
a=e[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:17:31: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
17 | F(j,19)for(int i=1;i+(1<<j+1)-2<n;i++)
| ~^~
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:54:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
54 | 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... |