#include "walk.h"
/*************************************
* author: marvinthang *
* created: 28.06.2023 15:43:09 *
*************************************/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define REPD(i, n) for (int i = (n); i--; )
#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; )
#define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
#include "debug.h"
#else
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define DB(...) 23
#define db(...) 23
#define debug(...) 23
#endif
template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
// end of template
const long long INF = 1e18;
long long 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();
vector <vector <int>> startAt(n), endAt(n);
for (int x: {s, g}) {
vector <pair <int, int>> f;
REP(i, n) f.emplace_back(h[i], i);
vector <int> nl, nr, ny;
REP(i, l.size()) {
if (l[i] <= x && x <= r[i]) f.emplace_back(y[i], -1 - i);
else {
nl.push_back(l[i]);
nr.push_back(r[i]);
ny.push_back(y[i]);
}
}
sort(f.rbegin(), f.rend());
set <int> s;
for (auto [_, i]: f) {
if (i >= 0) s.insert(i);
else {
i = -1 - i;
auto it = s.lower_bound(x);
int b = *it;
int a = b == x ? x : *prev(it);
if (a != l[i]) {
nl.push_back(l[i]);
nr.push_back(a);
ny.push_back(y[i]);
}
if (a != b) {
nl.push_back(a);
nr.push_back(b);
ny.push_back(y[i]);
}
if (b != r[i]) {
nl.push_back(b);
nr.push_back(r[i]);
ny.push_back(y[i]);
}
}
}
l = move(nl);
r = move(nr);
y = move(ny);
}
REP(i, l.size()) {
startAt[l[i]].push_back(i);
endAt[r[i]].push_back(i);
}
map <int, int> heights;
vector <vector <int>> yy(n);
vector <vector <pair <int, int>>> adj;
vector <int> pref(n);
auto add_edge = [&] (int u, int v, int w) {
adj[u].emplace_back(w, v);
adj[v].emplace_back(w, u);
};
REP(i, n) {
if (i) pref[i] = pref[i - 1] + yy[i - 1].size();
if (i == s || i == g) {
yy[i].push_back(0);
for (int j: startAt[i]) yy[i].push_back(y[j]);
for (int j: endAt[i]) yy[i].push_back(y[j]);
for (auto it: heights) if (it.fi <= h[i]) yy[i].push_back(it.fi);
sort(ALL(yy[i]));
yy[i].erase(unique(ALL(yy[i])), yy[i].end());
adj.resize(pref[i] + yy[i].size());
REP(j, (int) yy[i].size() - 1) add_edge(pref[i] + j, pref[i] + j + 1, yy[i][j + 1] - yy[i][j]);
} else {
for (int j: startAt[i]) {
yy[i].push_back(y[j]);
auto it = heights.lower_bound(y[j]);
if (it != heights.begin()) yy[i].push_back(prev(it)->fi);
}
for (int j: endAt[i]) {
yy[i].push_back(y[j]);
auto it = heights.lower_bound(y[j]);
if (it != heights.begin()) yy[i].push_back(prev(it)->fi);
}
sort(ALL(yy[i]));
yy[i].erase(unique(ALL(yy[i])), yy[i].end());
adj.resize(pref[i] + yy[i].size());
for (int j: startAt[i]) {
int p = lower_bound(ALL(yy[i]), y[j]) - yy[i].begin();
if (p) add_edge(pref[i] + p - 1, pref[i] + p, yy[i][p] - yy[i][p - 1]);
}
for (int j: endAt[i]) {
int p = lower_bound(ALL(yy[i]), y[j]) - yy[i].begin();
if (p) add_edge(pref[i] + p - 1, pref[i] + p, yy[i][p] - yy[i][p - 1]);
}
}
REP(j, yy[i].size()) if (heights.count(yy[i][j])) {
int k = heights[yy[i][j]];
int p = lower_bound(ALL(yy[k]), yy[i][j]) - yy[k].begin();
add_edge(pref[k] + p, pref[i] + j, x[i] - x[k]);
heights[yy[i][j]] = i;
}
for (int j: endAt[i]) heights.erase(y[j]);
for (int j: startAt[i]) heights[y[j]] = i;
}
priority_queue <pair <long long, int>, vector <pair <long long, int>>, greater <pair <long long, int>>> pq;
vector <long long> dist(adj.size(), INF);
pq.emplace(dist[pref[s]] = 0, pref[s]);
while (!pq.empty()) {
auto [du, u] = pq.top(); pq.pop();
if (du != dist[u]) continue;
for (auto [w, v]: adj[u]) if (minimize(dist[v], du + w)) pq.emplace(dist[v], v);
}
return dist[pref[g]] < INF ? dist[pref[g]] : -1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
296 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
227 ms |
31228 KB |
Output is correct |
4 |
Correct |
263 ms |
44468 KB |
Output is correct |
5 |
Correct |
172 ms |
34908 KB |
Output is correct |
6 |
Correct |
187 ms |
33260 KB |
Output is correct |
7 |
Correct |
171 ms |
38496 KB |
Output is correct |
8 |
Correct |
229 ms |
32192 KB |
Output is correct |
9 |
Correct |
212 ms |
42160 KB |
Output is correct |
10 |
Correct |
281 ms |
47076 KB |
Output is correct |
11 |
Correct |
188 ms |
33780 KB |
Output is correct |
12 |
Correct |
166 ms |
37916 KB |
Output is correct |
13 |
Correct |
262 ms |
50136 KB |
Output is correct |
14 |
Correct |
166 ms |
33896 KB |
Output is correct |
15 |
Correct |
173 ms |
35320 KB |
Output is correct |
16 |
Correct |
154 ms |
35832 KB |
Output is correct |
17 |
Correct |
170 ms |
35112 KB |
Output is correct |
18 |
Correct |
191 ms |
36708 KB |
Output is correct |
19 |
Correct |
7 ms |
2184 KB |
Output is correct |
20 |
Correct |
85 ms |
17008 KB |
Output is correct |
21 |
Correct |
120 ms |
33128 KB |
Output is correct |
22 |
Correct |
151 ms |
36676 KB |
Output is correct |
23 |
Correct |
242 ms |
43620 KB |
Output is correct |
24 |
Correct |
144 ms |
36224 KB |
Output is correct |
25 |
Correct |
135 ms |
34328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
9980 KB |
Output is correct |
2 |
Correct |
314 ms |
36760 KB |
Output is correct |
3 |
Correct |
332 ms |
36028 KB |
Output is correct |
4 |
Correct |
432 ms |
47676 KB |
Output is correct |
5 |
Correct |
538 ms |
48780 KB |
Output is correct |
6 |
Correct |
538 ms |
49076 KB |
Output is correct |
7 |
Correct |
206 ms |
30312 KB |
Output is correct |
8 |
Correct |
138 ms |
33872 KB |
Output is correct |
9 |
Correct |
465 ms |
50252 KB |
Output is correct |
10 |
Correct |
204 ms |
33516 KB |
Output is correct |
11 |
Correct |
24 ms |
6516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
9980 KB |
Output is correct |
2 |
Correct |
314 ms |
36760 KB |
Output is correct |
3 |
Correct |
332 ms |
36028 KB |
Output is correct |
4 |
Correct |
432 ms |
47676 KB |
Output is correct |
5 |
Correct |
538 ms |
48780 KB |
Output is correct |
6 |
Correct |
538 ms |
49076 KB |
Output is correct |
7 |
Correct |
206 ms |
30312 KB |
Output is correct |
8 |
Correct |
138 ms |
33872 KB |
Output is correct |
9 |
Correct |
465 ms |
50252 KB |
Output is correct |
10 |
Correct |
204 ms |
33516 KB |
Output is correct |
11 |
Correct |
24 ms |
6516 KB |
Output is correct |
12 |
Correct |
335 ms |
35660 KB |
Output is correct |
13 |
Correct |
361 ms |
47348 KB |
Output is correct |
14 |
Correct |
546 ms |
48728 KB |
Output is correct |
15 |
Correct |
295 ms |
41560 KB |
Output is correct |
16 |
Correct |
325 ms |
42740 KB |
Output is correct |
17 |
Correct |
353 ms |
46804 KB |
Output is correct |
18 |
Correct |
304 ms |
41612 KB |
Output is correct |
19 |
Correct |
333 ms |
43868 KB |
Output is correct |
20 |
Correct |
265 ms |
29884 KB |
Output is correct |
21 |
Correct |
82 ms |
12264 KB |
Output is correct |
22 |
Correct |
235 ms |
39420 KB |
Output is correct |
23 |
Correct |
213 ms |
39476 KB |
Output is correct |
24 |
Correct |
175 ms |
32996 KB |
Output is correct |
25 |
Correct |
194 ms |
39236 KB |
Output is correct |
26 |
Correct |
151 ms |
30168 KB |
Output is correct |
27 |
Correct |
559 ms |
52512 KB |
Output is correct |
28 |
Correct |
306 ms |
47044 KB |
Output is correct |
29 |
Correct |
565 ms |
49048 KB |
Output is correct |
30 |
Correct |
211 ms |
30156 KB |
Output is correct |
31 |
Correct |
513 ms |
49964 KB |
Output is correct |
32 |
Correct |
164 ms |
31832 KB |
Output is correct |
33 |
Correct |
171 ms |
33500 KB |
Output is correct |
34 |
Correct |
218 ms |
38436 KB |
Output is correct |
35 |
Correct |
243 ms |
35936 KB |
Output is correct |
36 |
Correct |
160 ms |
33628 KB |
Output is correct |
37 |
Correct |
119 ms |
30352 KB |
Output is correct |
38 |
Correct |
146 ms |
33492 KB |
Output is correct |
39 |
Correct |
246 ms |
40644 KB |
Output is correct |
40 |
Correct |
140 ms |
33044 KB |
Output is correct |
41 |
Correct |
140 ms |
31664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
296 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
292 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
227 ms |
31228 KB |
Output is correct |
21 |
Correct |
263 ms |
44468 KB |
Output is correct |
22 |
Correct |
172 ms |
34908 KB |
Output is correct |
23 |
Correct |
187 ms |
33260 KB |
Output is correct |
24 |
Correct |
171 ms |
38496 KB |
Output is correct |
25 |
Correct |
229 ms |
32192 KB |
Output is correct |
26 |
Correct |
212 ms |
42160 KB |
Output is correct |
27 |
Correct |
281 ms |
47076 KB |
Output is correct |
28 |
Correct |
188 ms |
33780 KB |
Output is correct |
29 |
Correct |
166 ms |
37916 KB |
Output is correct |
30 |
Correct |
262 ms |
50136 KB |
Output is correct |
31 |
Correct |
166 ms |
33896 KB |
Output is correct |
32 |
Correct |
173 ms |
35320 KB |
Output is correct |
33 |
Correct |
154 ms |
35832 KB |
Output is correct |
34 |
Correct |
170 ms |
35112 KB |
Output is correct |
35 |
Correct |
191 ms |
36708 KB |
Output is correct |
36 |
Correct |
7 ms |
2184 KB |
Output is correct |
37 |
Correct |
85 ms |
17008 KB |
Output is correct |
38 |
Correct |
120 ms |
33128 KB |
Output is correct |
39 |
Correct |
151 ms |
36676 KB |
Output is correct |
40 |
Correct |
242 ms |
43620 KB |
Output is correct |
41 |
Correct |
144 ms |
36224 KB |
Output is correct |
42 |
Correct |
135 ms |
34328 KB |
Output is correct |
43 |
Correct |
44 ms |
9980 KB |
Output is correct |
44 |
Correct |
314 ms |
36760 KB |
Output is correct |
45 |
Correct |
332 ms |
36028 KB |
Output is correct |
46 |
Correct |
432 ms |
47676 KB |
Output is correct |
47 |
Correct |
538 ms |
48780 KB |
Output is correct |
48 |
Correct |
538 ms |
49076 KB |
Output is correct |
49 |
Correct |
206 ms |
30312 KB |
Output is correct |
50 |
Correct |
138 ms |
33872 KB |
Output is correct |
51 |
Correct |
465 ms |
50252 KB |
Output is correct |
52 |
Correct |
204 ms |
33516 KB |
Output is correct |
53 |
Correct |
24 ms |
6516 KB |
Output is correct |
54 |
Correct |
335 ms |
35660 KB |
Output is correct |
55 |
Correct |
361 ms |
47348 KB |
Output is correct |
56 |
Correct |
546 ms |
48728 KB |
Output is correct |
57 |
Correct |
295 ms |
41560 KB |
Output is correct |
58 |
Correct |
325 ms |
42740 KB |
Output is correct |
59 |
Correct |
353 ms |
46804 KB |
Output is correct |
60 |
Correct |
304 ms |
41612 KB |
Output is correct |
61 |
Correct |
333 ms |
43868 KB |
Output is correct |
62 |
Correct |
265 ms |
29884 KB |
Output is correct |
63 |
Correct |
82 ms |
12264 KB |
Output is correct |
64 |
Correct |
235 ms |
39420 KB |
Output is correct |
65 |
Correct |
213 ms |
39476 KB |
Output is correct |
66 |
Correct |
175 ms |
32996 KB |
Output is correct |
67 |
Correct |
194 ms |
39236 KB |
Output is correct |
68 |
Correct |
151 ms |
30168 KB |
Output is correct |
69 |
Correct |
559 ms |
52512 KB |
Output is correct |
70 |
Correct |
306 ms |
47044 KB |
Output is correct |
71 |
Correct |
565 ms |
49048 KB |
Output is correct |
72 |
Correct |
211 ms |
30156 KB |
Output is correct |
73 |
Correct |
513 ms |
49964 KB |
Output is correct |
74 |
Correct |
164 ms |
31832 KB |
Output is correct |
75 |
Correct |
171 ms |
33500 KB |
Output is correct |
76 |
Correct |
218 ms |
38436 KB |
Output is correct |
77 |
Correct |
243 ms |
35936 KB |
Output is correct |
78 |
Correct |
160 ms |
33628 KB |
Output is correct |
79 |
Correct |
119 ms |
30352 KB |
Output is correct |
80 |
Correct |
146 ms |
33492 KB |
Output is correct |
81 |
Correct |
246 ms |
40644 KB |
Output is correct |
82 |
Correct |
140 ms |
33044 KB |
Output is correct |
83 |
Correct |
140 ms |
31664 KB |
Output is correct |
84 |
Correct |
39 ms |
8288 KB |
Output is correct |
85 |
Correct |
344 ms |
39320 KB |
Output is correct |
86 |
Correct |
633 ms |
62968 KB |
Output is correct |
87 |
Correct |
81 ms |
15804 KB |
Output is correct |
88 |
Correct |
99 ms |
17232 KB |
Output is correct |
89 |
Correct |
69 ms |
15740 KB |
Output is correct |
90 |
Correct |
14 ms |
3824 KB |
Output is correct |
91 |
Correct |
1 ms |
468 KB |
Output is correct |
92 |
Correct |
14 ms |
2900 KB |
Output is correct |
93 |
Correct |
177 ms |
26508 KB |
Output is correct |
94 |
Correct |
80 ms |
16596 KB |
Output is correct |
95 |
Correct |
255 ms |
44484 KB |
Output is correct |
96 |
Correct |
214 ms |
43320 KB |
Output is correct |
97 |
Correct |
180 ms |
36816 KB |
Output is correct |
98 |
Correct |
199 ms |
42976 KB |
Output is correct |
99 |
Correct |
807 ms |
80720 KB |
Output is correct |
100 |
Correct |
448 ms |
51420 KB |
Output is correct |
101 |
Correct |
606 ms |
60628 KB |
Output is correct |
102 |
Correct |
230 ms |
33248 KB |
Output is correct |
103 |
Correct |
166 ms |
34860 KB |
Output is correct |
104 |
Correct |
186 ms |
36524 KB |
Output is correct |
105 |
Correct |
214 ms |
38804 KB |
Output is correct |
106 |
Correct |
198 ms |
35760 KB |
Output is correct |
107 |
Correct |
219 ms |
38564 KB |
Output is correct |
108 |
Correct |
25 ms |
4508 KB |
Output is correct |
109 |
Correct |
427 ms |
44300 KB |
Output is correct |
110 |
Correct |
339 ms |
49960 KB |
Output is correct |
111 |
Correct |
295 ms |
48928 KB |
Output is correct |
112 |
Correct |
200 ms |
38264 KB |
Output is correct |
113 |
Correct |
193 ms |
41120 KB |
Output is correct |