#include<bits/stdc++.h>
#include "walk.h"
#define F first
#define S second
#define PB push_back
#define sz(s) (int(s.size()))
#define bit(n, k) (((n)>>(k))&1)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
const int maxn = 13e6 + 10, inf = 2e9;
const ll INF = 2e18;
ll dis[maxn];
bool mark[maxn];
vector<pii> v[maxn];
ll digi(int a, int b){
fill(dis, dis + maxn, INF);
priority_queue<pli, vector<pli>, greater<pli> > pq;
dis[a] = 0;
pq.push({0, a});
while(sz(pq)){
int u = pq.top().S;
pq.pop();
if(mark[u])
continue;
mark[u] = 1;
for(auto [y, c] : v[u]){
if(dis[u] + c < dis[y])
dis[y] = dis[u] + c, pq.push({dis[y], y});
}
}
return dis[b];
}
map<pii, int> mp;
int C = 0;
int up[maxn];
void add_edge(int a, int b, int h){
assert(mp.count({a, h}));
assert(mp.count({b, h}));
int A = mp[{a, h}], B = mp[{b, h}];
v[A].PB({B, abs(b-a)});
v[B].PB({A, abs(b-a)});
}
ll min_distance(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int src, int snk){
vector<int> hs = h;
for(int x : y)
hs.PB(x);
sort(hs.begin(), hs.end());
hs.resize( unique(hs.begin(), hs.end()) - hs.begin() );
int n = sz(x), m = sz(y);
vector<int> idsx(n), idsy(m);
iota(idsx.begin(), idsx.end(), 0);
iota(idsy.begin(), idsy.end(), 0);
sort(idsx.begin(), idsx.end(), [&](int i, int j){ return h[i] > h[j]; } );
sort(idsy.begin(), idsy.end(), [&](int i, int j){ return y[i] > y[j]; } );
for(int i = 0; i < n; i++){
up[i] = inf;
}
auto build = [&](int id, int h){
if(mp.count({x[id], h}) == 0){
mp[{x[id], h}] = C++;
if(up[id] != inf){
int A = C-1, B = mp[{x[id], up[id]}];
v[A].PB({B, abs(h - up[id])});
v[B].PB({A, abs(h - up[id])});
}
up[id] = h;
}
};
int ptx = 0, pty = 0;
set<int> ids;
while(ptx < n || pty < m){
if(ptx == n || (pty != m && h[idsx[ptx]] < y[idsy[pty]])){
auto itl = ids.lower_bound(l[ idsy[pty] ]);
auto itr = ids.upper_bound(r[ idsy[pty] ]);
int LSTX = -1;
for(auto it = itl; it != itr; it++){
build(*it, y[idsy[pty]]);
if(LSTX != -1)
add_edge(LSTX, x[*it], y[idsy[pty]]);
LSTX = x[*it];
}
pty++;
}
else{
ids.insert(idsx[ptx]);
ptx++;
}
}
for(int i = 0; i < n; i++){
build(i, 0);
}
ll num = digi(mp[{x[src], 0}], mp[{x[snk], 0}]);
if(num == INF)
num = -1;
return num;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
263 ms |
407416 KB |
Output is correct |
2 |
Correct |
263 ms |
407544 KB |
Output is correct |
3 |
Correct |
267 ms |
407416 KB |
Output is correct |
4 |
Correct |
262 ms |
407416 KB |
Output is correct |
5 |
Correct |
264 ms |
407544 KB |
Output is correct |
6 |
Correct |
263 ms |
407416 KB |
Output is correct |
7 |
Correct |
266 ms |
407416 KB |
Output is correct |
8 |
Correct |
270 ms |
407400 KB |
Output is correct |
9 |
Correct |
268 ms |
407516 KB |
Output is correct |
10 |
Correct |
271 ms |
407544 KB |
Output is correct |
11 |
Correct |
263 ms |
407420 KB |
Output is correct |
12 |
Correct |
264 ms |
407544 KB |
Output is correct |
13 |
Correct |
268 ms |
407416 KB |
Output is correct |
14 |
Correct |
274 ms |
407416 KB |
Output is correct |
15 |
Correct |
266 ms |
407416 KB |
Output is correct |
16 |
Correct |
271 ms |
407416 KB |
Output is correct |
17 |
Correct |
266 ms |
407544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
268 ms |
407416 KB |
Output is correct |
2 |
Correct |
262 ms |
407420 KB |
Output is correct |
3 |
Correct |
2166 ms |
490856 KB |
Output is correct |
4 |
Correct |
1989 ms |
505568 KB |
Output is correct |
5 |
Correct |
1460 ms |
492972 KB |
Output is correct |
6 |
Correct |
1327 ms |
484512 KB |
Output is correct |
7 |
Correct |
1453 ms |
493168 KB |
Output is correct |
8 |
Correct |
2785 ms |
513244 KB |
Output is correct |
9 |
Correct |
1865 ms |
491224 KB |
Output is correct |
10 |
Correct |
2908 ms |
539624 KB |
Output is correct |
11 |
Correct |
1402 ms |
456988 KB |
Output is correct |
12 |
Correct |
1020 ms |
449780 KB |
Output is correct |
13 |
Correct |
2280 ms |
524532 KB |
Output is correct |
14 |
Correct |
784 ms |
448112 KB |
Output is correct |
15 |
Correct |
936 ms |
449956 KB |
Output is correct |
16 |
Correct |
787 ms |
449884 KB |
Output is correct |
17 |
Correct |
835 ms |
448368 KB |
Output is correct |
18 |
Correct |
630 ms |
450668 KB |
Output is correct |
19 |
Correct |
282 ms |
409596 KB |
Output is correct |
20 |
Correct |
498 ms |
428732 KB |
Output is correct |
21 |
Correct |
823 ms |
445960 KB |
Output is correct |
22 |
Correct |
875 ms |
449768 KB |
Output is correct |
23 |
Correct |
798 ms |
458208 KB |
Output is correct |
24 |
Correct |
861 ms |
449468 KB |
Output is correct |
25 |
Correct |
990 ms |
447908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
431 ms |
417940 KB |
Output is correct |
2 |
Execution timed out |
4118 ms |
533188 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
431 ms |
417940 KB |
Output is correct |
2 |
Execution timed out |
4118 ms |
533188 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
263 ms |
407416 KB |
Output is correct |
2 |
Correct |
263 ms |
407544 KB |
Output is correct |
3 |
Correct |
267 ms |
407416 KB |
Output is correct |
4 |
Correct |
262 ms |
407416 KB |
Output is correct |
5 |
Correct |
264 ms |
407544 KB |
Output is correct |
6 |
Correct |
263 ms |
407416 KB |
Output is correct |
7 |
Correct |
266 ms |
407416 KB |
Output is correct |
8 |
Correct |
270 ms |
407400 KB |
Output is correct |
9 |
Correct |
268 ms |
407516 KB |
Output is correct |
10 |
Correct |
271 ms |
407544 KB |
Output is correct |
11 |
Correct |
263 ms |
407420 KB |
Output is correct |
12 |
Correct |
264 ms |
407544 KB |
Output is correct |
13 |
Correct |
268 ms |
407416 KB |
Output is correct |
14 |
Correct |
274 ms |
407416 KB |
Output is correct |
15 |
Correct |
266 ms |
407416 KB |
Output is correct |
16 |
Correct |
271 ms |
407416 KB |
Output is correct |
17 |
Correct |
266 ms |
407544 KB |
Output is correct |
18 |
Correct |
268 ms |
407416 KB |
Output is correct |
19 |
Correct |
262 ms |
407420 KB |
Output is correct |
20 |
Correct |
2166 ms |
490856 KB |
Output is correct |
21 |
Correct |
1989 ms |
505568 KB |
Output is correct |
22 |
Correct |
1460 ms |
492972 KB |
Output is correct |
23 |
Correct |
1327 ms |
484512 KB |
Output is correct |
24 |
Correct |
1453 ms |
493168 KB |
Output is correct |
25 |
Correct |
2785 ms |
513244 KB |
Output is correct |
26 |
Correct |
1865 ms |
491224 KB |
Output is correct |
27 |
Correct |
2908 ms |
539624 KB |
Output is correct |
28 |
Correct |
1402 ms |
456988 KB |
Output is correct |
29 |
Correct |
1020 ms |
449780 KB |
Output is correct |
30 |
Correct |
2280 ms |
524532 KB |
Output is correct |
31 |
Correct |
784 ms |
448112 KB |
Output is correct |
32 |
Correct |
936 ms |
449956 KB |
Output is correct |
33 |
Correct |
787 ms |
449884 KB |
Output is correct |
34 |
Correct |
835 ms |
448368 KB |
Output is correct |
35 |
Correct |
630 ms |
450668 KB |
Output is correct |
36 |
Correct |
282 ms |
409596 KB |
Output is correct |
37 |
Correct |
498 ms |
428732 KB |
Output is correct |
38 |
Correct |
823 ms |
445960 KB |
Output is correct |
39 |
Correct |
875 ms |
449768 KB |
Output is correct |
40 |
Correct |
798 ms |
458208 KB |
Output is correct |
41 |
Correct |
861 ms |
449468 KB |
Output is correct |
42 |
Correct |
990 ms |
447908 KB |
Output is correct |
43 |
Correct |
431 ms |
417940 KB |
Output is correct |
44 |
Execution timed out |
4118 ms |
533188 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |