#include "walk.h"
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
using i64 = long long;
using d64 = long double;
using pi = pair<int, int>;
using pli = pair<i64, i64>;
using ti = tuple<int, int, int>;
using tli = tuple<i64, i64, i64>;
#define iterall(cont) cont.begin(), cont.end()
#define prec(n) setprecision(n) << fixed
const i64 inf = numeric_limits<i64>::max() / 3;
vector<i64> dijkstra(int sv, const vector<vector<pli>> &adj) {
vector<i64> dist(adj.size(), inf);
priority_queue<pli, vector<pli>, greater<>> pq;
dist[sv] = 0;
pq.emplace(0, sv);
while (!pq.empty()) {
auto [W, u] = pq.top();
pq.pop();
if (W > dist[u]) continue;
for (auto [v, w] : adj[u]) {
if (dist[v] > W + w) {
pq.emplace(W + w, v);
dist[v] = W + w;
}
}
}
return dist;
}
class Edge {
public:
pair<int, i64> u, v;
i64 w;
Edge() = default;
Edge(decltype(u) _u, decltype(v) _v, decltype(w) _w) {
if (_u > _v) swap(_u, _v);
u = _u, v = _v, w = _w;
}
};
bool edgeCompare(const Edge &lhs, const Edge &rhs) {
return lhs.u == rhs.u ? lhs.v < rhs.v : lhs.u < rhs.u;
}
bool edgeEqual(const Edge &lhs, const Edge &rhs) {
return lhs.u == rhs.u && lhs.v == rhs.v;
}
namespace Helper {
bool inRange(int x, int s, int e) { return s <= x && x <= e; }
template <typename T>
int findIndex(const vector<T> &vec, T &&toFind) {
return lower_bound(iterall(vec), toFind) - vec.begin();
}
template <typename T>
int findIndex(const vector<T> &vec, const T &toFind) {
return lower_bound(iterall(vec), toFind) - vec.begin();
}
} // namespace Helper
const bool DEBUG = false;
i64 min_distance(vector<int> x, vector<int> h, vector<int> l, vector<int> r,
vector<int> y, int s, int g) {
int N = x.size();
int M = l.size();
vector<tuple<i64, int, int>> horizontalLines;
for (int i = 0; i < M; i++) horizontalLines.emplace_back(y[i], l[i], r[i]);
sort(iterall(horizontalLines));
vector<pair<i64, int>> verticalLines;
for (int i = 0; i < N; i++) verticalLines.emplace_back(h[i], i);
sort(iterall(verticalLines), greater<>());
set<int> verticalSet;
for (int i = 0; i < N; i++) verticalSet.emplace(i);
vector<Edge> edge;
vector<pair<int, i64>> vertex;
int vertNum = 2;
vertex.emplace_back(s, 0);
vertex.emplace_back(g, 0);
set<pi> adjacentForwardLook = {pi(s, 0), pi(g, 1)}; // y Increasing
vector<vector<int>> horizontalVertex(M);
vector<vector<int>> horizontalVertexAppended(M);
auto appendVertex = [&](int x, int i) mutable {
vertex.emplace_back(x, get<0>(horizontalLines[i]));
adjacentForwardLook.emplace(x, vertNum);
horizontalVertex[i].emplace_back(vertNum);
horizontalVertexAppended[i].emplace_back(vertNum);
++vertNum;
};
for (int i = 0; i < M; i++) {
auto [Y, L, R] = horizontalLines[i];
while (!verticalLines.empty() && verticalLines.back().first < Y) {
verticalSet.erase(verticalLines.back().second);
verticalLines.pop_back();
}
{
auto siter = adjacentForwardLook.lower_bound({L, 0});
auto eiter = adjacentForwardLook.upper_bound({R, vertNum});
for (auto it = siter; it != eiter; it++) {
if (Y > h[it->first]) continue;
vertex.emplace_back(it->first, Y);
edge.emplace_back(Edge(vertex[it->second], vertex[vertNum],
Y - vertex[it->second].second));
horizontalVertexAppended[i].emplace_back(vertNum);
++vertNum;
}
adjacentForwardLook.erase(siter, eiter);
}
appendVertex(L, i);
appendVertex(R, i);
if (Helper::inRange(s, L, R)) {
if (Y <= h[s]) {
appendVertex(s, i);
} else {
auto iter = verticalSet.lower_bound(s);
if (iter != verticalSet.end() && Helper::inRange(*iter, L, R)) {
appendVertex(*iter, i);
}
--iter;
if (iter != verticalSet.end() && Helper::inRange(*iter, L, R)) {
appendVertex(*iter, i);
}
}
}
if (Helper::inRange(g, L, R)) {
if (Y <= h[g]) {
appendVertex(g, i);
} else {
auto iter = verticalSet.lower_bound(g);
if (iter != verticalSet.end() && Helper::inRange(*iter, L, R)) {
appendVertex(*iter, i);
}
--iter;
if (iter != verticalSet.end() && Helper::inRange(*iter, L, R)) {
appendVertex(*iter, i);
}
}
}
}
set<pi> adjacentBackwardLook;
for (int i = M - 1; i >= 0; i--) {
auto [Y, L, R] = horizontalLines[i];
auto siter = adjacentBackwardLook.lower_bound({L, 0});
auto eiter = adjacentBackwardLook.upper_bound({R, vertNum});
for (auto it = siter; it != eiter; it++) {
vertex.emplace_back(it->first, Y);
edge.emplace_back(Edge(vertex[it->second], vertex[vertNum],
vertex[it->second].second - Y));
horizontalVertexAppended[i].emplace_back(vertNum);
++vertNum;
}
adjacentBackwardLook.erase(siter, eiter);
for (auto vn : horizontalVertex[i]) {
adjacentBackwardLook.emplace(vertex[vn].first, vn);
}
}
for (int i = 0; i < M; i++) {
auto &hvX = horizontalVertexAppended[i];
sort(iterall(hvX), [&vertex](int l, int r) {
return vertex[l].first < vertex[r].first;
});
hvX.erase(unique(iterall(hvX),
[&vertex](int l, int r) {
return vertex[l].first == vertex[r].first;
}),
hvX.end());
for (int j = 0; j < hvX.size() - 1; j++) {
int lvn = hvX[j];
int rvn = hvX[j + 1];
edge.emplace_back(
Edge(vertex[lvn], vertex[rvn],
x[vertex[rvn].first] - x[vertex[lvn].first]));
}
}
sort(iterall(vertex));
vertex.erase(unique(iterall(vertex)), vertex.end());
sort(iterall(edge), edgeCompare);
edge.erase(unique(iterall(edge), edgeEqual), edge.end());
int V = vertex.size();
vector<vector<pli>> Graph(V);
for (auto ed : edge) {
auto uIndex = Helper::findIndex(vertex, ed.u);
auto vIndex = Helper::findIndex(vertex, ed.v);
Graph[uIndex].emplace_back(vIndex, ed.w);
Graph[vIndex].emplace_back(uIndex, ed.w);
}
if (DEBUG) {
for (auto [x, y] : vertex) cout << x << " " << y << endl;
for (auto ed : edge) {
cout << ed.u.first << " " << ed.u.second << " " << ed.v.first << " "
<< ed.v.second << " " << ed.w << endl;
}
}
int startVertex = Helper::findIndex(vertex, {s, 0});
int endVertex = Helper::findIndex(vertex, {g, 0});
i64 ret = dijkstra(startVertex, Graph)[endVertex];
if (ret >= inf) ret = -1;
return ret;
}
Compilation message
walk.cpp: In function 'i64 min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:213:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
213 | for (int j = 0; j < hvX.size() - 1; j++) {
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
896 ms |
101544 KB |
Output is correct |
4 |
Correct |
1170 ms |
102240 KB |
Output is correct |
5 |
Correct |
731 ms |
78088 KB |
Output is correct |
6 |
Correct |
697 ms |
75812 KB |
Output is correct |
7 |
Correct |
716 ms |
78216 KB |
Output is correct |
8 |
Correct |
883 ms |
110024 KB |
Output is correct |
9 |
Correct |
1000 ms |
99808 KB |
Output is correct |
10 |
Correct |
1109 ms |
110320 KB |
Output is correct |
11 |
Correct |
858 ms |
86372 KB |
Output is correct |
12 |
Correct |
689 ms |
65956 KB |
Output is correct |
13 |
Correct |
1106 ms |
113236 KB |
Output is correct |
14 |
Correct |
742 ms |
77868 KB |
Output is correct |
15 |
Correct |
590 ms |
70476 KB |
Output is correct |
16 |
Correct |
462 ms |
65044 KB |
Output is correct |
17 |
Correct |
474 ms |
62372 KB |
Output is correct |
18 |
Correct |
681 ms |
109868 KB |
Output is correct |
19 |
Correct |
35 ms |
4276 KB |
Output is correct |
20 |
Correct |
277 ms |
39464 KB |
Output is correct |
21 |
Correct |
517 ms |
67876 KB |
Output is correct |
22 |
Correct |
477 ms |
59944 KB |
Output is correct |
23 |
Correct |
564 ms |
79604 KB |
Output is correct |
24 |
Correct |
464 ms |
63652 KB |
Output is correct |
25 |
Correct |
495 ms |
66212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
232 ms |
34096 KB |
Output is correct |
2 |
Correct |
909 ms |
124608 KB |
Output is correct |
3 |
Correct |
966 ms |
128700 KB |
Output is correct |
4 |
Correct |
1067 ms |
136148 KB |
Output is correct |
5 |
Correct |
1387 ms |
141692 KB |
Output is correct |
6 |
Correct |
1206 ms |
134508 KB |
Output is correct |
7 |
Correct |
564 ms |
73228 KB |
Output is correct |
8 |
Correct |
611 ms |
66084 KB |
Output is correct |
9 |
Correct |
1114 ms |
130752 KB |
Output is correct |
10 |
Correct |
572 ms |
89312 KB |
Output is correct |
11 |
Correct |
28 ms |
4340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
232 ms |
34096 KB |
Output is correct |
2 |
Correct |
909 ms |
124608 KB |
Output is correct |
3 |
Correct |
966 ms |
128700 KB |
Output is correct |
4 |
Correct |
1067 ms |
136148 KB |
Output is correct |
5 |
Correct |
1387 ms |
141692 KB |
Output is correct |
6 |
Correct |
1206 ms |
134508 KB |
Output is correct |
7 |
Correct |
564 ms |
73228 KB |
Output is correct |
8 |
Correct |
611 ms |
66084 KB |
Output is correct |
9 |
Correct |
1114 ms |
130752 KB |
Output is correct |
10 |
Correct |
572 ms |
89312 KB |
Output is correct |
11 |
Correct |
28 ms |
4340 KB |
Output is correct |
12 |
Correct |
970 ms |
130156 KB |
Output is correct |
13 |
Correct |
970 ms |
135900 KB |
Output is correct |
14 |
Correct |
1278 ms |
141644 KB |
Output is correct |
15 |
Correct |
936 ms |
115024 KB |
Output is correct |
16 |
Correct |
1052 ms |
118024 KB |
Output is correct |
17 |
Correct |
1171 ms |
138184 KB |
Output is correct |
18 |
Correct |
856 ms |
115064 KB |
Output is correct |
19 |
Correct |
1035 ms |
118144 KB |
Output is correct |
20 |
Correct |
623 ms |
71020 KB |
Output is correct |
21 |
Correct |
123 ms |
11368 KB |
Output is correct |
22 |
Correct |
742 ms |
105276 KB |
Output is correct |
23 |
Correct |
783 ms |
99484 KB |
Output is correct |
24 |
Correct |
610 ms |
73276 KB |
Output is correct |
25 |
Correct |
714 ms |
92240 KB |
Output is correct |
26 |
Correct |
478 ms |
57236 KB |
Output is correct |
27 |
Correct |
1278 ms |
142408 KB |
Output is correct |
28 |
Correct |
1032 ms |
133920 KB |
Output is correct |
29 |
Correct |
1264 ms |
134432 KB |
Output is correct |
30 |
Correct |
640 ms |
72640 KB |
Output is correct |
31 |
Correct |
1330 ms |
130632 KB |
Output is correct |
32 |
Correct |
632 ms |
80188 KB |
Output is correct |
33 |
Correct |
540 ms |
74404 KB |
Output is correct |
34 |
Correct |
685 ms |
83256 KB |
Output is correct |
35 |
Correct |
640 ms |
83304 KB |
Output is correct |
36 |
Correct |
591 ms |
74440 KB |
Output is correct |
37 |
Correct |
513 ms |
68000 KB |
Output is correct |
38 |
Correct |
461 ms |
59828 KB |
Output is correct |
39 |
Correct |
603 ms |
79556 KB |
Output is correct |
40 |
Correct |
485 ms |
63564 KB |
Output is correct |
41 |
Correct |
576 ms |
66228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
896 ms |
101544 KB |
Output is correct |
21 |
Correct |
1170 ms |
102240 KB |
Output is correct |
22 |
Correct |
731 ms |
78088 KB |
Output is correct |
23 |
Correct |
697 ms |
75812 KB |
Output is correct |
24 |
Correct |
716 ms |
78216 KB |
Output is correct |
25 |
Correct |
883 ms |
110024 KB |
Output is correct |
26 |
Correct |
1000 ms |
99808 KB |
Output is correct |
27 |
Correct |
1109 ms |
110320 KB |
Output is correct |
28 |
Correct |
858 ms |
86372 KB |
Output is correct |
29 |
Correct |
689 ms |
65956 KB |
Output is correct |
30 |
Correct |
1106 ms |
113236 KB |
Output is correct |
31 |
Correct |
742 ms |
77868 KB |
Output is correct |
32 |
Correct |
590 ms |
70476 KB |
Output is correct |
33 |
Correct |
462 ms |
65044 KB |
Output is correct |
34 |
Correct |
474 ms |
62372 KB |
Output is correct |
35 |
Correct |
681 ms |
109868 KB |
Output is correct |
36 |
Correct |
35 ms |
4276 KB |
Output is correct |
37 |
Correct |
277 ms |
39464 KB |
Output is correct |
38 |
Correct |
517 ms |
67876 KB |
Output is correct |
39 |
Correct |
477 ms |
59944 KB |
Output is correct |
40 |
Correct |
564 ms |
79604 KB |
Output is correct |
41 |
Correct |
464 ms |
63652 KB |
Output is correct |
42 |
Correct |
495 ms |
66212 KB |
Output is correct |
43 |
Correct |
232 ms |
34096 KB |
Output is correct |
44 |
Correct |
909 ms |
124608 KB |
Output is correct |
45 |
Correct |
966 ms |
128700 KB |
Output is correct |
46 |
Correct |
1067 ms |
136148 KB |
Output is correct |
47 |
Correct |
1387 ms |
141692 KB |
Output is correct |
48 |
Correct |
1206 ms |
134508 KB |
Output is correct |
49 |
Correct |
564 ms |
73228 KB |
Output is correct |
50 |
Correct |
611 ms |
66084 KB |
Output is correct |
51 |
Correct |
1114 ms |
130752 KB |
Output is correct |
52 |
Correct |
572 ms |
89312 KB |
Output is correct |
53 |
Correct |
28 ms |
4340 KB |
Output is correct |
54 |
Correct |
970 ms |
130156 KB |
Output is correct |
55 |
Correct |
970 ms |
135900 KB |
Output is correct |
56 |
Correct |
1278 ms |
141644 KB |
Output is correct |
57 |
Correct |
936 ms |
115024 KB |
Output is correct |
58 |
Correct |
1052 ms |
118024 KB |
Output is correct |
59 |
Correct |
1171 ms |
138184 KB |
Output is correct |
60 |
Correct |
856 ms |
115064 KB |
Output is correct |
61 |
Correct |
1035 ms |
118144 KB |
Output is correct |
62 |
Correct |
623 ms |
71020 KB |
Output is correct |
63 |
Correct |
123 ms |
11368 KB |
Output is correct |
64 |
Correct |
742 ms |
105276 KB |
Output is correct |
65 |
Correct |
783 ms |
99484 KB |
Output is correct |
66 |
Correct |
610 ms |
73276 KB |
Output is correct |
67 |
Correct |
714 ms |
92240 KB |
Output is correct |
68 |
Correct |
478 ms |
57236 KB |
Output is correct |
69 |
Correct |
1278 ms |
142408 KB |
Output is correct |
70 |
Correct |
1032 ms |
133920 KB |
Output is correct |
71 |
Correct |
1264 ms |
134432 KB |
Output is correct |
72 |
Correct |
640 ms |
72640 KB |
Output is correct |
73 |
Correct |
1330 ms |
130632 KB |
Output is correct |
74 |
Correct |
632 ms |
80188 KB |
Output is correct |
75 |
Correct |
540 ms |
74404 KB |
Output is correct |
76 |
Correct |
685 ms |
83256 KB |
Output is correct |
77 |
Correct |
640 ms |
83304 KB |
Output is correct |
78 |
Correct |
591 ms |
74440 KB |
Output is correct |
79 |
Correct |
513 ms |
68000 KB |
Output is correct |
80 |
Correct |
461 ms |
59828 KB |
Output is correct |
81 |
Correct |
603 ms |
79556 KB |
Output is correct |
82 |
Correct |
485 ms |
63564 KB |
Output is correct |
83 |
Correct |
576 ms |
66228 KB |
Output is correct |
84 |
Correct |
219 ms |
27476 KB |
Output is correct |
85 |
Correct |
1133 ms |
133608 KB |
Output is correct |
86 |
Correct |
1630 ms |
165468 KB |
Output is correct |
87 |
Correct |
122 ms |
19564 KB |
Output is correct |
88 |
Correct |
179 ms |
20076 KB |
Output is correct |
89 |
Correct |
150 ms |
19612 KB |
Output is correct |
90 |
Correct |
58 ms |
9092 KB |
Output is correct |
91 |
Correct |
2 ms |
512 KB |
Output is correct |
92 |
Correct |
52 ms |
5396 KB |
Output is correct |
93 |
Correct |
502 ms |
50696 KB |
Output is correct |
94 |
Correct |
134 ms |
11628 KB |
Output is correct |
95 |
Correct |
805 ms |
110640 KB |
Output is correct |
96 |
Correct |
747 ms |
100840 KB |
Output is correct |
97 |
Correct |
753 ms |
81548 KB |
Output is correct |
98 |
Correct |
819 ms |
92088 KB |
Output is correct |
99 |
Correct |
1926 ms |
199204 KB |
Output is correct |
100 |
Correct |
1159 ms |
135432 KB |
Output is correct |
101 |
Correct |
1426 ms |
150788 KB |
Output is correct |
102 |
Correct |
628 ms |
72640 KB |
Output is correct |
103 |
Correct |
526 ms |
75660 KB |
Output is correct |
104 |
Correct |
510 ms |
71972 KB |
Output is correct |
105 |
Correct |
595 ms |
80788 KB |
Output is correct |
106 |
Correct |
569 ms |
78732 KB |
Output is correct |
107 |
Correct |
559 ms |
74764 KB |
Output is correct |
108 |
Correct |
80 ms |
10804 KB |
Output is correct |
109 |
Correct |
1065 ms |
107524 KB |
Output is correct |
110 |
Correct |
968 ms |
134552 KB |
Output is correct |
111 |
Correct |
985 ms |
134684 KB |
Output is correct |
112 |
Correct |
675 ms |
81444 KB |
Output is correct |
113 |
Correct |
629 ms |
75944 KB |
Output is correct |