#include <bits/stdc++.h>
using namespace std;
class Graph {
private:
struct edge_t {
int v;
long long w;
edge_t() {}
edge_t(int v, long long w) : v(v), w(w) {}
bool operator < (const edge_t &o) const { return w > o.w; }
};
vector<vector<edge_t>> adj;
public:
Graph() {}
int AddVertex() {
adj.emplace_back();
return (int) adj.size() - 1;
}
void AddEdge(int u, int v, long long w) {
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
long long Dijkstra(int s, int g) {
priority_queue<edge_t> pq;
vector<long long> dist(adj.size(), -1);
pq.emplace(s, 0);
dist[s] = 0;
while (!pq.empty()) {
int u = pq.top().v;
long long d = pq.top().w;
pq.pop();
if (dist[u] != d) continue;
for (auto e : adj[u]) {
if (dist[e.v] == -1 || dist[e.v] > dist[u] + e.w) {
dist[e.v] = dist[u] + e.w;
pq.emplace(e.v, dist[e.v]);
}
}
}
return dist[g];
}
};
struct Point {
int x, y;
Point() {}
Point(int x, int y) : x(x), y(y) {}
bool operator < (const Point &o) const { return make_pair(x, y) < make_pair(o.x, o.y); }
bool operator > (const Point &o) const { return make_pair(x, y) > make_pair(o.x, o.y); }
bool operator == (const Point &o) const { return x == o.x && y == o.y; }
};
class Planar {
private:
Graph G;
map<Point, int> id;
set<pair<Point, Point>> edges;
public:
Planar() {}
void AddVertex(Point p) {
if (!id.count(p)) id.emplace(p, G.AddVertex());
}
void AddEdge(Point a, Point b) {
if (a == b) return;
if (edges.count({min(a, b), max(a, b)})) return;
AddVertex(a), AddVertex(b);
G.AddEdge(id[a], id[b], abs(a.x - b.x) + abs(a.y - b.y));
edges.emplace(min(a, b), max(a, b));
}
long long Dijkstra(Point s, Point g) {
AddVertex(s), AddVertex(g);
return G.Dijkstra(id[s], id[g]);
}
};
long long min_distance(vector<int> x, vector<int> h, vector<int> l,
vector<int> r, vector<int> y, int s, int g) {
Planar G;
auto SplitSkywalk = [&](int p) { // Split skywalks that passes over p into separate parts
struct event_t {
int t; // type 0 = skywalk, 1 = building
int id;
int h, l, r;
event_t() {}
event_t(int t, int id, int h, int l, int r) : t(t), id(id), h(h), l(l), r(r) {}
};
int n = x.size(), m = y.size();
vector<int> new_l;
vector<int> new_r;
vector<int> new_y;
vector<event_t> events;
for (int i = 0; i < m; i++) {
events.emplace_back(0, i, y[i], l[i], r[i]);
}
for (int i = 0; i < n; i++) {
events.emplace_back(1, i, h[i], i, i);
}
set<int> active; // active buildings
for (int i = 0; i < n; i++) {
active.emplace(i);
}
sort(begin(events), end(events), [&](const event_t &a, const event_t &b) {
return make_pair(a.h, a.t) < make_pair(b.h, b.t);
});
for (auto event : events) {
if (event.t == 0) { // skywalk
if (event.r < p || p < event.l) { // doesn't pass over p
new_y.emplace_back(event.h);
new_l.emplace_back(event.l);
new_r.emplace_back(event.r);
continue;
}
auto lb = active.lower_bound(p);
auto ub = active.upper_bound(p);
int on_left = (ub == begin(active) ? -1 : *prev(ub));
int on_right = (lb == end(active) ? -1 : *lb);
if (event.l <= on_left && on_right <= event.r) {
new_y.emplace_back(event.h);
new_l.emplace_back(event.l);
new_r.emplace_back(on_left);
new_y.emplace_back(event.h);
new_l.emplace_back(on_left);
new_r.emplace_back(on_right);
new_y.emplace_back(event.h);
new_l.emplace_back(on_right);
new_r.emplace_back(event.r);
}
} else { // building
active.erase(event.id);
}
}
l = new_l, r = new_r, y = new_y;
};
auto BuildGraph = [&]() { // For each endpoint of skywalks, add vertices on the skywalk above and below the current one
l.emplace_back(s);
r.emplace_back(s);
y.emplace_back(0);
l.emplace_back(g);
r.emplace_back(g);
y.emplace_back(0);
int n = x.size(), m = y.size();
vector<vector<pair<int, int>>> events(n); // (height, id)
vector<int> last_point(m); // last point added on the i-th skywalk
for (int i = 0; i < m; i++) {
events[l[i]].emplace_back(+y[i], i); // add new skywalk starting from at l[i]
events[r[i]].emplace_back(-y[i], i); // delete skywalks ending at r[i]
last_point[i] = x[l[i]];
}
set<pair<int, int>> ys; // (height, id)
ys.emplace(-1, -1);
for (int i = 0; i < n; i++) {
sort(begin(events[i]), end(events[i]), greater<pair<int, int>>());
for (auto event : events[i]) {
int y = abs(event.first), id = event.second;
auto lb = ys.lower_bound({y, -1});
auto ub = ys.upper_bound({y, m});
int above = (ub == end(ys) ? -1 : ub->first);
int id_above = (ub == end(ys) ? -1 : ub->second);
int below = (lb == begin(ys) ? -1 : prev(lb)->first);
int id_below = (lb == begin(ys) ? -1 : prev(lb)->second);
if (above != -1 && above <= h[i]) {
G.AddEdge(Point(x[i], y), Point(x[i], above));
G.AddEdge(Point(last_point[id_above], above), Point(x[i], above));
last_point[id_above] = x[i];
}
if (below != -1) {
G.AddEdge(Point(x[i], y), Point(x[i], below));
G.AddEdge(Point(last_point[id_below], below), Point(x[i], below));
last_point[id_below] = x[i];
}
if (event.first > 0) {
ys.emplace(y, id);
} else if (event.first < 0) {
G.AddEdge(Point(x[i], y), Point(last_point[id], y));
ys.erase({y, id});
}
}
}
};
SplitSkywalk(s);
SplitSkywalk(g);
BuildGraph();
return G.Dijkstra(Point(x[s], 0), Point(x[g], 0));
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
512 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
1166 ms |
122568 KB |
Output is correct |
4 |
Correct |
1101 ms |
122236 KB |
Output is correct |
5 |
Correct |
792 ms |
92236 KB |
Output is correct |
6 |
Correct |
754 ms |
84516 KB |
Output is correct |
7 |
Correct |
767 ms |
92880 KB |
Output is correct |
8 |
Correct |
1265 ms |
132804 KB |
Output is correct |
9 |
Correct |
1006 ms |
114068 KB |
Output is correct |
10 |
Correct |
1214 ms |
129964 KB |
Output is correct |
11 |
Correct |
876 ms |
93832 KB |
Output is correct |
12 |
Correct |
559 ms |
50152 KB |
Output is correct |
13 |
Correct |
1160 ms |
132544 KB |
Output is correct |
14 |
Correct |
650 ms |
59776 KB |
Output is correct |
15 |
Correct |
592 ms |
54100 KB |
Output is correct |
16 |
Correct |
482 ms |
56592 KB |
Output is correct |
17 |
Correct |
493 ms |
54676 KB |
Output is correct |
18 |
Correct |
1185 ms |
99420 KB |
Output is correct |
19 |
Correct |
27 ms |
3212 KB |
Output is correct |
20 |
Correct |
322 ms |
29480 KB |
Output is correct |
21 |
Correct |
541 ms |
52324 KB |
Output is correct |
22 |
Correct |
466 ms |
50184 KB |
Output is correct |
23 |
Correct |
920 ms |
80320 KB |
Output is correct |
24 |
Correct |
460 ms |
52900 KB |
Output is correct |
25 |
Correct |
472 ms |
54668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
167 ms |
20264 KB |
Output is correct |
2 |
Correct |
1741 ms |
160220 KB |
Output is correct |
3 |
Correct |
1898 ms |
166708 KB |
Output is correct |
4 |
Correct |
1980 ms |
171172 KB |
Output is correct |
5 |
Correct |
2653 ms |
181128 KB |
Output is correct |
6 |
Correct |
2365 ms |
163448 KB |
Output is correct |
7 |
Correct |
820 ms |
86908 KB |
Output is correct |
8 |
Correct |
480 ms |
50184 KB |
Output is correct |
9 |
Correct |
2228 ms |
155140 KB |
Output is correct |
10 |
Correct |
752 ms |
82692 KB |
Output is correct |
11 |
Correct |
50 ms |
4508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
167 ms |
20264 KB |
Output is correct |
2 |
Correct |
1741 ms |
160220 KB |
Output is correct |
3 |
Correct |
1898 ms |
166708 KB |
Output is correct |
4 |
Correct |
1980 ms |
171172 KB |
Output is correct |
5 |
Correct |
2653 ms |
181128 KB |
Output is correct |
6 |
Correct |
2365 ms |
163448 KB |
Output is correct |
7 |
Correct |
820 ms |
86908 KB |
Output is correct |
8 |
Correct |
480 ms |
50184 KB |
Output is correct |
9 |
Correct |
2228 ms |
155140 KB |
Output is correct |
10 |
Correct |
752 ms |
82692 KB |
Output is correct |
11 |
Correct |
50 ms |
4508 KB |
Output is correct |
12 |
Correct |
1919 ms |
165936 KB |
Output is correct |
13 |
Correct |
1847 ms |
170736 KB |
Output is correct |
14 |
Correct |
2683 ms |
181100 KB |
Output is correct |
15 |
Correct |
1458 ms |
126888 KB |
Output is correct |
16 |
Correct |
1606 ms |
141200 KB |
Output is correct |
17 |
Correct |
1840 ms |
171472 KB |
Output is correct |
18 |
Correct |
1437 ms |
127188 KB |
Output is correct |
19 |
Correct |
1575 ms |
140940 KB |
Output is correct |
20 |
Correct |
953 ms |
84780 KB |
Output is correct |
21 |
Correct |
168 ms |
8752 KB |
Output is correct |
22 |
Correct |
1122 ms |
128884 KB |
Output is correct |
23 |
Correct |
1074 ms |
119480 KB |
Output is correct |
24 |
Correct |
667 ms |
72636 KB |
Output is correct |
25 |
Correct |
884 ms |
105468 KB |
Output is correct |
26 |
Correct |
487 ms |
49332 KB |
Output is correct |
27 |
Correct |
2706 ms |
179564 KB |
Output is correct |
28 |
Correct |
1615 ms |
167968 KB |
Output is correct |
29 |
Correct |
2423 ms |
163252 KB |
Output is correct |
30 |
Correct |
862 ms |
86268 KB |
Output is correct |
31 |
Correct |
2371 ms |
155124 KB |
Output is correct |
32 |
Correct |
607 ms |
65520 KB |
Output is correct |
33 |
Correct |
592 ms |
64268 KB |
Output is correct |
34 |
Correct |
792 ms |
80248 KB |
Output is correct |
35 |
Correct |
781 ms |
83652 KB |
Output is correct |
36 |
Correct |
590 ms |
65348 KB |
Output is correct |
37 |
Correct |
537 ms |
52452 KB |
Output is correct |
38 |
Correct |
497 ms |
50056 KB |
Output is correct |
39 |
Correct |
917 ms |
80340 KB |
Output is correct |
40 |
Correct |
467 ms |
53032 KB |
Output is correct |
41 |
Correct |
468 ms |
54568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
512 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
256 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
1166 ms |
122568 KB |
Output is correct |
21 |
Correct |
1101 ms |
122236 KB |
Output is correct |
22 |
Correct |
792 ms |
92236 KB |
Output is correct |
23 |
Correct |
754 ms |
84516 KB |
Output is correct |
24 |
Correct |
767 ms |
92880 KB |
Output is correct |
25 |
Correct |
1265 ms |
132804 KB |
Output is correct |
26 |
Correct |
1006 ms |
114068 KB |
Output is correct |
27 |
Correct |
1214 ms |
129964 KB |
Output is correct |
28 |
Correct |
876 ms |
93832 KB |
Output is correct |
29 |
Correct |
559 ms |
50152 KB |
Output is correct |
30 |
Correct |
1160 ms |
132544 KB |
Output is correct |
31 |
Correct |
650 ms |
59776 KB |
Output is correct |
32 |
Correct |
592 ms |
54100 KB |
Output is correct |
33 |
Correct |
482 ms |
56592 KB |
Output is correct |
34 |
Correct |
493 ms |
54676 KB |
Output is correct |
35 |
Correct |
1185 ms |
99420 KB |
Output is correct |
36 |
Correct |
27 ms |
3212 KB |
Output is correct |
37 |
Correct |
322 ms |
29480 KB |
Output is correct |
38 |
Correct |
541 ms |
52324 KB |
Output is correct |
39 |
Correct |
466 ms |
50184 KB |
Output is correct |
40 |
Correct |
920 ms |
80320 KB |
Output is correct |
41 |
Correct |
460 ms |
52900 KB |
Output is correct |
42 |
Correct |
472 ms |
54668 KB |
Output is correct |
43 |
Correct |
167 ms |
20264 KB |
Output is correct |
44 |
Correct |
1741 ms |
160220 KB |
Output is correct |
45 |
Correct |
1898 ms |
166708 KB |
Output is correct |
46 |
Correct |
1980 ms |
171172 KB |
Output is correct |
47 |
Correct |
2653 ms |
181128 KB |
Output is correct |
48 |
Correct |
2365 ms |
163448 KB |
Output is correct |
49 |
Correct |
820 ms |
86908 KB |
Output is correct |
50 |
Correct |
480 ms |
50184 KB |
Output is correct |
51 |
Correct |
2228 ms |
155140 KB |
Output is correct |
52 |
Correct |
752 ms |
82692 KB |
Output is correct |
53 |
Correct |
50 ms |
4508 KB |
Output is correct |
54 |
Correct |
1919 ms |
165936 KB |
Output is correct |
55 |
Correct |
1847 ms |
170736 KB |
Output is correct |
56 |
Correct |
2683 ms |
181100 KB |
Output is correct |
57 |
Correct |
1458 ms |
126888 KB |
Output is correct |
58 |
Correct |
1606 ms |
141200 KB |
Output is correct |
59 |
Correct |
1840 ms |
171472 KB |
Output is correct |
60 |
Correct |
1437 ms |
127188 KB |
Output is correct |
61 |
Correct |
1575 ms |
140940 KB |
Output is correct |
62 |
Correct |
953 ms |
84780 KB |
Output is correct |
63 |
Correct |
168 ms |
8752 KB |
Output is correct |
64 |
Correct |
1122 ms |
128884 KB |
Output is correct |
65 |
Correct |
1074 ms |
119480 KB |
Output is correct |
66 |
Correct |
667 ms |
72636 KB |
Output is correct |
67 |
Correct |
884 ms |
105468 KB |
Output is correct |
68 |
Correct |
487 ms |
49332 KB |
Output is correct |
69 |
Correct |
2706 ms |
179564 KB |
Output is correct |
70 |
Correct |
1615 ms |
167968 KB |
Output is correct |
71 |
Correct |
2423 ms |
163252 KB |
Output is correct |
72 |
Correct |
862 ms |
86268 KB |
Output is correct |
73 |
Correct |
2371 ms |
155124 KB |
Output is correct |
74 |
Correct |
607 ms |
65520 KB |
Output is correct |
75 |
Correct |
592 ms |
64268 KB |
Output is correct |
76 |
Correct |
792 ms |
80248 KB |
Output is correct |
77 |
Correct |
781 ms |
83652 KB |
Output is correct |
78 |
Correct |
590 ms |
65348 KB |
Output is correct |
79 |
Correct |
537 ms |
52452 KB |
Output is correct |
80 |
Correct |
497 ms |
50056 KB |
Output is correct |
81 |
Correct |
917 ms |
80340 KB |
Output is correct |
82 |
Correct |
467 ms |
53032 KB |
Output is correct |
83 |
Correct |
468 ms |
54568 KB |
Output is correct |
84 |
Correct |
152 ms |
17088 KB |
Output is correct |
85 |
Correct |
1990 ms |
171208 KB |
Output is correct |
86 |
Correct |
3156 ms |
209696 KB |
Output is correct |
87 |
Correct |
179 ms |
14700 KB |
Output is correct |
88 |
Correct |
257 ms |
16252 KB |
Output is correct |
89 |
Correct |
172 ms |
14440 KB |
Output is correct |
90 |
Correct |
57 ms |
7700 KB |
Output is correct |
91 |
Correct |
7 ms |
512 KB |
Output is correct |
92 |
Correct |
57 ms |
6572 KB |
Output is correct |
93 |
Correct |
671 ms |
59356 KB |
Output is correct |
94 |
Correct |
180 ms |
9008 KB |
Output is correct |
95 |
Correct |
1205 ms |
136848 KB |
Output is correct |
96 |
Correct |
1004 ms |
116832 KB |
Output is correct |
97 |
Correct |
727 ms |
76836 KB |
Output is correct |
98 |
Correct |
869 ms |
105316 KB |
Output is correct |
99 |
Correct |
3759 ms |
251816 KB |
Output is correct |
100 |
Correct |
1779 ms |
170156 KB |
Output is correct |
101 |
Correct |
2782 ms |
186532 KB |
Output is correct |
102 |
Correct |
896 ms |
86640 KB |
Output is correct |
103 |
Correct |
607 ms |
63980 KB |
Output is correct |
104 |
Correct |
585 ms |
62652 KB |
Output is correct |
105 |
Correct |
801 ms |
76020 KB |
Output is correct |
106 |
Correct |
653 ms |
65060 KB |
Output is correct |
107 |
Correct |
748 ms |
64756 KB |
Output is correct |
108 |
Correct |
119 ms |
12880 KB |
Output is correct |
109 |
Correct |
1831 ms |
128604 KB |
Output is correct |
110 |
Correct |
1594 ms |
169012 KB |
Output is correct |
111 |
Correct |
1602 ms |
169348 KB |
Output is correct |
112 |
Correct |
723 ms |
81084 KB |
Output is correct |
113 |
Correct |
688 ms |
79356 KB |
Output is correct |