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<bits/stdc++.h>
#include "jumps.h"
using namespace std ;
const int N = 2e5 ;
bool flag1, flag2, flag3 ;
int pw[N + 1], dist[2001][2001], mn[2001][2001][11], ds[N], mx[N][18] ;
vector<int> gr[N + 1] ;
void bfs(int city)
{
for(int i = 0 ; i < 2000 ; i++)
dist[city][i] = 1e9 ;
deque<int> d ;
d.push_back(city) ;
dist[city][city] = 0 ;
while(d.size())
{
int num = d[0] ;
d.pop_front() ;
for(int i : gr[num])
{
if(dist[city][i] != 1e9)continue ;
dist[city][i] = dist[city][num] + 1 ;
d.push_back(i) ;
}
}
}
int more_bfs(int a, int b, int c, int d)
{
int ans = 1e9 ;
deque<int> dq ;
for(int i = 0 ; i < N ; i++)
{
ds[i] = 1e9 ;
if(a <= i && i <= b)ds[i] = 0, dq.push_back(i) ;
}
while(dq.size())
{
int num = dq[0] ;
if(c <= num && num <= d)
ans = min(ans, ds[num]) ;
dq.pop_front() ;
for(int i : gr[num])
{
if(ds[i] != 1e9)continue ;
ds[i] = ds[num] + 1 ;
dq.push_back(i) ;
}
}
if(ans == 1e9)ans = -1 ;
return ans ;
}
void build_sparce_table(int city)
{
for(int i = 2 ; i <= 2000 ; i++)
pw[i] = pw[i / 2] + 1 ;
for(int i = 0 ; i < 2000 ; i++)
mn[city][i][0] = dist[city][i] ;
for(int i = 1 ; i < 11 ; i++)
for(int j = 0 ; j <= 2000 - (1 << i) ; j++)
mn[city][j][i] = min(mn[city][j][i - 1], mn[city][j + (1 << (i - 1))][i - 1]) ;
}
int get_min(int l, int r, int city)
{
int num = pw[r - l + 1] ;
return min(mn[city][l][num], mn[city][r - (1 << num) + 1][num]) ;
}
void build_sparce(vector<int> a)
{
for(int i = 2 ; i <= N ; i++)
pw[i] = pw[i / 2] + 1 ;
for(int i = 0 ; i < a.size() ; i++)
mx[i][0] = a[i] ;
for(int i = 1 ; i < 18 ; i++)
for(int j = 0 ; j <= (int)a.size() - (1 << i) ; j++)
mx[j][i] = max(mx[j][i - 1], mx[j + (1 << (i - 1))][i - 1]) ;
}
int get_max(int l, int r)
{
int num = pw[r - l + 1] ;
return max(mx[l][num], mx[r - (1 << num) + 1][num]) ;
}
void init(int n, vector<int> a)
{
for(int i = 0 ; i < n ; i++)
if(a[i] != i + 1)flag1 = 1 ;
if(!flag1)return ;
if(n > 2000)flag2 = 1 ;
else
{
for(int i = 0 ; i < n ; i++)
{
for(int j = i - 1 ; j >= 0 ; j--)
if(a[j] > a[i])
{
gr[i].push_back(j) ;
break ;
}
for(int j = i + 1 ; j < a.size() ; j++)
if(a[j] > a[i])
{
gr[i].push_back(j) ;
break ;
}
}
for(int i = 0 ; i < n ; i++)
{
bfs(i) ;
build_sparce_table(i) ;
}
return ;
}
build_sparce(a) ;
for(int i = 0 ; i < n ; i++)
{
int l1 = -1, r1 = i, l2 = i, r2 = n ;
while(l1 + 1 < r1)
{
int mid = (l1 + r1) >> 1 ;
if(get_max(mid, i) > a[i])l1 = mid ;
else r1 = mid ;
}
if(l1 != -1)gr[i].push_back(l1) ;
while(l2 + 1 < r2)
{
int mid = (l2 + r2) >> 1 ;
if(a[i] < get_max(i, mid))r2 = mid ;
else l2 = mid ;
}
if(r2 != n)gr[i].push_back(r2) ;
}
}
int minimum_jumps(int a, int b, int c, int d)
{
if(!flag1)
{
if(a <= c && c <= b || a <= d && d <= b || c <= a && a <= d || a <= d && d <= b)return 0 ;
if(c - b > 0)return c - b ;
else return -1 ;
}
if(!flag2)
{
int ans = 1e9 ;
for(int i = a ; i <= b ; i++)
{
ans = min(ans, get_min(c, d, i)) ;
}
if(ans == 1e9)ans = -1 ;
return ans ;
}
if(!flag3)
{
return more_bfs(a, b, c, d) ;
}
}
//signed main()
//{
// ios_base::sync_with_stdio( 0 ) ;
// cin.tie( 0 ) ;
// cout.tie( 0 ) ;
// int n, q ;
// cin >> n >> q ;
// vector<int> v(n) ;
// for(int i = 0 ; i < n ; i++)
// cin >> v[i] ;
// init(n, v) ;
// while(q--)
// {
// int a, b, c, d ;
// cin >> a >> b >> c >> d ;
// cout << minimum_jumps(a, b, c, d) << '\n' ;
// }
// return 0 ;
//}
Compilation message (stderr)
jumps.cpp: In function 'void build_sparce(std::vector<int>)':
jumps.cpp:71:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for(int i = 0 ; i < a.size() ; i++)
| ~~^~~~~~~~~~
jumps.cpp: In function 'void init(int, std::vector<int>)':
jumps.cpp:98:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for(int j = i + 1 ; j < a.size() ; j++)
| ~~^~~~~~~~~~
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:136:19: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
136 | if(a <= c && c <= b || a <= d && d <= b || c <= a && a <= d || a <= d && d <= b)return 0 ;
| ~~~~~~~^~~~~~~~~
jumps.cpp:136:59: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
136 | if(a <= c && c <= b || a <= d && d <= b || c <= a && a <= d || a <= d && d <= b)return 0 ;
| ~~~~~~~^~~~~~~~~
jumps.cpp:136:79: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
136 | if(a <= c && c <= b || a <= d && d <= b || c <= a && a <= d || a <= d && d <= b)return 0 ;
| ~~~~~~~^~~~~~~~~
jumps.cpp:154:1: warning: control reaches end of non-void function [-Wreturn-type]
154 | }
| ^
# | 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... |