#include <bits/stdc++.h>
#include "walk.h"
using namespace std;
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(); // = h.size()
int m = l.size(); // = r.size() = y.size()
// s and g can be anything, but n and m are small anyway so it doesn't matter.
// or I suppose also with small numbers of intersections.
vector<pair<int, int>> sorted;
for (int i = 0; i < m; i++) {
sorted.push_back({y[i], i});
}
sort(sorted.rbegin(), sorted.rend());
int maxi = -1;
map<pair<int, int>, vector<array<int, 3>>> adjList;
vector<vector<int>> pts(n);
set<int> tall;
int ptr1 = 0;
vector<pair<int, int>> buildings;
for (int i = 0; i < n; i++) {
buildings.push_back({h[i], i});
}
sort(buildings.rbegin(), buildings.rend());
for (int k = 0; k < m; k++) {
int yc = sorted[k].first;
int i = sorted[k].second;
while (ptr1 < n && buildings[ptr1].first >= yc) {
tall.insert(buildings[ptr1].second);
ptr1++;
}
vector<int> enough;
for (auto it = tall.lower_bound(l[i]); it != tall.end(); it++) {
if ((*it) > r[i]) break;
enough.push_back(*it);
pts[*it].push_back(y[i]);
}
int t = enough.size();
maxi = max(maxi, t);
if (maxi > 50) break;
for (int j = 0; j < t - 1; j++) {
int x1 = enough[j];
int x2 = enough[j + 1];
adjList[{x1, y[i]}].push_back({x2, y[i], x[x2] - x[x1]});
adjList[{x2, y[i]}].push_back({x1, y[i], x[x2] - x[x1]});
}
}
if (maxi <= 50) {
for (int i = 0; i < n; i++) {
pts[i].push_back(0);
sort(pts[i].begin(), pts[i].end());
int t = pts[i].size();
for (int j = 0; j < t - 1; j++) {
int y1 = pts[i][j];
int y2 = pts[i][j + 1];
adjList[{i, y1}].push_back({i, y2, y2 - y1});
adjList[{i, y2}].push_back({i, y1, y2 - y1});
}
}
priority_queue<pair<long long, pair<int, int>>, vector<pair<long long, pair<int, int>>>, greater<pair<long long, pair<int, int>>>> pq;
map<pair<int, int>, long long> dist;
set<pair<int, int>> visited;
pq.push({0ll, {s, 0}});
while (!pq.empty()) {
auto top = pq.top();
pq.pop();
if (visited.find(top.second) != visited.end()) continue;
visited.insert(top.second);
dist[top.second] = top.first;
for (auto con: adjList[top.second]) {
if (visited.find({con[0], con[1]}) == visited.end()) {
pq.push({top.first + con[2], {con[0], con[1]}});
}
}
}
return dist.find({g, 0}) == dist.end()? -1: dist[{g, 0}];
}
// Assume s = 0, g = n - 1 and h consists of equal elements.
long long ans = x[n - 1] - x[0];
map<pair<int, int>, long long> dp;
vector<array<int, 4>> events;
for (int i = 0; i < m; i++) {
events.push_back({r[i] - 1, y[i], i, 1});
events.push_back({l[i] - 1, y[i], i, 0});
}
sort(events.rbegin(), events.rend());
vector<vector<int>> right(n);
for (int i = 0; i < m; i++) right[r[i]].push_back(i);
set<pair<int, int>> open;
int ptr = 0;
for (int i = n - 1; i >= 0; i--) {
while (ptr < 2 * m && events[ptr][0] >= i) {
int yc = events[ptr][1];
int ind = events[ptr][2];
int typ = events[ptr][3];
if (typ) {
open.insert({yc, ind});
} else {
open.erase({yc, ind});
}
ptr++;
}
for (int ind: right[i]) {
long long val = 1e18;
auto it = open.lower_bound({y[ind], -1});
if (it != open.end()) {
val = min(val, (long long) ((*it).first - y[ind]) + dp[{r[(*it).second], (*it).first}]);
}
if (it != open.begin()) {
it--;
val = min(val, (long long) (y[ind] - (*it).first) + dp[{r[(*it).second], (*it).first}]);
}
dp[{i, y[ind]}] = i == n - 1? y[ind]: val;
}
}
long long extra = 1e18;
for (int i = 0; i < m; i++) {
if (l[i] == 0) {
extra = min(extra, y[i] + dp[{r[i], y[i]}]);
}
}
ans += extra;
if (ans >= 1e18) return -1;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2372 ms |
201736 KB |
Output is correct |
4 |
Correct |
1774 ms |
234052 KB |
Output is correct |
5 |
Correct |
663 ms |
129608 KB |
Output is correct |
6 |
Correct |
576 ms |
114720 KB |
Output is correct |
7 |
Correct |
714 ms |
129312 KB |
Output is correct |
8 |
Correct |
3450 ms |
261228 KB |
Output is correct |
9 |
Correct |
931 ms |
137544 KB |
Output is correct |
10 |
Correct |
2525 ms |
320060 KB |
Output is correct |
11 |
Correct |
851 ms |
113864 KB |
Output is correct |
12 |
Correct |
519 ms |
88880 KB |
Output is correct |
13 |
Correct |
2005 ms |
281568 KB |
Output is correct |
14 |
Correct |
230 ms |
53836 KB |
Output is correct |
15 |
Correct |
533 ms |
88252 KB |
Output is correct |
16 |
Correct |
515 ms |
89012 KB |
Output is correct |
17 |
Correct |
474 ms |
85760 KB |
Output is correct |
18 |
Correct |
397 ms |
69300 KB |
Output is correct |
19 |
Correct |
12 ms |
4440 KB |
Output is correct |
20 |
Correct |
166 ms |
42888 KB |
Output is correct |
21 |
Correct |
396 ms |
81392 KB |
Output is correct |
22 |
Correct |
597 ms |
87872 KB |
Output is correct |
23 |
Correct |
539 ms |
103164 KB |
Output is correct |
24 |
Correct |
448 ms |
87368 KB |
Output is correct |
25 |
Correct |
448 ms |
84804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
129 ms |
23752 KB |
Output is correct |
2 |
Correct |
174 ms |
13620 KB |
Output is correct |
3 |
Correct |
141 ms |
16004 KB |
Output is correct |
4 |
Correct |
181 ms |
27608 KB |
Output is correct |
5 |
Correct |
215 ms |
33080 KB |
Output is correct |
6 |
Correct |
200 ms |
28472 KB |
Output is correct |
7 |
Correct |
99 ms |
19844 KB |
Output is correct |
8 |
Correct |
455 ms |
84800 KB |
Output is correct |
9 |
Correct |
187 ms |
30260 KB |
Output is correct |
10 |
Correct |
146 ms |
31076 KB |
Output is correct |
11 |
Correct |
16 ms |
6096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
129 ms |
23752 KB |
Output is correct |
2 |
Correct |
174 ms |
13620 KB |
Output is correct |
3 |
Correct |
141 ms |
16004 KB |
Output is correct |
4 |
Correct |
181 ms |
27608 KB |
Output is correct |
5 |
Correct |
215 ms |
33080 KB |
Output is correct |
6 |
Correct |
200 ms |
28472 KB |
Output is correct |
7 |
Correct |
99 ms |
19844 KB |
Output is correct |
8 |
Correct |
455 ms |
84800 KB |
Output is correct |
9 |
Correct |
187 ms |
30260 KB |
Output is correct |
10 |
Correct |
146 ms |
31076 KB |
Output is correct |
11 |
Correct |
16 ms |
6096 KB |
Output is correct |
12 |
Correct |
146 ms |
15484 KB |
Output is correct |
13 |
Correct |
184 ms |
22668 KB |
Output is correct |
14 |
Correct |
220 ms |
25900 KB |
Output is correct |
15 |
Correct |
217 ms |
40740 KB |
Output is correct |
16 |
Correct |
138 ms |
22484 KB |
Output is correct |
17 |
Correct |
135 ms |
22208 KB |
Output is correct |
18 |
Correct |
199 ms |
41400 KB |
Output is correct |
19 |
Correct |
134 ms |
23232 KB |
Output is correct |
20 |
Correct |
83 ms |
15040 KB |
Output is correct |
21 |
Correct |
26 ms |
8144 KB |
Output is correct |
22 |
Correct |
108 ms |
19788 KB |
Output is correct |
23 |
Correct |
113 ms |
20664 KB |
Output is correct |
24 |
Correct |
457 ms |
92984 KB |
Output is correct |
25 |
Correct |
1349 ms |
271216 KB |
Output is correct |
26 |
Correct |
234 ms |
50268 KB |
Output is correct |
27 |
Correct |
220 ms |
27908 KB |
Output is correct |
28 |
Correct |
132 ms |
21864 KB |
Output is correct |
29 |
Correct |
189 ms |
23228 KB |
Output is correct |
30 |
Correct |
95 ms |
15568 KB |
Output is correct |
31 |
Correct |
182 ms |
24136 KB |
Output is correct |
32 |
Correct |
134 ms |
30264 KB |
Output is correct |
33 |
Correct |
126 ms |
27084 KB |
Output is correct |
34 |
Correct |
117 ms |
26304 KB |
Output is correct |
35 |
Correct |
119 ms |
25028 KB |
Output is correct |
36 |
Correct |
634 ms |
103948 KB |
Output is correct |
37 |
Correct |
360 ms |
78708 KB |
Output is correct |
38 |
Correct |
500 ms |
84808 KB |
Output is correct |
39 |
Correct |
578 ms |
100412 KB |
Output is correct |
40 |
Correct |
439 ms |
84544 KB |
Output is correct |
41 |
Correct |
454 ms |
81988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
2372 ms |
201736 KB |
Output is correct |
21 |
Correct |
1774 ms |
234052 KB |
Output is correct |
22 |
Correct |
663 ms |
129608 KB |
Output is correct |
23 |
Correct |
576 ms |
114720 KB |
Output is correct |
24 |
Correct |
714 ms |
129312 KB |
Output is correct |
25 |
Correct |
3450 ms |
261228 KB |
Output is correct |
26 |
Correct |
931 ms |
137544 KB |
Output is correct |
27 |
Correct |
2525 ms |
320060 KB |
Output is correct |
28 |
Correct |
851 ms |
113864 KB |
Output is correct |
29 |
Correct |
519 ms |
88880 KB |
Output is correct |
30 |
Correct |
2005 ms |
281568 KB |
Output is correct |
31 |
Correct |
230 ms |
53836 KB |
Output is correct |
32 |
Correct |
533 ms |
88252 KB |
Output is correct |
33 |
Correct |
515 ms |
89012 KB |
Output is correct |
34 |
Correct |
474 ms |
85760 KB |
Output is correct |
35 |
Correct |
397 ms |
69300 KB |
Output is correct |
36 |
Correct |
12 ms |
4440 KB |
Output is correct |
37 |
Correct |
166 ms |
42888 KB |
Output is correct |
38 |
Correct |
396 ms |
81392 KB |
Output is correct |
39 |
Correct |
597 ms |
87872 KB |
Output is correct |
40 |
Correct |
539 ms |
103164 KB |
Output is correct |
41 |
Correct |
448 ms |
87368 KB |
Output is correct |
42 |
Correct |
448 ms |
84804 KB |
Output is correct |
43 |
Correct |
129 ms |
23752 KB |
Output is correct |
44 |
Correct |
174 ms |
13620 KB |
Output is correct |
45 |
Correct |
141 ms |
16004 KB |
Output is correct |
46 |
Correct |
181 ms |
27608 KB |
Output is correct |
47 |
Correct |
215 ms |
33080 KB |
Output is correct |
48 |
Correct |
200 ms |
28472 KB |
Output is correct |
49 |
Correct |
99 ms |
19844 KB |
Output is correct |
50 |
Correct |
455 ms |
84800 KB |
Output is correct |
51 |
Correct |
187 ms |
30260 KB |
Output is correct |
52 |
Correct |
146 ms |
31076 KB |
Output is correct |
53 |
Correct |
16 ms |
6096 KB |
Output is correct |
54 |
Correct |
146 ms |
15484 KB |
Output is correct |
55 |
Correct |
184 ms |
22668 KB |
Output is correct |
56 |
Correct |
220 ms |
25900 KB |
Output is correct |
57 |
Correct |
217 ms |
40740 KB |
Output is correct |
58 |
Correct |
138 ms |
22484 KB |
Output is correct |
59 |
Correct |
135 ms |
22208 KB |
Output is correct |
60 |
Correct |
199 ms |
41400 KB |
Output is correct |
61 |
Correct |
134 ms |
23232 KB |
Output is correct |
62 |
Correct |
83 ms |
15040 KB |
Output is correct |
63 |
Correct |
26 ms |
8144 KB |
Output is correct |
64 |
Correct |
108 ms |
19788 KB |
Output is correct |
65 |
Correct |
113 ms |
20664 KB |
Output is correct |
66 |
Correct |
457 ms |
92984 KB |
Output is correct |
67 |
Correct |
1349 ms |
271216 KB |
Output is correct |
68 |
Correct |
234 ms |
50268 KB |
Output is correct |
69 |
Correct |
220 ms |
27908 KB |
Output is correct |
70 |
Correct |
132 ms |
21864 KB |
Output is correct |
71 |
Correct |
189 ms |
23228 KB |
Output is correct |
72 |
Correct |
95 ms |
15568 KB |
Output is correct |
73 |
Correct |
182 ms |
24136 KB |
Output is correct |
74 |
Correct |
134 ms |
30264 KB |
Output is correct |
75 |
Correct |
126 ms |
27084 KB |
Output is correct |
76 |
Correct |
117 ms |
26304 KB |
Output is correct |
77 |
Correct |
119 ms |
25028 KB |
Output is correct |
78 |
Correct |
634 ms |
103948 KB |
Output is correct |
79 |
Correct |
360 ms |
78708 KB |
Output is correct |
80 |
Correct |
500 ms |
84808 KB |
Output is correct |
81 |
Correct |
578 ms |
100412 KB |
Output is correct |
82 |
Correct |
439 ms |
84544 KB |
Output is correct |
83 |
Correct |
454 ms |
81988 KB |
Output is correct |
84 |
Correct |
110 ms |
21960 KB |
Output is correct |
85 |
Incorrect |
145 ms |
17296 KB |
Output isn't correct |
86 |
Halted |
0 ms |
0 KB |
- |