#include "walk.h"
#include <bits/stdc++.h>
using ll = long long;
using std::pair;
using std::vector;
template <class T>
using min_heap = std::priority_queue<T, vector<T>, std::greater<T>>;
ll min_distance(vector<int> X, vector<int> H, vector<int> L, vector<int> R, vector<int> Y, int src, int dst) {
const int N = (int)X.size();
for (const int pivot : {src, dst}) {
const int M = (int)Y.size();
vector<pair<int, int>> left, right;
for (int i = pivot; i >= 0; --i) {
if (left.empty() or left.back().first < H[i]) {
left.emplace_back(H[i], i);
}
}
for (int i = pivot; i < N; ++i) {
if (right.empty() or right.back().first < H[i]) {
right.emplace_back(H[i], i);
}
}
for (int i = 0; i < M; ++i) {
if (L[i] < pivot and pivot < R[i]) {
const int a = std::lower_bound(left.begin(), left.end(), pair(Y[i], 0))->second;
const int b = std::lower_bound(right.begin(), right.end(), pair(Y[i], 0))->second;
L.push_back(L[i]);
R.push_back(a);
Y.push_back(Y[i]);
L.push_back(b);
R.push_back(R[i]);
Y.push_back(Y[i]);
L[i] = a;
R[i] = b;
}
}
}
vector<vector<pair<int, int>>> graph(N);
const auto add_vertex = [&] {
graph.emplace_back();
return (int)graph.size() - 1;
};
const auto add_edge = [&](const int u, const int v, const int c) {
graph[u].emplace_back(v, c);
graph[v].emplace_back(u, c);
};
vector<vector<pair<int, int>>> vertices(N);
for (int i = 0; i < N; ++i) {
vertices[i].emplace_back(0, i);
}
const int M = (int)Y.size();
vector<int> order(M);
std::iota(order.begin(), order.end(), 0);
std::sort(order.begin(), order.end(), [&](const int i, const int j) {
return Y[i] < Y[j];
});
for (int step = 0; step < 2; ++step) {
std::set<int> remain;
for (int i = 0; i < N; ++i) {
remain.insert(i);
}
for (const int e : order) {
if (L[e] == R[e]) {
continue;
}
vector<int> list;
list.push_back(L[e]);
remain.insert(L[e]);
for (auto itr = remain.upper_bound(L[e]); itr != remain.end() and *itr < R[e]; itr = remain.erase(itr)) {
if (Y[e] <= H[*itr]) {
list.push_back(*itr);
}
}
list.push_back(R[e]);
remain.insert(R[e]);
const int n = (int)list.size();
vector<int> id(n);
for (int i = 0; i < n; ++i) {
id[i] = add_vertex();
vertices[list[i]].emplace_back(Y[e], id[i]);
}
for (int i = 0; i + 1 < n; ++i) {
add_edge(id[i], id[i + 1], X[list[i + 1]] - X[list[i]]);
}
}
std::reverse(order.begin(), order.end());
}
for (auto& vec : vertices) {
std::sort(vec.begin(), vec.end());
const int n = (int)vec.size();
for (int i = 0; i + 1 < n; ++i) {
add_edge(vec[i].second, vec[i + 1].second, vec[i + 1].first - vec[i].first);
}
}
vector<ll> dist(graph.size(), std::numeric_limits<ll>::max());
min_heap<pair<ll, int>> heap;
const auto push = [&](const int u, const ll d) {
if (dist[u] > d) {
dist[u] = d;
heap.emplace(d, u);
}
};
push(src, 0);
while (!heap.empty()) {
const auto [d, u] = heap.top();
heap.pop();
if (d > dist[u]) {
continue;
}
for (const auto& [v, c] : graph[u]) {
push(v, d + c);
}
}
const ll ret = dist[dst];
return ret == std::numeric_limits<ll>::max() ? -1 : ret;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
300 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
720 ms |
69112 KB |
Output is correct |
4 |
Correct |
1020 ms |
91100 KB |
Output is correct |
5 |
Correct |
591 ms |
71920 KB |
Output is correct |
6 |
Correct |
587 ms |
68500 KB |
Output is correct |
7 |
Correct |
610 ms |
72008 KB |
Output is correct |
8 |
Correct |
753 ms |
72404 KB |
Output is correct |
9 |
Correct |
774 ms |
80776 KB |
Output is correct |
10 |
Correct |
1020 ms |
91180 KB |
Output is correct |
11 |
Correct |
735 ms |
64208 KB |
Output is correct |
12 |
Correct |
605 ms |
57156 KB |
Output is correct |
13 |
Correct |
1052 ms |
96244 KB |
Output is correct |
14 |
Correct |
485 ms |
57372 KB |
Output is correct |
15 |
Correct |
532 ms |
60048 KB |
Output is correct |
16 |
Correct |
495 ms |
57796 KB |
Output is correct |
17 |
Correct |
581 ms |
55896 KB |
Output is correct |
18 |
Correct |
287 ms |
59016 KB |
Output is correct |
19 |
Correct |
12 ms |
3216 KB |
Output is correct |
20 |
Correct |
139 ms |
29520 KB |
Output is correct |
21 |
Correct |
490 ms |
51956 KB |
Output is correct |
22 |
Correct |
564 ms |
57116 KB |
Output is correct |
23 |
Correct |
420 ms |
71296 KB |
Output is correct |
24 |
Correct |
538 ms |
56464 KB |
Output is correct |
25 |
Correct |
629 ms |
55128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
178 ms |
23800 KB |
Output is correct |
2 |
Correct |
804 ms |
80540 KB |
Output is correct |
3 |
Correct |
960 ms |
86028 KB |
Output is correct |
4 |
Correct |
1323 ms |
118600 KB |
Output is correct |
5 |
Correct |
1342 ms |
123408 KB |
Output is correct |
6 |
Correct |
1249 ms |
116988 KB |
Output is correct |
7 |
Correct |
692 ms |
75524 KB |
Output is correct |
8 |
Correct |
598 ms |
57200 KB |
Output is correct |
9 |
Correct |
1146 ms |
111296 KB |
Output is correct |
10 |
Correct |
507 ms |
79628 KB |
Output is correct |
11 |
Correct |
44 ms |
9276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
178 ms |
23800 KB |
Output is correct |
2 |
Correct |
804 ms |
80540 KB |
Output is correct |
3 |
Correct |
960 ms |
86028 KB |
Output is correct |
4 |
Correct |
1323 ms |
118600 KB |
Output is correct |
5 |
Correct |
1342 ms |
123408 KB |
Output is correct |
6 |
Correct |
1249 ms |
116988 KB |
Output is correct |
7 |
Correct |
692 ms |
75524 KB |
Output is correct |
8 |
Correct |
598 ms |
57200 KB |
Output is correct |
9 |
Correct |
1146 ms |
111296 KB |
Output is correct |
10 |
Correct |
507 ms |
79628 KB |
Output is correct |
11 |
Correct |
44 ms |
9276 KB |
Output is correct |
12 |
Correct |
908 ms |
85276 KB |
Output is correct |
13 |
Correct |
890 ms |
107548 KB |
Output is correct |
14 |
Correct |
1255 ms |
114716 KB |
Output is correct |
15 |
Correct |
1008 ms |
91284 KB |
Output is correct |
16 |
Correct |
1027 ms |
96532 KB |
Output is correct |
17 |
Correct |
1258 ms |
108956 KB |
Output is correct |
18 |
Correct |
1060 ms |
91332 KB |
Output is correct |
19 |
Correct |
1040 ms |
96752 KB |
Output is correct |
20 |
Correct |
681 ms |
67508 KB |
Output is correct |
21 |
Correct |
155 ms |
26376 KB |
Output is correct |
22 |
Correct |
768 ms |
87892 KB |
Output is correct |
23 |
Correct |
697 ms |
83184 KB |
Output is correct |
24 |
Correct |
567 ms |
65872 KB |
Output is correct |
25 |
Correct |
642 ms |
78604 KB |
Output is correct |
26 |
Correct |
429 ms |
54064 KB |
Output is correct |
27 |
Correct |
1227 ms |
116940 KB |
Output is correct |
28 |
Correct |
888 ms |
105072 KB |
Output is correct |
29 |
Correct |
1200 ms |
106224 KB |
Output is correct |
30 |
Correct |
633 ms |
67788 KB |
Output is correct |
31 |
Correct |
1089 ms |
101100 KB |
Output is correct |
32 |
Correct |
573 ms |
64248 KB |
Output is correct |
33 |
Correct |
614 ms |
68236 KB |
Output is correct |
34 |
Correct |
496 ms |
73724 KB |
Output is correct |
35 |
Correct |
609 ms |
71432 KB |
Output is correct |
36 |
Correct |
688 ms |
62852 KB |
Output is correct |
37 |
Correct |
604 ms |
52072 KB |
Output is correct |
38 |
Correct |
611 ms |
57108 KB |
Output is correct |
39 |
Correct |
472 ms |
71420 KB |
Output is correct |
40 |
Correct |
497 ms |
56320 KB |
Output is correct |
41 |
Correct |
562 ms |
55068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
300 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
720 ms |
69112 KB |
Output is correct |
21 |
Correct |
1020 ms |
91100 KB |
Output is correct |
22 |
Correct |
591 ms |
71920 KB |
Output is correct |
23 |
Correct |
587 ms |
68500 KB |
Output is correct |
24 |
Correct |
610 ms |
72008 KB |
Output is correct |
25 |
Correct |
753 ms |
72404 KB |
Output is correct |
26 |
Correct |
774 ms |
80776 KB |
Output is correct |
27 |
Correct |
1020 ms |
91180 KB |
Output is correct |
28 |
Correct |
735 ms |
64208 KB |
Output is correct |
29 |
Correct |
605 ms |
57156 KB |
Output is correct |
30 |
Correct |
1052 ms |
96244 KB |
Output is correct |
31 |
Correct |
485 ms |
57372 KB |
Output is correct |
32 |
Correct |
532 ms |
60048 KB |
Output is correct |
33 |
Correct |
495 ms |
57796 KB |
Output is correct |
34 |
Correct |
581 ms |
55896 KB |
Output is correct |
35 |
Correct |
287 ms |
59016 KB |
Output is correct |
36 |
Correct |
12 ms |
3216 KB |
Output is correct |
37 |
Correct |
139 ms |
29520 KB |
Output is correct |
38 |
Correct |
490 ms |
51956 KB |
Output is correct |
39 |
Correct |
564 ms |
57116 KB |
Output is correct |
40 |
Correct |
420 ms |
71296 KB |
Output is correct |
41 |
Correct |
538 ms |
56464 KB |
Output is correct |
42 |
Correct |
629 ms |
55128 KB |
Output is correct |
43 |
Correct |
178 ms |
23800 KB |
Output is correct |
44 |
Correct |
804 ms |
80540 KB |
Output is correct |
45 |
Correct |
960 ms |
86028 KB |
Output is correct |
46 |
Correct |
1323 ms |
118600 KB |
Output is correct |
47 |
Correct |
1342 ms |
123408 KB |
Output is correct |
48 |
Correct |
1249 ms |
116988 KB |
Output is correct |
49 |
Correct |
692 ms |
75524 KB |
Output is correct |
50 |
Correct |
598 ms |
57200 KB |
Output is correct |
51 |
Correct |
1146 ms |
111296 KB |
Output is correct |
52 |
Correct |
507 ms |
79628 KB |
Output is correct |
53 |
Correct |
44 ms |
9276 KB |
Output is correct |
54 |
Correct |
908 ms |
85276 KB |
Output is correct |
55 |
Correct |
890 ms |
107548 KB |
Output is correct |
56 |
Correct |
1255 ms |
114716 KB |
Output is correct |
57 |
Correct |
1008 ms |
91284 KB |
Output is correct |
58 |
Correct |
1027 ms |
96532 KB |
Output is correct |
59 |
Correct |
1258 ms |
108956 KB |
Output is correct |
60 |
Correct |
1060 ms |
91332 KB |
Output is correct |
61 |
Correct |
1040 ms |
96752 KB |
Output is correct |
62 |
Correct |
681 ms |
67508 KB |
Output is correct |
63 |
Correct |
155 ms |
26376 KB |
Output is correct |
64 |
Correct |
768 ms |
87892 KB |
Output is correct |
65 |
Correct |
697 ms |
83184 KB |
Output is correct |
66 |
Correct |
567 ms |
65872 KB |
Output is correct |
67 |
Correct |
642 ms |
78604 KB |
Output is correct |
68 |
Correct |
429 ms |
54064 KB |
Output is correct |
69 |
Correct |
1227 ms |
116940 KB |
Output is correct |
70 |
Correct |
888 ms |
105072 KB |
Output is correct |
71 |
Correct |
1200 ms |
106224 KB |
Output is correct |
72 |
Correct |
633 ms |
67788 KB |
Output is correct |
73 |
Correct |
1089 ms |
101100 KB |
Output is correct |
74 |
Correct |
573 ms |
64248 KB |
Output is correct |
75 |
Correct |
614 ms |
68236 KB |
Output is correct |
76 |
Correct |
496 ms |
73724 KB |
Output is correct |
77 |
Correct |
609 ms |
71432 KB |
Output is correct |
78 |
Correct |
688 ms |
62852 KB |
Output is correct |
79 |
Correct |
604 ms |
52072 KB |
Output is correct |
80 |
Correct |
611 ms |
57108 KB |
Output is correct |
81 |
Correct |
472 ms |
71420 KB |
Output is correct |
82 |
Correct |
497 ms |
56320 KB |
Output is correct |
83 |
Correct |
562 ms |
55068 KB |
Output is correct |
84 |
Correct |
170 ms |
19468 KB |
Output is correct |
85 |
Correct |
950 ms |
90308 KB |
Output is correct |
86 |
Correct |
1575 ms |
146796 KB |
Output is correct |
87 |
Correct |
262 ms |
41144 KB |
Output is correct |
88 |
Correct |
256 ms |
35496 KB |
Output is correct |
89 |
Correct |
275 ms |
41088 KB |
Output is correct |
90 |
Correct |
61 ms |
7948 KB |
Output is correct |
91 |
Correct |
2 ms |
724 KB |
Output is correct |
92 |
Correct |
32 ms |
5980 KB |
Output is correct |
93 |
Correct |
498 ms |
55512 KB |
Output is correct |
94 |
Correct |
148 ms |
28596 KB |
Output is correct |
95 |
Correct |
716 ms |
91580 KB |
Output is correct |
96 |
Correct |
710 ms |
83776 KB |
Output is correct |
97 |
Correct |
612 ms |
67120 KB |
Output is correct |
98 |
Correct |
636 ms |
78576 KB |
Output is correct |
99 |
Correct |
2053 ms |
193272 KB |
Output is correct |
100 |
Correct |
1266 ms |
108084 KB |
Output is correct |
101 |
Correct |
1467 ms |
133852 KB |
Output is correct |
102 |
Correct |
671 ms |
68148 KB |
Output is correct |
103 |
Correct |
485 ms |
59888 KB |
Output is correct |
104 |
Correct |
540 ms |
62136 KB |
Output is correct |
105 |
Correct |
463 ms |
67972 KB |
Output is correct |
106 |
Correct |
527 ms |
63864 KB |
Output is correct |
107 |
Correct |
468 ms |
65432 KB |
Output is correct |
108 |
Correct |
52 ms |
9120 KB |
Output is correct |
109 |
Correct |
997 ms |
90724 KB |
Output is correct |
110 |
Correct |
903 ms |
106636 KB |
Output is correct |
111 |
Correct |
793 ms |
106508 KB |
Output is correct |
112 |
Correct |
581 ms |
69116 KB |
Output is correct |
113 |
Correct |
640 ms |
64624 KB |
Output is correct |