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 <vector>
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#define pb push_back
#define F first
#define S second
#define ld long double
#define all(a) a.begin(),a.end()
#define pii pair <int,int>
#define PII pair<pii , pii>
#define sz(v) (int)v.size()
#define rep(i , a , b) for(int i=a;i <= (b);i++)
#define per(i , a , b) for(int i=a;i >= (b);i--)
#define deb(x) cout <<#x << " : " << x << "\n" ;
using namespace std ;
const int maxn = 2e5 + 10 , maxq = 32, inf = 1e9+10 , lg = 19 ,sq = 707 ,mod = 998244353 ;
int mx[maxn][lg+1] , c = 1 , L[maxn] , R[maxn], h[maxn] , dis[maxn] , par[maxn][lg+1] ;
vector< int> G[maxn] ;
int ch(int l , int r){
int x = 31 - __builtin_clz(r-l+1) ;
return (h[mx[l][x]] > h[mx[r-(1<<x)+1][x]] ? mx[l][x] : mx[r-(1<<x)+1][x]) ;
}
void dfs(int v){
for(int u : G[v]){
dis[u] = dis[v] + 1;
dfs(u) ;
}
}
void init(int n, vector<int> H) {
rep(i ,1 ,n){
h[i] = H[i-1] ;
mx[i][0] = i ;
}
vector <int> vec;;
rep(i , 1, n){
while(sz(vec) && h[vec.back()] < h[i])vec.pop_back() ;
L[i] = -1 ;
if(sz(vec)!=0)L[i] = vec.back() ;
vec.pb(i) ;
}
vec.clear() ;
per(i ,n,1){
while(sz(vec) && h[vec.back()] < h[i])vec.pop_back() ;
R[i] = -1 ;
if(sz(vec)!=0)R[i] = vec.back() ;
vec.pb(i) ;
}
int id =0 ;
rep(i ,1 ,n){
if(L[i]+R[i] == -2){
id = i ;
continue ;
}
if(L[i]!=-1 && (R[i] == -1 || h[L[i]] < h[R[i]])){
G[L[i]].pb(i) ;
if(R[i] == -1)par[i][0] = L[i] ;
else par[i][0] = R[i] ;
}else{
G[R[i]].pb(i) ;
if(L[i] == -1)par[i][0] = R[i] ;
else par[i][0] = L[i] ;
}
}
dfs(id) ;
rep(i ,1 ,n){
rep(j , 1 ,lg){
if(i+(1<<(j-1))>n){
mx[i][j] = mx[i][j-1] ;
}else{
mx[i][j] = (h[mx[i][j-1]] > h[mx[i+(1<<(j-1))][j-1]] ? mx[i][j-1] : mx[i+(1<<(j-1))][j-1]);
}
par[i][j] = par[par[i][j-1]][j-1];
}
}
}
int answer(int a, int b){
if(L[b] >= a)return inf ;
int ans =0 ;
per(i , lg , 0){
if(par[a][i] != 0 && dis[par[a][i]] <= dis[b]){
a = par[a][i] ;
ans += (1<<i);
}
}
return ans + dis[a]-dis[b] ;
}
int minimum_jumps(int A, int B, int C, int D) {
A++;B++;C++;D++;
int ans = inf ;
rep(i , A ,B){
rep(j, C , D){
ans = min(ans , answer(i , j)) ;
}
}
return (ans == inf ? -1 : ans) ;
}
# | 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... |