#include "bits/extc++.h"
using namespace std;
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t;
((cerr << " | " << u), ...);
cerr << endl;
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
dbgh(__VA_ARGS__)
#else
#define dbg(...)
#define cerr \
if (false) \
cerr
#endif
using ll = long long;
#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))
template <typename T>
using min_pq = priority_queue<T, vector<T>, greater<T>>;
template <typename A, typename B>
ostream& operator<<(ostream& out, const pair<A, B>& p) {
return out << "(" << p.first << ", " << p.second << ")";
}
template <typename Cb>
struct CmpByKey {
Cb cb;
CmpByKey(const Cb& cb) : cb(cb) {}
template <typename T>
bool operator()(const T& a, const T& b) const {
return cb(a) < cb(b);
}
};
template <typename T>
struct Comp {
map<T, int> mp;
int operator()(const T& t) {
auto [it, inserted] = mp.emplace(t, -1);
if (inserted) {
it->second = sz(mp) - 1;
}
return it->second;
}
};
template <typename T, typename Cmp = less<T>>
vector<T> sorted(vector<T> arr, Cmp cmp = Cmp()) {
sort(begin(arr), end(arr), cmp);
return arr;
}
template <typename S>
long dijkstra(const vector<tuple<S, S, long>>& edges, S src, S dest) {
dbg("A");
int n = 2 * sz(edges) + 10;
Comp<S> comp;
vector<pair<int, long>> graph[n];
for (auto& [u, v, w] : edges) {
int cu = comp(u), cv = comp(v);
graph[cu].emplace_back(cv, w);
graph[cv].emplace_back(cu, w);
}
long dist[n];
memset(dist, 0x3f, sizeof(dist));
min_pq<pair<long, int>> pq;
auto push = [&](int u, long d) -> void {
if (d >= dist[u]) {
return;
}
dist[u] = d;
pq.emplace(d, u);
};
int c_src = comp(src), c_dest = comp(dest);
push(c_src, 0);
while (sz(pq)) {
auto [d, u] = pq.top();
pq.pop();
if (u == c_dest) {
return d;
}
if (d != dist[u]) {
continue;
}
for (auto& [v, w] : graph[u]) {
push(v, d + w);
}
}
return -1;
}
namespace s1 {
long solve(vector<pair<int, int>> arr,
vector<array<int, 3>> walk,
int src,
int dest) {
map<int, vector<pair<int, int>>> evts;
for (auto& [x, h] : arr) {
evts[-h].emplace_back(x, -1);
}
for (auto& [ql, qr, h] : walk) {
evts[-h].emplace_back(ql, qr);
}
vector<array<int, 3>> d_walk;
{
set<int> pos;
for (auto& [nh, ev] : evts) {
int h = -nh;
for (auto& [x, ty] : ev) {
if (ty == -1) {
dbg(h, x);
pos.insert(x);
}
}
for (auto& [ql, qr] : ev) {
if (qr == -1) {
continue;
}
dbg(h, ql, qr);
auto it = pos.lower_bound(ql);
while (true) {
int cl = *it;
++it;
int cr = *it;
d_walk.push_back({cl, cr, h});
dbg("A", *it, qr);
assert(*it <= qr);
if (*it == qr) {
break;
}
}
}
}
}
for (auto& [ql, qr, h] : d_walk) {
dbg(ql, qr, h);
}
using S = pair<int, int>;
S s_src {src, 0}, s_dest {dest, 0};
vector<S> states {s_src, s_dest};
for (auto& [ql, qr, h] : d_walk) {
states.emplace_back(ql, h);
states.emplace_back(qr, h);
}
sort(begin(states), end(states));
states.erase(unique(begin(states), end(states)), states.end());
map<int, vector<int>> buildings;
for (auto& [x, y] : states) {
buildings[x].push_back(y);
}
vector<tuple<S, S, long>> edges;
for (auto& [x, ys] : buildings) {
sort(begin(ys), end(ys));
for (int i = 0; i + 1 < sz(ys); i++) {
edges.emplace_back(S(x, ys[i]), S(x, ys[i + 1]), ys[i + 1] - ys[i]);
}
}
for (auto& [ql, qr, h] : d_walk) {
edges.emplace_back(S(ql, h), S(qr, h), qr - ql);
}
for (auto& [u, v, w] : edges) {
dbg(u, v, w);
}
return dijkstra(edges, s_src, s_dest);
}
} // namespace s1
namespace s2 {
template <typename Cb>
void add_edges(vector<array<int, 4>> arr, const Cb& cb) {
for (auto& [_ql, qr, _i, _] : arr) {
// [] -> [)
qr++;
}
int n = sz(arr);
map<pair<int, int>, int> mp;
auto split = [&](int ind) -> void {
auto it = mp.lower_bound({ind + 1, -1});
if (it == mp.begin()) {
return;
}
--it;
auto [cql, cqr] = it->first;
if (cql < ind && ind < cqr) {
int cv = it->second;
mp.erase(it);
mp[{cql, ind}] = cv;
mp[{ind, cqr}] = cv;
}
};
for (auto& [ql, qr, qi, _] : arr) {
split(ql);
split(qr);
for (auto it = mp.lower_bound({ql, -1}); it != mp.end();
it = mp.erase(it)) {
auto [cql, cqr] = it->first;
if (qr <= cql) {
break;
}
assert(ql <= cql && cqr <= qr);
cb(qi, it->second);
}
mp[{ql, qr}] = qi;
}
}
long solve(vector<pair<int, int>> arr,
vector<array<int, 3>> walk,
int src,
int dest) {
int c_src = sz(walk), c_dest = sz(walk) + 1;
walk.push_back({src, src, 0});
walk.push_back({dest, dest, 0});
vector<tuple<int, int, long>> edges;
// auto inter = [&](int l1, int r1, int l2, int r2) -> bool {
// if (l1 > l2) {
// swap(l1, l2);
// swap(r1, r2);
// }
// return l2 <= r1;
// };
// for (int i = 0; i < sz(walk); i++) {
// for (int j = i + 1; j < sz(walk); j++) {
// auto& [l1, r1, h1] = walk[i];
// auto& [l2, r2, h2] = walk[j];
// if (inter(l1, r1, l2, r2)) {
// dbg(i, j);
// edges.emplace_back(i, j, abs(h1 - h2));
// }
// }
// }
vector<array<int, 4>> n_walk;
for (int i = 0; i < sz(walk); i++) {
auto& [ql, qr, qh] = walk[i];
n_walk.push_back({ql, qr, i, qh});
}
auto cb_edge = [&](int u, int v) -> void {
dbg(u, v);
edges.emplace_back(u, v, abs(walk[u][2] - walk[v][2]));
};
add_edges(
sorted(n_walk, CmpByKey([&](const auto& a) -> int { return a[3]; })),
cb_edge);
add_edges(
sorted(n_walk, CmpByKey([&](const auto& a) -> int { return -a[3]; })),
cb_edge);
long ans = dijkstra(edges, c_src, c_dest);
if (ans == -1) {
return ans;
} else {
return ans + dest - src;
}
}
} // namespace s2
ll min_distance(vector<int> arr_x,
vector<int> arr_h,
vector<int> walk_l,
vector<int> walk_r,
vector<int> walk_y,
int src,
int dest) {
int n = sz(arr_x), m = sz(walk_l);
vector<pair<int, int>> arr(n);
for (int i = 0; i < n; i++) {
arr[i] = {arr_x[i], arr_h[i]};
}
vector<array<int, 3>> walk(m);
for (int i = 0; i < m; i++) {
walk[i] = {arr_x[walk_l[i]], arr_x[walk_r[i]], walk_y[i]};
}
src = arr_x[src];
dest = arr_x[dest];
if (src == arr_x[0] && dest == arr_x[n - 1]) {
return s2::solve(arr, walk, src, dest);
} else {
return s1::solve(arr, walk, src, dest);
}
}
Compilation message
walk.cpp: In function 'int64_t s1::solve(std::vector<std::pair<int, int> >, std::vector<std::array<int, 3> >, int, int)':
walk.cpp:167:16: warning: unused structured binding declaration [-Wunused-variable]
167 | for (auto& [ql, qr, h] : d_walk) {
| ^~~~~~~~~~~
walk.cpp:201:16: warning: unused structured binding declaration [-Wunused-variable]
201 | for (auto& [u, v, w] : edges) {
| ^~~~~~~~~
walk.cpp: In instantiation of 'void s2::add_edges(std::vector<std::array<int, 4> >, const Cb&) [with Cb = s2::solve(std::vector<std::pair<int, int> >, std::vector<std::array<int, 3> >, int, int)::<lambda(int, int)>]':
walk.cpp:300:16: required from here
walk.cpp:219:9: warning: unused variable 'n' [-Wunused-variable]
219 | int n = sz(arr);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
428 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 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 |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1115 ms |
282816 KB |
Output is correct |
4 |
Correct |
1241 ms |
290276 KB |
Output is correct |
5 |
Correct |
855 ms |
249412 KB |
Output is correct |
6 |
Correct |
777 ms |
216420 KB |
Output is correct |
7 |
Correct |
851 ms |
249588 KB |
Output is correct |
8 |
Correct |
1466 ms |
361404 KB |
Output is correct |
9 |
Correct |
1002 ms |
248660 KB |
Output is correct |
10 |
Correct |
1944 ms |
409772 KB |
Output is correct |
11 |
Correct |
626 ms |
145776 KB |
Output is correct |
12 |
Correct |
379 ms |
44936 KB |
Output is correct |
13 |
Correct |
645 ms |
87308 KB |
Output is correct |
14 |
Correct |
304 ms |
79844 KB |
Output is correct |
15 |
Correct |
291 ms |
70908 KB |
Output is correct |
16 |
Correct |
285 ms |
72980 KB |
Output is correct |
17 |
Correct |
287 ms |
70312 KB |
Output is correct |
18 |
Correct |
158 ms |
45048 KB |
Output is correct |
19 |
Correct |
9 ms |
4024 KB |
Output is correct |
20 |
Correct |
106 ms |
37368 KB |
Output is correct |
21 |
Correct |
442 ms |
54700 KB |
Output is correct |
22 |
Correct |
378 ms |
57896 KB |
Output is correct |
23 |
Correct |
231 ms |
59144 KB |
Output is correct |
24 |
Correct |
369 ms |
56456 KB |
Output is correct |
25 |
Correct |
352 ms |
52920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
197 ms |
36632 KB |
Output is correct |
2 |
Correct |
380 ms |
86876 KB |
Output is correct |
3 |
Correct |
389 ms |
88404 KB |
Output is correct |
4 |
Correct |
394 ms |
90836 KB |
Output is correct |
5 |
Correct |
367 ms |
92552 KB |
Output is correct |
6 |
Correct |
328 ms |
83912 KB |
Output is correct |
7 |
Correct |
202 ms |
45904 KB |
Output is correct |
8 |
Correct |
377 ms |
41012 KB |
Output is correct |
9 |
Correct |
297 ms |
79532 KB |
Output is correct |
10 |
Correct |
260 ms |
48408 KB |
Output is correct |
11 |
Correct |
8 ms |
1848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
197 ms |
36632 KB |
Output is correct |
2 |
Correct |
380 ms |
86876 KB |
Output is correct |
3 |
Correct |
389 ms |
88404 KB |
Output is correct |
4 |
Correct |
394 ms |
90836 KB |
Output is correct |
5 |
Correct |
367 ms |
92552 KB |
Output is correct |
6 |
Correct |
328 ms |
83912 KB |
Output is correct |
7 |
Correct |
202 ms |
45904 KB |
Output is correct |
8 |
Correct |
377 ms |
41012 KB |
Output is correct |
9 |
Correct |
297 ms |
79532 KB |
Output is correct |
10 |
Correct |
260 ms |
48408 KB |
Output is correct |
11 |
Correct |
8 ms |
1848 KB |
Output is correct |
12 |
Correct |
373 ms |
88340 KB |
Output is correct |
13 |
Correct |
400 ms |
90992 KB |
Output is correct |
14 |
Correct |
362 ms |
92564 KB |
Output is correct |
15 |
Correct |
312 ms |
58212 KB |
Output is correct |
16 |
Correct |
388 ms |
74480 KB |
Output is correct |
17 |
Correct |
458 ms |
88656 KB |
Output is correct |
18 |
Correct |
328 ms |
58264 KB |
Output is correct |
19 |
Correct |
387 ms |
74544 KB |
Output is correct |
20 |
Correct |
191 ms |
46576 KB |
Output is correct |
21 |
Correct |
19 ms |
4028 KB |
Output is correct |
22 |
Correct |
394 ms |
71000 KB |
Output is correct |
23 |
Correct |
404 ms |
68636 KB |
Output is correct |
24 |
Correct |
361 ms |
53212 KB |
Output is correct |
25 |
Correct |
399 ms |
63232 KB |
Output is correct |
26 |
Correct |
292 ms |
44960 KB |
Output is correct |
27 |
Correct |
360 ms |
92192 KB |
Output is correct |
28 |
Correct |
524 ms |
91912 KB |
Output is correct |
29 |
Correct |
322 ms |
83600 KB |
Output is correct |
30 |
Correct |
200 ms |
45812 KB |
Output is correct |
31 |
Correct |
309 ms |
79464 KB |
Output is correct |
32 |
Correct |
329 ms |
50472 KB |
Output is correct |
33 |
Correct |
363 ms |
51000 KB |
Output is correct |
34 |
Correct |
240 ms |
51728 KB |
Output is correct |
35 |
Correct |
355 ms |
59476 KB |
Output is correct |
36 |
Correct |
421 ms |
50448 KB |
Output is correct |
37 |
Correct |
461 ms |
51956 KB |
Output is correct |
38 |
Correct |
423 ms |
54740 KB |
Output is correct |
39 |
Correct |
251 ms |
56240 KB |
Output is correct |
40 |
Correct |
384 ms |
53412 KB |
Output is correct |
41 |
Correct |
403 ms |
50200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
428 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 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 |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1115 ms |
282816 KB |
Output is correct |
21 |
Correct |
1241 ms |
290276 KB |
Output is correct |
22 |
Correct |
855 ms |
249412 KB |
Output is correct |
23 |
Correct |
777 ms |
216420 KB |
Output is correct |
24 |
Correct |
851 ms |
249588 KB |
Output is correct |
25 |
Correct |
1466 ms |
361404 KB |
Output is correct |
26 |
Correct |
1002 ms |
248660 KB |
Output is correct |
27 |
Correct |
1944 ms |
409772 KB |
Output is correct |
28 |
Correct |
626 ms |
145776 KB |
Output is correct |
29 |
Correct |
379 ms |
44936 KB |
Output is correct |
30 |
Correct |
645 ms |
87308 KB |
Output is correct |
31 |
Correct |
304 ms |
79844 KB |
Output is correct |
32 |
Correct |
291 ms |
70908 KB |
Output is correct |
33 |
Correct |
285 ms |
72980 KB |
Output is correct |
34 |
Correct |
287 ms |
70312 KB |
Output is correct |
35 |
Correct |
158 ms |
45048 KB |
Output is correct |
36 |
Correct |
9 ms |
4024 KB |
Output is correct |
37 |
Correct |
106 ms |
37368 KB |
Output is correct |
38 |
Correct |
442 ms |
54700 KB |
Output is correct |
39 |
Correct |
378 ms |
57896 KB |
Output is correct |
40 |
Correct |
231 ms |
59144 KB |
Output is correct |
41 |
Correct |
369 ms |
56456 KB |
Output is correct |
42 |
Correct |
352 ms |
52920 KB |
Output is correct |
43 |
Correct |
197 ms |
36632 KB |
Output is correct |
44 |
Correct |
380 ms |
86876 KB |
Output is correct |
45 |
Correct |
389 ms |
88404 KB |
Output is correct |
46 |
Correct |
394 ms |
90836 KB |
Output is correct |
47 |
Correct |
367 ms |
92552 KB |
Output is correct |
48 |
Correct |
328 ms |
83912 KB |
Output is correct |
49 |
Correct |
202 ms |
45904 KB |
Output is correct |
50 |
Correct |
377 ms |
41012 KB |
Output is correct |
51 |
Correct |
297 ms |
79532 KB |
Output is correct |
52 |
Correct |
260 ms |
48408 KB |
Output is correct |
53 |
Correct |
8 ms |
1848 KB |
Output is correct |
54 |
Correct |
373 ms |
88340 KB |
Output is correct |
55 |
Correct |
400 ms |
90992 KB |
Output is correct |
56 |
Correct |
362 ms |
92564 KB |
Output is correct |
57 |
Correct |
312 ms |
58212 KB |
Output is correct |
58 |
Correct |
388 ms |
74480 KB |
Output is correct |
59 |
Correct |
458 ms |
88656 KB |
Output is correct |
60 |
Correct |
328 ms |
58264 KB |
Output is correct |
61 |
Correct |
387 ms |
74544 KB |
Output is correct |
62 |
Correct |
191 ms |
46576 KB |
Output is correct |
63 |
Correct |
19 ms |
4028 KB |
Output is correct |
64 |
Correct |
394 ms |
71000 KB |
Output is correct |
65 |
Correct |
404 ms |
68636 KB |
Output is correct |
66 |
Correct |
361 ms |
53212 KB |
Output is correct |
67 |
Correct |
399 ms |
63232 KB |
Output is correct |
68 |
Correct |
292 ms |
44960 KB |
Output is correct |
69 |
Correct |
360 ms |
92192 KB |
Output is correct |
70 |
Correct |
524 ms |
91912 KB |
Output is correct |
71 |
Correct |
322 ms |
83600 KB |
Output is correct |
72 |
Correct |
200 ms |
45812 KB |
Output is correct |
73 |
Correct |
309 ms |
79464 KB |
Output is correct |
74 |
Correct |
329 ms |
50472 KB |
Output is correct |
75 |
Correct |
363 ms |
51000 KB |
Output is correct |
76 |
Correct |
240 ms |
51728 KB |
Output is correct |
77 |
Correct |
355 ms |
59476 KB |
Output is correct |
78 |
Correct |
421 ms |
50448 KB |
Output is correct |
79 |
Correct |
461 ms |
51956 KB |
Output is correct |
80 |
Correct |
423 ms |
54740 KB |
Output is correct |
81 |
Correct |
251 ms |
56240 KB |
Output is correct |
82 |
Correct |
384 ms |
53412 KB |
Output is correct |
83 |
Correct |
403 ms |
50200 KB |
Output is correct |
84 |
Correct |
86 ms |
24372 KB |
Output is correct |
85 |
Runtime error |
1524 ms |
1048576 KB |
Execution killed with signal 9 |
86 |
Halted |
0 ms |
0 KB |
- |