#include <bits/stdc++.h>
#include "walk.h"
#define sz(x) (int)x.size()
#define mkt make_tuple
#define lp(i,a,b) for(int i = a ; i < b ; i++ )
#define ff first
#define ss second
#define pb push_back
#define ll long long
#define mk make_pair
#define pii pair<int,int>
#define debug printf
#define all(x) x.begin(),x.end()
#define tiii tuple<int,int,int>
const int MAXN = 1e5+100 ;
const int inf = 1e9+7 ;
const ll linf = 1e18+10 ;
using namespace std ;
struct Seg
{
vector<int> y ;
int id[MAXN*4] , mx[MAXN*4] ;
int m(int l, int r) { return (l+r)>>1 ; }
void upd(int pos, int l, int r, int beg, int en , int k )
{
if( l > en || r < beg ) return ;
mx[pos] = k ;
if(l >= beg && r <= en)
{
id[pos] = k ;
return ;
}
upd(pos<<1 , l, m(l,r) , beg, en, k ) ;
upd(pos<<1|1 , m(l,r) + 1 , r , beg, en ,k ) ;
}
int qry(int pos, int l, int r, int beg, int en)
{
if( l > en || r < beg ) return sz(y)-1 ;
if( l >= beg && r <= en ) return mx[pos] ;
int al = qry(pos<<1 , l, m(l,r) , beg, en ) ;
int ar = qry(pos<<1|1 , m(l,r)+1, r, beg, en ) ;
if( y[ id[pos] ] <= min( y[ al ] , y[ar] ) ) return id[pos] ;
return y[al] < y[ar] ? al : ar ;
}
} segCima, segBaixo ;
struct Event
{
int y , l , r , id ;
Event(int y = 0 ,int l = 0 , int r = 0 , int id = 0 ) : y(y) , l(l) , r(r) , id(id) {}
bool operator < ( Event other ) const { return y > other.y ; }
};
int N , M , S , G ;
vector<Event> sweep ;
vector<pii> adj[MAXN] ;
ll dist[MAXN] ;
void dijkstra()
{
lp(i,0,M) dist[i] = linf ;
dist[M-2] = 0 ;
priority_queue< pair<ll,int> , vector<pair<ll,int> > , greater< pair<ll,int> > > fila ;
fila.push(mk(0,M-2)) ;
while(!fila.empty())
{
int x = fila.top().ss ;
ll d = fila.top().ff ;
fila.pop() ;
if(d != dist[x]) continue ;
for(auto e : adj[x] )
{
ll dd = d + (ll)e.ss ;
if(dd >= dist[e.ff]) continue ;
fila.push( mk( (dist[e.ff] = dd) , e.ff ) ) ;
}
}
}
long long min_distance(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int s, int g)
{
if(s == g) return 0LL ;
N = sz(x) ;
l.pb(0) ;
r.pb(0) ;
y.pb(0) ;
l.pb( N-1 ) ;
r.pb(N-1) ;
y.pb(0) ;
M = sz(l) ;
S = s ;
G = g ;
lp(i,0,M)
{
sweep.pb( Event( y[i] , l[i] , r[i] , i) ) ;
segCima.y.pb( y[i] ) ;
segBaixo.y.pb( -y[i] ) ;
}
segCima.y.pb( inf ) ;
segBaixo.y.pb( inf ) ;
lp(i,0,MAXN*4)
{
segCima.id[i] = segCima.mx[i]= sz(segCima.y) - 1 ;
segBaixo.id[i] = segBaixo.mx[i]= sz(segBaixo.y) - 1 ;
}
sort(all(sweep)) ;
for(auto e : sweep )
{
// debug("Processing %d %d %d\n" , e.y , e.l , e.r ) ;
int mnY = segCima.qry( 1 , 1 , N , e.r + 1 , e.r+1 ) ;
segCima.upd(1,1,N, e.l + 1 ,e.r + 1 , e.id ) ;
//debug("E deu %d\n\n" , mnY ) ;
if( mnY == sz(y) ) continue ;
adj[ mnY ].pb( mk( e.id , y[mnY] - y[e.id] ) ) ;
adj[e.id].pb( mk( mnY , y[mnY] - y[e.id] ) ) ;
}
reverse(all(sweep)) ;
for(auto e : sweep )
{
int mxY = segBaixo.qry(1,1,N, e.r+1, e.r+1 ) ;
segBaixo.upd(1,1,N, e.l + 1 , e.r + 1 , e.id ) ;
if(mxY == sz(y)) continue ;
adj[ mxY ].pb( mk( e.id , y[e.id] - y[mxY] ) ) ;
adj[e.id].pb( mk( mxY , y[e.id] - y[mxY] ) ) ;
}
/*lp(i,0,M)
{
debug("Adjacency of %d (%d %d %d):\n", i , l[i] , r[i] , y[i] ) ;
for(auto e : adj[i]) debug("%d %d\n" , e.ff, e.ss ) ;
debug("\n") ;
} */
dijkstra() ;
if( dist[M-1] == linf ) return -1LL ;
return dist[M-1] + (ll)( x[N-1] - x[0] ) ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
8960 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
8960 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
14900 KB |
Output is correct |
2 |
Correct |
185 ms |
22488 KB |
Output is correct |
3 |
Correct |
228 ms |
22872 KB |
Output is correct |
4 |
Correct |
314 ms |
26328 KB |
Output is correct |
5 |
Correct |
311 ms |
26584 KB |
Output is correct |
6 |
Correct |
296 ms |
26392 KB |
Output is correct |
7 |
Correct |
150 ms |
19352 KB |
Output is correct |
8 |
Correct |
242 ms |
23256 KB |
Output is correct |
9 |
Correct |
282 ms |
26328 KB |
Output is correct |
10 |
Correct |
211 ms |
25304 KB |
Output is correct |
11 |
Correct |
19 ms |
10488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
14900 KB |
Output is correct |
2 |
Correct |
185 ms |
22488 KB |
Output is correct |
3 |
Correct |
228 ms |
22872 KB |
Output is correct |
4 |
Correct |
314 ms |
26328 KB |
Output is correct |
5 |
Correct |
311 ms |
26584 KB |
Output is correct |
6 |
Correct |
296 ms |
26392 KB |
Output is correct |
7 |
Correct |
150 ms |
19352 KB |
Output is correct |
8 |
Correct |
242 ms |
23256 KB |
Output is correct |
9 |
Correct |
282 ms |
26328 KB |
Output is correct |
10 |
Correct |
211 ms |
25304 KB |
Output is correct |
11 |
Correct |
19 ms |
10488 KB |
Output is correct |
12 |
Correct |
229 ms |
23000 KB |
Output is correct |
13 |
Correct |
303 ms |
26200 KB |
Output is correct |
14 |
Correct |
307 ms |
26456 KB |
Output is correct |
15 |
Correct |
244 ms |
26072 KB |
Output is correct |
16 |
Correct |
281 ms |
28120 KB |
Output is correct |
17 |
Correct |
270 ms |
27224 KB |
Output is correct |
18 |
Correct |
250 ms |
26072 KB |
Output is correct |
19 |
Correct |
308 ms |
28248 KB |
Output is correct |
20 |
Correct |
175 ms |
19364 KB |
Output is correct |
21 |
Correct |
42 ms |
12536 KB |
Output is correct |
22 |
Correct |
233 ms |
24188 KB |
Output is correct |
23 |
Correct |
244 ms |
24796 KB |
Output is correct |
24 |
Correct |
250 ms |
25156 KB |
Output is correct |
25 |
Correct |
250 ms |
24332 KB |
Output is correct |
26 |
Correct |
248 ms |
24596 KB |
Output is correct |
27 |
Correct |
317 ms |
26456 KB |
Output is correct |
28 |
Correct |
287 ms |
26200 KB |
Output is correct |
29 |
Correct |
304 ms |
26328 KB |
Output is correct |
30 |
Correct |
163 ms |
19352 KB |
Output is correct |
31 |
Correct |
278 ms |
26200 KB |
Output is correct |
32 |
Correct |
217 ms |
24664 KB |
Output is correct |
33 |
Correct |
227 ms |
24792 KB |
Output is correct |
34 |
Correct |
211 ms |
25908 KB |
Output is correct |
35 |
Correct |
243 ms |
25048 KB |
Output is correct |
36 |
Correct |
245 ms |
23912 KB |
Output is correct |
37 |
Correct |
218 ms |
22748 KB |
Output is correct |
38 |
Correct |
243 ms |
25048 KB |
Output is correct |
39 |
Correct |
204 ms |
26956 KB |
Output is correct |
40 |
Correct |
229 ms |
23896 KB |
Output is correct |
41 |
Correct |
231 ms |
23312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
8960 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |