#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
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 = Y.size();
map<pair<int, int>, int> mp;
vector<pair<int, int>> p;
for (int i = 0; i < N; i++) {
mp[{X[i], 0}] = i;
p.emplace_back(X[i], 0);
}
vector<vector<pair<int, int>>> adj(N);
vector<tuple<int, int, int>> T(N + M);
for (int i = 0; i < N; i++) {
T[i] = {H[i], 1, i};
}
for (int i = 0; i < M; i++) {
T[N + i] = {Y[i], 0, i};
}
sort(T.rbegin(), T.rend());
set<int> S;
int q;
for (auto [y, t, k] : T) {
if (t == 1) {
S.insert(k);
} else {
q = -1;
auto it = S.lower_bound(L[k]);
while (it != S.end() && (*it) <= R[k]) {
int z = *it, w; it++;
if (!mp.count({X[z], y})) {
mp[{X[z], y}] = w = p.size();
p.emplace_back(X[z], y);
adj.push_back({});
} else {
w = mp[{X[z], y}];
}
if (q != -1) {
adj[q].emplace_back(w, X[z] - p[q].first);
adj[w].emplace_back(q, X[z] - p[q].first);
}
q = w;
}
}
}
q = -1;
for (auto [P, k] : mp) {
auto [x, y] = P;
if (q != -1 && p[q].first == x) {
adj[k].emplace_back(q, y - p[q].second);
adj[q].emplace_back(k, y - p[q].second);
}
q = k;
}
int K = adj.size();
vector<ll> dis(K, -1);
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
pq.emplace(0, s);
while (!pq.empty()) {
auto [d, x] = pq.top();
pq.pop();
if (dis[x] != -1) {
continue;
}
dis[x] = d;
for (auto [y, z] : adj[x]) {
pq.emplace(d + z, y);
}
}
return dis[g];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 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 |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1296 ms |
129608 KB |
Output is correct |
4 |
Correct |
1222 ms |
148744 KB |
Output is correct |
5 |
Correct |
743 ms |
129596 KB |
Output is correct |
6 |
Correct |
641 ms |
115596 KB |
Output is correct |
7 |
Correct |
678 ms |
129816 KB |
Output is correct |
8 |
Correct |
1679 ms |
165708 KB |
Output is correct |
9 |
Correct |
892 ms |
126724 KB |
Output is correct |
10 |
Correct |
1746 ms |
203208 KB |
Output is correct |
11 |
Correct |
649 ms |
73204 KB |
Output is correct |
12 |
Correct |
405 ms |
56764 KB |
Output is correct |
13 |
Correct |
1390 ms |
178900 KB |
Output is correct |
14 |
Correct |
284 ms |
54576 KB |
Output is correct |
15 |
Correct |
350 ms |
55968 KB |
Output is correct |
16 |
Correct |
337 ms |
56940 KB |
Output is correct |
17 |
Correct |
372 ms |
54472 KB |
Output is correct |
18 |
Correct |
254 ms |
58948 KB |
Output is correct |
19 |
Correct |
10 ms |
2956 KB |
Output is correct |
20 |
Correct |
94 ms |
27392 KB |
Output is correct |
21 |
Correct |
339 ms |
52312 KB |
Output is correct |
22 |
Correct |
346 ms |
55864 KB |
Output is correct |
23 |
Correct |
352 ms |
71048 KB |
Output is correct |
24 |
Correct |
319 ms |
55804 KB |
Output is correct |
25 |
Correct |
349 ms |
53696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
14404 KB |
Output is correct |
2 |
Execution timed out |
4086 ms |
639420 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
14404 KB |
Output is correct |
2 |
Execution timed out |
4086 ms |
639420 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 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 |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1296 ms |
129608 KB |
Output is correct |
21 |
Correct |
1222 ms |
148744 KB |
Output is correct |
22 |
Correct |
743 ms |
129596 KB |
Output is correct |
23 |
Correct |
641 ms |
115596 KB |
Output is correct |
24 |
Correct |
678 ms |
129816 KB |
Output is correct |
25 |
Correct |
1679 ms |
165708 KB |
Output is correct |
26 |
Correct |
892 ms |
126724 KB |
Output is correct |
27 |
Correct |
1746 ms |
203208 KB |
Output is correct |
28 |
Correct |
649 ms |
73204 KB |
Output is correct |
29 |
Correct |
405 ms |
56764 KB |
Output is correct |
30 |
Correct |
1390 ms |
178900 KB |
Output is correct |
31 |
Correct |
284 ms |
54576 KB |
Output is correct |
32 |
Correct |
350 ms |
55968 KB |
Output is correct |
33 |
Correct |
337 ms |
56940 KB |
Output is correct |
34 |
Correct |
372 ms |
54472 KB |
Output is correct |
35 |
Correct |
254 ms |
58948 KB |
Output is correct |
36 |
Correct |
10 ms |
2956 KB |
Output is correct |
37 |
Correct |
94 ms |
27392 KB |
Output is correct |
38 |
Correct |
339 ms |
52312 KB |
Output is correct |
39 |
Correct |
346 ms |
55864 KB |
Output is correct |
40 |
Correct |
352 ms |
71048 KB |
Output is correct |
41 |
Correct |
319 ms |
55804 KB |
Output is correct |
42 |
Correct |
349 ms |
53696 KB |
Output is correct |
43 |
Correct |
91 ms |
14404 KB |
Output is correct |
44 |
Execution timed out |
4086 ms |
639420 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |