#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18L + 20;
const int maxN = 1e5 + 20;
const int max_nodes = maxN * 12;
int X[maxN];
int Y[maxN * 2];
int H[maxN * 2];
int L[maxN];
int R[maxN];
pair<int, int> orderY[maxN * 2];
vector<int> qadd[maxN];
vector<int> qrem[maxN];
vector<int> row[maxN * 2];
vector<int> col[maxN];
long long dist[max_nodes];
vector<pair<int, int>> adj[max_nodes];
map<pair<int, int>, int> points;
int node_cnt;
int get_id(int i, int j) {
auto it = points.find(make_pair(i, j));
if (it == points.end()) {
return points[make_pair(i, j)] = ++node_cnt;
}
else {
return it->second;
}
}
void add_edge(int i1, int j1, int i2, int j2) {
int u = get_id(i1, j1);
int v = get_id(i2, j2);
int w = abs(X[i1] - X[i2]) + abs(Y[j1] - Y[j2]);
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
long long min_distance(vector<int> _X, vector<int> _H, vector<int> _L, vector<int> _R, vector<int> _Y, int _S, int _T) {
int N = _X.size();
int M = _L.size();
bool sub34 = (_S == 0 && _T == N - 1);
for (int i = 1; i <= N; i++) {
X[i] = _X[i - 1];
}
for (int i = 1; i <= M; i++) {
orderY[i] = make_pair(_Y[i - 1], i);
}
for (int i = M + 1; i <= M + N; i++) {
orderY[i] = make_pair(_H[i - M - 1], i);
}
sort(orderY + 1, orderY + N + M + 1);
for (int i = 1; i <= N + M; i++) {
Y[i] = orderY[i].first;
H[orderY[i].second] = i;
}
for (int i = 1; i <= M; i++) {
L[i] = _L[i - 1] + 1;
R[i] = _R[i - 1] + 1;
qadd[L[i]].push_back(i);
qrem[R[i]].push_back(i);
}
set<int> walks;
for (int i = 1; i <= N; i++) {
col[i].push_back(0);
col[i].push_back(H[M + i]);
for (auto id: qadd[i]) {
walks.insert(H[id]);
}
if (sub34) {
for (auto id: qadd[i]) {
col[i].push_back(H[id]);
row[H[id]].push_back(i);
auto it = walks.lower_bound(H[id]);
if (it != walks.begin()) {
it--;
col[i].push_back(*it);
row[*it].push_back(i);
}
}
for (auto id: qrem[i]) {
col[i].push_back(H[id]);
row[H[id]].push_back(i);
auto it = walks.lower_bound(H[id]);
if (it != walks.begin()) {
it--;
col[i].push_back(*it);
row[*it].push_back(i);
}
}
}
else {
auto it = walks.upper_bound(H[M + i]);
while (it != walks.begin()) {
it--;
col[i].push_back(*it);
row[*it].push_back(i);
}
}
for (auto id: qrem[i]) {
walks.erase(H[id]);
}
}
for (int i = 1; i <= N; i++) {
sort(col[i].begin(), col[i].end());
for (int j = 0; j < (int)col[i].size() - 1; j++) {
add_edge(i, col[i][j], i, col[i][j + 1]);
}
}
for (int i = 1; i <= M; i++) {
sort(row[H[i]].begin(), row[H[i]].end());
for (int j = 0; j < (int)row[H[i]].size() - 1; j++) {
add_edge(row[H[i]][j], H[i], row[H[i]][j + 1], H[i]);
}
}
int S = get_id(_S + 1, 0);
int T = get_id(_T + 1, 0);
for (int i = 1; i <= node_cnt; i++) {
dist[i] = inf;
}
dist[S] = 0;
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> Q;
Q.emplace(0, S);
while (!Q.empty()) {
int u = Q.top().second;
if (Q.top().first != dist[u]) {
Q.pop();
continue;
}
Q.pop();
for (auto e: adj[u]) {
int v = e.first;
int w = e.second;
if (dist[u] + w < dist[v]) {
dist[v] = dist[u] + w;
Q.emplace(dist[v], v);
}
}
}
if (dist[T] == inf) {
return -1;
}
else {
return dist[T];
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
40148 KB |
Output is correct |
2 |
Correct |
18 ms |
40276 KB |
Output is correct |
3 |
Correct |
19 ms |
40276 KB |
Output is correct |
4 |
Correct |
18 ms |
40220 KB |
Output is correct |
5 |
Correct |
22 ms |
40340 KB |
Output is correct |
6 |
Correct |
18 ms |
40344 KB |
Output is correct |
7 |
Correct |
18 ms |
40276 KB |
Output is correct |
8 |
Correct |
18 ms |
40324 KB |
Output is correct |
9 |
Correct |
18 ms |
40232 KB |
Output is correct |
10 |
Correct |
18 ms |
40404 KB |
Output is correct |
11 |
Correct |
18 ms |
40276 KB |
Output is correct |
12 |
Correct |
18 ms |
40260 KB |
Output is correct |
13 |
Correct |
18 ms |
40276 KB |
Output is correct |
14 |
Correct |
18 ms |
40296 KB |
Output is correct |
15 |
Correct |
20 ms |
40176 KB |
Output is correct |
16 |
Correct |
18 ms |
40276 KB |
Output is correct |
17 |
Correct |
18 ms |
40308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
40148 KB |
Output is correct |
2 |
Correct |
17 ms |
40152 KB |
Output is correct |
3 |
Correct |
1512 ms |
162760 KB |
Output is correct |
4 |
Correct |
1332 ms |
190436 KB |
Output is correct |
5 |
Correct |
1024 ms |
170240 KB |
Output is correct |
6 |
Correct |
918 ms |
157744 KB |
Output is correct |
7 |
Correct |
954 ms |
170316 KB |
Output is correct |
8 |
Correct |
1935 ms |
194864 KB |
Output is correct |
9 |
Correct |
1102 ms |
166900 KB |
Output is correct |
10 |
Correct |
1848 ms |
237380 KB |
Output is correct |
11 |
Correct |
700 ms |
115312 KB |
Output is correct |
12 |
Correct |
501 ms |
112596 KB |
Output is correct |
13 |
Correct |
696 ms |
135900 KB |
Output is correct |
14 |
Correct |
451 ms |
106556 KB |
Output is correct |
15 |
Correct |
612 ms |
110112 KB |
Output is correct |
16 |
Correct |
471 ms |
109752 KB |
Output is correct |
17 |
Correct |
508 ms |
107716 KB |
Output is correct |
18 |
Correct |
665 ms |
118568 KB |
Output is correct |
19 |
Correct |
31 ms |
43476 KB |
Output is correct |
20 |
Correct |
166 ms |
72716 KB |
Output is correct |
21 |
Correct |
442 ms |
105312 KB |
Output is correct |
22 |
Correct |
516 ms |
112740 KB |
Output is correct |
23 |
Correct |
681 ms |
120208 KB |
Output is correct |
24 |
Correct |
505 ms |
111136 KB |
Output is correct |
25 |
Correct |
474 ms |
108768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
208 ms |
66700 KB |
Output is correct |
2 |
Correct |
797 ms |
109932 KB |
Output is correct |
3 |
Correct |
780 ms |
116516 KB |
Output is correct |
4 |
Correct |
1059 ms |
145640 KB |
Output is correct |
5 |
Correct |
1169 ms |
144880 KB |
Output is correct |
6 |
Correct |
1142 ms |
142640 KB |
Output is correct |
7 |
Correct |
504 ms |
108924 KB |
Output is correct |
8 |
Correct |
499 ms |
116656 KB |
Output is correct |
9 |
Correct |
1186 ms |
141580 KB |
Output is correct |
10 |
Correct |
672 ms |
118680 KB |
Output is correct |
11 |
Correct |
58 ms |
54532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
208 ms |
66700 KB |
Output is correct |
2 |
Correct |
797 ms |
109932 KB |
Output is correct |
3 |
Correct |
780 ms |
116516 KB |
Output is correct |
4 |
Correct |
1059 ms |
145640 KB |
Output is correct |
5 |
Correct |
1169 ms |
144880 KB |
Output is correct |
6 |
Correct |
1142 ms |
142640 KB |
Output is correct |
7 |
Correct |
504 ms |
108924 KB |
Output is correct |
8 |
Correct |
499 ms |
116656 KB |
Output is correct |
9 |
Correct |
1186 ms |
141580 KB |
Output is correct |
10 |
Correct |
672 ms |
118680 KB |
Output is correct |
11 |
Correct |
58 ms |
54532 KB |
Output is correct |
12 |
Correct |
788 ms |
116480 KB |
Output is correct |
13 |
Correct |
925 ms |
144336 KB |
Output is correct |
14 |
Correct |
1171 ms |
144828 KB |
Output is correct |
15 |
Correct |
923 ms |
135736 KB |
Output is correct |
16 |
Correct |
813 ms |
135924 KB |
Output is correct |
17 |
Correct |
968 ms |
145952 KB |
Output is correct |
18 |
Correct |
895 ms |
135828 KB |
Output is correct |
19 |
Correct |
852 ms |
136016 KB |
Output is correct |
20 |
Correct |
545 ms |
108084 KB |
Output is correct |
21 |
Correct |
142 ms |
69816 KB |
Output is correct |
22 |
Correct |
614 ms |
128900 KB |
Output is correct |
23 |
Correct |
593 ms |
127296 KB |
Output is correct |
24 |
Correct |
495 ms |
116304 KB |
Output is correct |
25 |
Correct |
556 ms |
122916 KB |
Output is correct |
26 |
Correct |
440 ms |
110396 KB |
Output is correct |
27 |
Correct |
1422 ms |
145444 KB |
Output is correct |
28 |
Correct |
751 ms |
143712 KB |
Output is correct |
29 |
Correct |
1255 ms |
142520 KB |
Output is correct |
30 |
Correct |
536 ms |
109300 KB |
Output is correct |
31 |
Correct |
1192 ms |
139540 KB |
Output is correct |
32 |
Correct |
540 ms |
112888 KB |
Output is correct |
33 |
Correct |
536 ms |
117236 KB |
Output is correct |
34 |
Correct |
658 ms |
119704 KB |
Output is correct |
35 |
Correct |
673 ms |
120340 KB |
Output is correct |
36 |
Correct |
482 ms |
114864 KB |
Output is correct |
37 |
Correct |
425 ms |
108104 KB |
Output is correct |
38 |
Correct |
475 ms |
115812 KB |
Output is correct |
39 |
Correct |
696 ms |
123096 KB |
Output is correct |
40 |
Correct |
506 ms |
114204 KB |
Output is correct |
41 |
Correct |
446 ms |
111480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
40148 KB |
Output is correct |
2 |
Correct |
18 ms |
40276 KB |
Output is correct |
3 |
Correct |
19 ms |
40276 KB |
Output is correct |
4 |
Correct |
18 ms |
40220 KB |
Output is correct |
5 |
Correct |
22 ms |
40340 KB |
Output is correct |
6 |
Correct |
18 ms |
40344 KB |
Output is correct |
7 |
Correct |
18 ms |
40276 KB |
Output is correct |
8 |
Correct |
18 ms |
40324 KB |
Output is correct |
9 |
Correct |
18 ms |
40232 KB |
Output is correct |
10 |
Correct |
18 ms |
40404 KB |
Output is correct |
11 |
Correct |
18 ms |
40276 KB |
Output is correct |
12 |
Correct |
18 ms |
40260 KB |
Output is correct |
13 |
Correct |
18 ms |
40276 KB |
Output is correct |
14 |
Correct |
18 ms |
40296 KB |
Output is correct |
15 |
Correct |
20 ms |
40176 KB |
Output is correct |
16 |
Correct |
18 ms |
40276 KB |
Output is correct |
17 |
Correct |
18 ms |
40308 KB |
Output is correct |
18 |
Correct |
17 ms |
40148 KB |
Output is correct |
19 |
Correct |
17 ms |
40152 KB |
Output is correct |
20 |
Correct |
1512 ms |
162760 KB |
Output is correct |
21 |
Correct |
1332 ms |
190436 KB |
Output is correct |
22 |
Correct |
1024 ms |
170240 KB |
Output is correct |
23 |
Correct |
918 ms |
157744 KB |
Output is correct |
24 |
Correct |
954 ms |
170316 KB |
Output is correct |
25 |
Correct |
1935 ms |
194864 KB |
Output is correct |
26 |
Correct |
1102 ms |
166900 KB |
Output is correct |
27 |
Correct |
1848 ms |
237380 KB |
Output is correct |
28 |
Correct |
700 ms |
115312 KB |
Output is correct |
29 |
Correct |
501 ms |
112596 KB |
Output is correct |
30 |
Correct |
696 ms |
135900 KB |
Output is correct |
31 |
Correct |
451 ms |
106556 KB |
Output is correct |
32 |
Correct |
612 ms |
110112 KB |
Output is correct |
33 |
Correct |
471 ms |
109752 KB |
Output is correct |
34 |
Correct |
508 ms |
107716 KB |
Output is correct |
35 |
Correct |
665 ms |
118568 KB |
Output is correct |
36 |
Correct |
31 ms |
43476 KB |
Output is correct |
37 |
Correct |
166 ms |
72716 KB |
Output is correct |
38 |
Correct |
442 ms |
105312 KB |
Output is correct |
39 |
Correct |
516 ms |
112740 KB |
Output is correct |
40 |
Correct |
681 ms |
120208 KB |
Output is correct |
41 |
Correct |
505 ms |
111136 KB |
Output is correct |
42 |
Correct |
474 ms |
108768 KB |
Output is correct |
43 |
Correct |
208 ms |
66700 KB |
Output is correct |
44 |
Correct |
797 ms |
109932 KB |
Output is correct |
45 |
Correct |
780 ms |
116516 KB |
Output is correct |
46 |
Correct |
1059 ms |
145640 KB |
Output is correct |
47 |
Correct |
1169 ms |
144880 KB |
Output is correct |
48 |
Correct |
1142 ms |
142640 KB |
Output is correct |
49 |
Correct |
504 ms |
108924 KB |
Output is correct |
50 |
Correct |
499 ms |
116656 KB |
Output is correct |
51 |
Correct |
1186 ms |
141580 KB |
Output is correct |
52 |
Correct |
672 ms |
118680 KB |
Output is correct |
53 |
Correct |
58 ms |
54532 KB |
Output is correct |
54 |
Correct |
788 ms |
116480 KB |
Output is correct |
55 |
Correct |
925 ms |
144336 KB |
Output is correct |
56 |
Correct |
1171 ms |
144828 KB |
Output is correct |
57 |
Correct |
923 ms |
135736 KB |
Output is correct |
58 |
Correct |
813 ms |
135924 KB |
Output is correct |
59 |
Correct |
968 ms |
145952 KB |
Output is correct |
60 |
Correct |
895 ms |
135828 KB |
Output is correct |
61 |
Correct |
852 ms |
136016 KB |
Output is correct |
62 |
Correct |
545 ms |
108084 KB |
Output is correct |
63 |
Correct |
142 ms |
69816 KB |
Output is correct |
64 |
Correct |
614 ms |
128900 KB |
Output is correct |
65 |
Correct |
593 ms |
127296 KB |
Output is correct |
66 |
Correct |
495 ms |
116304 KB |
Output is correct |
67 |
Correct |
556 ms |
122916 KB |
Output is correct |
68 |
Correct |
440 ms |
110396 KB |
Output is correct |
69 |
Correct |
1422 ms |
145444 KB |
Output is correct |
70 |
Correct |
751 ms |
143712 KB |
Output is correct |
71 |
Correct |
1255 ms |
142520 KB |
Output is correct |
72 |
Correct |
536 ms |
109300 KB |
Output is correct |
73 |
Correct |
1192 ms |
139540 KB |
Output is correct |
74 |
Correct |
540 ms |
112888 KB |
Output is correct |
75 |
Correct |
536 ms |
117236 KB |
Output is correct |
76 |
Correct |
658 ms |
119704 KB |
Output is correct |
77 |
Correct |
673 ms |
120340 KB |
Output is correct |
78 |
Correct |
482 ms |
114864 KB |
Output is correct |
79 |
Correct |
425 ms |
108104 KB |
Output is correct |
80 |
Correct |
475 ms |
115812 KB |
Output is correct |
81 |
Correct |
696 ms |
123096 KB |
Output is correct |
82 |
Correct |
506 ms |
114204 KB |
Output is correct |
83 |
Correct |
446 ms |
111480 KB |
Output is correct |
84 |
Correct |
144 ms |
61416 KB |
Output is correct |
85 |
Runtime error |
3664 ms |
1048576 KB |
Execution killed with signal 11 |
86 |
Halted |
0 ms |
0 KB |
- |