#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
//#define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define dbv(v) cerr << #v << "="; for (auto _x : v) cerr << _x << ", "; cerr << endl
#define dba(a, n) cerr << #a << "="; for (int _i = 0; _i < (n); ++_i) cerr << a[_i] << ", "; cerr << endl
template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& x) {
return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
ll 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<vector<int>> vert(n), hor(m);
vector<vector<pair<int, int>>> events(n);
for (int i = 0; i < m; ++i) {
events[l[i]].emplace_back(i, 1);
events[r[i]].emplace_back(i, -1);
}
set<pair<int, int>> S;
for (int i = 0; i < n; ++i) {
for (auto& e : events[i]) {
int j = e.first;
vert[i].push_back(y[j]);
hor[j].push_back(x[i]);
if (e.second == 1) S.insert({y[j], j});
}
vector<int> nvert;
for (int yj : vert[i]) {
auto it = S.lower_bound({yj, -1});
if (it == S.begin()) continue;
int k = prev(it)->second;
hor[k].push_back(x[i]);
nvert.push_back(y[k]);
}
vert[i].insert(vert[i].end(), nvert.begin(), nvert.end());
if (i == s || i == g) vert[i].push_back(0);
for (auto& e : events[i]) {
int j = e.first;
if (e.second == -1) S.erase({y[j], j});
}
}
auto f = [&](int q) {
vector<pair<int, int>> lhs = {{h[q], q}}, rhs = {{h[q], q}};
for (int i = q - 1; i >= 0; --i) {
if (h[i] > lhs.back().first) lhs.push_back({h[i], i});
}
for (int i = q + 1; i < n; ++i) {
if (h[i] > rhs.back().first) rhs.push_back({h[i], i});
}
for (int i = 0; i < m; ++i) {
if (r[i] < q || l[i] > q) continue;
auto it = lower_bound(lhs.begin(), lhs.end(), make_pair(y[i], -1));
if (it == lhs.end()) continue;
int p = it->second;
if (p < l[i]) continue;
vert[p].push_back(y[i]);
hor[i].push_back(x[p]);
}
for (int i = 0; i < m; ++i) {
if (r[i] < q || l[i] > q) continue;
auto it = lower_bound(rhs.begin(), rhs.end(), make_pair(y[i], -1));
if (it == rhs.end()) continue;
int p = it->second;
if (p > r[i]) continue;
vert[p].push_back(y[i]);
hor[i].push_back(x[p]);
}
};
f(s);
f(g);
map<pair<int, int>, int> mp;
auto getId = [&](pair<int, int> a) {
auto it = mp.find(a);
if (it != mp.end()) return it->second;
int s = mp.size();
return mp[a] = s;
};
for (int i = 0; i < n; ++i)
for (int j : vert[i]) getId({x[i], j});
for (int i = 0; i < m; ++i)
for (int j : hor[i]) getId({j, y[i]});
vector<vector<pair<int, int>>> E(mp.size());
for (int i = 0; i < n; ++i) {
sort(vert[i].begin(), vert[i].end());
vert[i].resize(unique(vert[i].begin(), vert[i].end()) - vert[i].begin());
for (int j = 0; j + 1 < vert[i].size(); ++j) {
int u = getId({x[i], vert[i][j]});
int v = getId({x[i], vert[i][j + 1]});
int d = vert[i][j + 1] - vert[i][j];
E[u].push_back({v, d});
E[v].push_back({u, d});
}
}
for (int i = 0; i < m; ++i) {
sort(hor[i].begin(), hor[i].end());
hor[i].resize(unique(hor[i].begin(), hor[i].end()) - hor[i].begin());
for (int j = 0; j + 1 < hor[i].size(); ++j) {
int u = getId({hor[i][j], y[i]});
int v = getId({hor[i][j + 1], y[i]});
int d = hor[i][j + 1] - hor[i][j];
E[u].push_back({v, d});
E[v].push_back({u, d});
}
}
const ll INF = 1LL << 60;
vector<ll> dist(mp.size(), INF);
int src = getId({x[s], 0}), dest = getId({x[g], 0});
typedef pair<ll, int> Pair;
priority_queue<Pair, vector<Pair>, greater<Pair>> pq;
dist[src] = 0;
pq.emplace(dist[src], src);
while (!pq.empty()) {
int u = pq.top().second;
ll cdist = pq.top().first;
pq.pop();
if (cdist > dist[u]) continue;
for (auto e : E[u]) {
int v = e.first;
ll val = dist[u] + e.second;
if (val < dist[v]) {
dist[v] = val;
pq.emplace(val, v);
}
}
}
return dist[dest] == INF ? -1 : dist[dest];
}
Compilation message
walk.cpp: In function 'll min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:92:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j + 1 < vert[i].size(); ++j) {
~~~~~~^~~~~~~~~~~~~~~~
walk.cpp:103:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j + 1 < hor[i].size(); ++j) {
~~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
292 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
256 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
1123 ms |
64892 KB |
Output is correct |
4 |
Correct |
992 ms |
72392 KB |
Output is correct |
5 |
Correct |
768 ms |
56304 KB |
Output is correct |
6 |
Correct |
673 ms |
54336 KB |
Output is correct |
7 |
Correct |
693 ms |
56540 KB |
Output is correct |
8 |
Correct |
1210 ms |
67492 KB |
Output is correct |
9 |
Correct |
920 ms |
67536 KB |
Output is correct |
10 |
Correct |
1128 ms |
72772 KB |
Output is correct |
11 |
Correct |
880 ms |
57548 KB |
Output is correct |
12 |
Correct |
664 ms |
47388 KB |
Output is correct |
13 |
Correct |
1055 ms |
73712 KB |
Output is correct |
14 |
Correct |
850 ms |
47560 KB |
Output is correct |
15 |
Correct |
940 ms |
48096 KB |
Output is correct |
16 |
Correct |
815 ms |
50168 KB |
Output is correct |
17 |
Correct |
603 ms |
47892 KB |
Output is correct |
18 |
Correct |
1278 ms |
57468 KB |
Output is correct |
19 |
Correct |
21 ms |
2808 KB |
Output is correct |
20 |
Correct |
273 ms |
25208 KB |
Output is correct |
21 |
Correct |
569 ms |
46784 KB |
Output is correct |
22 |
Correct |
646 ms |
46992 KB |
Output is correct |
23 |
Correct |
1145 ms |
59260 KB |
Output is correct |
24 |
Correct |
625 ms |
48504 KB |
Output is correct |
25 |
Correct |
556 ms |
47708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
234 ms |
16376 KB |
Output is correct |
2 |
Correct |
1489 ms |
72912 KB |
Output is correct |
3 |
Correct |
1536 ms |
75016 KB |
Output is correct |
4 |
Correct |
1542 ms |
82860 KB |
Output is correct |
5 |
Correct |
1720 ms |
84128 KB |
Output is correct |
6 |
Correct |
1682 ms |
80536 KB |
Output is correct |
7 |
Correct |
631 ms |
47428 KB |
Output is correct |
8 |
Correct |
660 ms |
48504 KB |
Output is correct |
9 |
Correct |
1718 ms |
79240 KB |
Output is correct |
10 |
Correct |
862 ms |
52124 KB |
Output is correct |
11 |
Correct |
18 ms |
4216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
234 ms |
16376 KB |
Output is correct |
2 |
Correct |
1489 ms |
72912 KB |
Output is correct |
3 |
Correct |
1536 ms |
75016 KB |
Output is correct |
4 |
Correct |
1542 ms |
82860 KB |
Output is correct |
5 |
Correct |
1720 ms |
84128 KB |
Output is correct |
6 |
Correct |
1682 ms |
80536 KB |
Output is correct |
7 |
Correct |
631 ms |
47428 KB |
Output is correct |
8 |
Correct |
660 ms |
48504 KB |
Output is correct |
9 |
Correct |
1718 ms |
79240 KB |
Output is correct |
10 |
Correct |
862 ms |
52124 KB |
Output is correct |
11 |
Correct |
18 ms |
4216 KB |
Output is correct |
12 |
Correct |
1645 ms |
75084 KB |
Output is correct |
13 |
Correct |
1396 ms |
82648 KB |
Output is correct |
14 |
Correct |
1731 ms |
84340 KB |
Output is correct |
15 |
Correct |
1098 ms |
72432 KB |
Output is correct |
16 |
Correct |
1181 ms |
72824 KB |
Output is correct |
17 |
Correct |
1245 ms |
82788 KB |
Output is correct |
18 |
Correct |
1104 ms |
72448 KB |
Output is correct |
19 |
Correct |
1128 ms |
72036 KB |
Output is correct |
20 |
Correct |
717 ms |
45680 KB |
Output is correct |
21 |
Correct |
49 ms |
8952 KB |
Output is correct |
22 |
Correct |
1004 ms |
67216 KB |
Output is correct |
23 |
Correct |
891 ms |
65360 KB |
Output is correct |
24 |
Correct |
651 ms |
52856 KB |
Output is correct |
25 |
Correct |
781 ms |
60612 KB |
Output is correct |
26 |
Correct |
588 ms |
46460 KB |
Output is correct |
27 |
Correct |
1800 ms |
85320 KB |
Output is correct |
28 |
Correct |
1430 ms |
82724 KB |
Output is correct |
29 |
Correct |
1798 ms |
81836 KB |
Output is correct |
30 |
Correct |
642 ms |
48388 KB |
Output is correct |
31 |
Correct |
1646 ms |
80064 KB |
Output is correct |
32 |
Correct |
741 ms |
50652 KB |
Output is correct |
33 |
Correct |
723 ms |
50652 KB |
Output is correct |
34 |
Correct |
943 ms |
56216 KB |
Output is correct |
35 |
Correct |
816 ms |
57640 KB |
Output is correct |
36 |
Correct |
634 ms |
52268 KB |
Output is correct |
37 |
Correct |
668 ms |
48448 KB |
Output is correct |
38 |
Correct |
732 ms |
49016 KB |
Output is correct |
39 |
Correct |
1150 ms |
60760 KB |
Output is correct |
40 |
Correct |
626 ms |
49968 KB |
Output is correct |
41 |
Correct |
565 ms |
49272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
292 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
256 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
256 KB |
Output is correct |
20 |
Correct |
1123 ms |
64892 KB |
Output is correct |
21 |
Correct |
992 ms |
72392 KB |
Output is correct |
22 |
Correct |
768 ms |
56304 KB |
Output is correct |
23 |
Correct |
673 ms |
54336 KB |
Output is correct |
24 |
Correct |
693 ms |
56540 KB |
Output is correct |
25 |
Correct |
1210 ms |
67492 KB |
Output is correct |
26 |
Correct |
920 ms |
67536 KB |
Output is correct |
27 |
Correct |
1128 ms |
72772 KB |
Output is correct |
28 |
Correct |
880 ms |
57548 KB |
Output is correct |
29 |
Correct |
664 ms |
47388 KB |
Output is correct |
30 |
Correct |
1055 ms |
73712 KB |
Output is correct |
31 |
Correct |
850 ms |
47560 KB |
Output is correct |
32 |
Correct |
940 ms |
48096 KB |
Output is correct |
33 |
Correct |
815 ms |
50168 KB |
Output is correct |
34 |
Correct |
603 ms |
47892 KB |
Output is correct |
35 |
Correct |
1278 ms |
57468 KB |
Output is correct |
36 |
Correct |
21 ms |
2808 KB |
Output is correct |
37 |
Correct |
273 ms |
25208 KB |
Output is correct |
38 |
Correct |
569 ms |
46784 KB |
Output is correct |
39 |
Correct |
646 ms |
46992 KB |
Output is correct |
40 |
Correct |
1145 ms |
59260 KB |
Output is correct |
41 |
Correct |
625 ms |
48504 KB |
Output is correct |
42 |
Correct |
556 ms |
47708 KB |
Output is correct |
43 |
Correct |
234 ms |
16376 KB |
Output is correct |
44 |
Correct |
1489 ms |
72912 KB |
Output is correct |
45 |
Correct |
1536 ms |
75016 KB |
Output is correct |
46 |
Correct |
1542 ms |
82860 KB |
Output is correct |
47 |
Correct |
1720 ms |
84128 KB |
Output is correct |
48 |
Correct |
1682 ms |
80536 KB |
Output is correct |
49 |
Correct |
631 ms |
47428 KB |
Output is correct |
50 |
Correct |
660 ms |
48504 KB |
Output is correct |
51 |
Correct |
1718 ms |
79240 KB |
Output is correct |
52 |
Correct |
862 ms |
52124 KB |
Output is correct |
53 |
Correct |
18 ms |
4216 KB |
Output is correct |
54 |
Correct |
1645 ms |
75084 KB |
Output is correct |
55 |
Correct |
1396 ms |
82648 KB |
Output is correct |
56 |
Correct |
1731 ms |
84340 KB |
Output is correct |
57 |
Correct |
1098 ms |
72432 KB |
Output is correct |
58 |
Correct |
1181 ms |
72824 KB |
Output is correct |
59 |
Correct |
1245 ms |
82788 KB |
Output is correct |
60 |
Correct |
1104 ms |
72448 KB |
Output is correct |
61 |
Correct |
1128 ms |
72036 KB |
Output is correct |
62 |
Correct |
717 ms |
45680 KB |
Output is correct |
63 |
Correct |
49 ms |
8952 KB |
Output is correct |
64 |
Correct |
1004 ms |
67216 KB |
Output is correct |
65 |
Correct |
891 ms |
65360 KB |
Output is correct |
66 |
Correct |
651 ms |
52856 KB |
Output is correct |
67 |
Correct |
781 ms |
60612 KB |
Output is correct |
68 |
Correct |
588 ms |
46460 KB |
Output is correct |
69 |
Correct |
1800 ms |
85320 KB |
Output is correct |
70 |
Correct |
1430 ms |
82724 KB |
Output is correct |
71 |
Correct |
1798 ms |
81836 KB |
Output is correct |
72 |
Correct |
642 ms |
48388 KB |
Output is correct |
73 |
Correct |
1646 ms |
80064 KB |
Output is correct |
74 |
Correct |
741 ms |
50652 KB |
Output is correct |
75 |
Correct |
723 ms |
50652 KB |
Output is correct |
76 |
Correct |
943 ms |
56216 KB |
Output is correct |
77 |
Correct |
816 ms |
57640 KB |
Output is correct |
78 |
Correct |
634 ms |
52268 KB |
Output is correct |
79 |
Correct |
668 ms |
48448 KB |
Output is correct |
80 |
Correct |
732 ms |
49016 KB |
Output is correct |
81 |
Correct |
1150 ms |
60760 KB |
Output is correct |
82 |
Correct |
626 ms |
49968 KB |
Output is correct |
83 |
Correct |
565 ms |
49272 KB |
Output is correct |
84 |
Correct |
161 ms |
13148 KB |
Output is correct |
85 |
Correct |
1592 ms |
77804 KB |
Output is correct |
86 |
Correct |
2230 ms |
98280 KB |
Output is correct |
87 |
Correct |
92 ms |
14840 KB |
Output is correct |
88 |
Correct |
95 ms |
14580 KB |
Output is correct |
89 |
Correct |
90 ms |
14712 KB |
Output is correct |
90 |
Correct |
43 ms |
5112 KB |
Output is correct |
91 |
Correct |
4 ms |
496 KB |
Output is correct |
92 |
Correct |
34 ms |
3836 KB |
Output is correct |
93 |
Correct |
450 ms |
34560 KB |
Output is correct |
94 |
Correct |
50 ms |
9464 KB |
Output is correct |
95 |
Correct |
1055 ms |
70756 KB |
Output is correct |
96 |
Correct |
889 ms |
66164 KB |
Output is correct |
97 |
Correct |
737 ms |
54048 KB |
Output is correct |
98 |
Correct |
783 ms |
61048 KB |
Output is correct |
99 |
Correct |
2914 ms |
116868 KB |
Output is correct |
100 |
Correct |
1610 ms |
82844 KB |
Output is correct |
101 |
Correct |
2063 ms |
90976 KB |
Output is correct |
102 |
Correct |
677 ms |
47668 KB |
Output is correct |
103 |
Correct |
694 ms |
49784 KB |
Output is correct |
104 |
Correct |
720 ms |
49784 KB |
Output is correct |
105 |
Correct |
953 ms |
55264 KB |
Output is correct |
106 |
Correct |
793 ms |
50200 KB |
Output is correct |
107 |
Correct |
915 ms |
54504 KB |
Output is correct |
108 |
Correct |
68 ms |
6904 KB |
Output is correct |
109 |
Correct |
1213 ms |
66560 KB |
Output is correct |
110 |
Correct |
1355 ms |
81836 KB |
Output is correct |
111 |
Correct |
1326 ms |
81536 KB |
Output is correct |
112 |
Correct |
836 ms |
56156 KB |
Output is correct |
113 |
Correct |
703 ms |
54156 KB |
Output is correct |