#include "walk.h"
#include <set>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const long long inf = 1LL << 61;
struct edge {
int ax, bx, y;
};
struct state {
int pos; long long cost;
bool operator<(const state& s) const {
return cost > s.cost;
}
};
long long min_distance(std::vector<int> x, std::vector<int> h, std::vector<int> l, std::vector<int> r, std::vector<int> y, int s, int g) {
int N = x.size(), M = y.size();
vector<int> compy = h;
compy.insert(compy.end(), y.begin(), y.end());
sort(compy.begin(), compy.end());
compy.erase(unique(compy.begin(), compy.end()), compy.end());
int S = compy.size();
vector<vector<int> > layer(S);
for(int i = 0; i < N; ++i) {
layer[lower_bound(compy.begin(), compy.end(), h[i]) - compy.begin()].push_back(i);
}
for(int i = 0; i < M; ++i) {
layer[lower_bound(compy.begin(), compy.end(), y[i]) - compy.begin()].push_back(i + N);
}
vector<vector<int> > branchy(N, vector<int>({ 0 }));
vector<edge> E;
set<int> st;
for(int i = S - 1; i >= 0; --i) {
sort(layer[i].begin(), layer[i].end());
for(int j : layer[i]) {
if(j < N) {
st.insert(j);
}
else {
int id = j - N;
set<int>::iterator it = st.lower_bound(l[id]);
while(it != st.end() && *it <= r[id]) {
int px = *it;
branchy[px].push_back(y[id]);
++it;
if(it != st.end() && *it <= r[id]) {
int qx = *it;
E.push_back(edge{px, qx, y[id]});
}
}
}
}
}
vector<int> vx, vy;
vector<int> branchsum(N + 1);
for(int i = 0; i < N; ++i) {
sort(branchy[i].begin(), branchy[i].end());
branchy[i].erase(unique(branchy[i].begin(), branchy[i].end()), branchy[i].end());
branchsum[i + 1] = branchsum[i] + branchy[i].size();
for(int j : branchy[i]) {
vx.push_back(x[i]);
vy.push_back(j);
}
}
int V = branchsum[N];
vector<vector<int> > G(V);
for(int i = 0; i < N; ++i) {
for(int j = branchsum[i]; j < branchsum[i + 1] - 1; ++j) {
G[j].push_back(j + 1);
G[j + 1].push_back(j);
}
}
for(int i = 0; i < int(E.size()); ++i) {
int pa = lower_bound(branchy[E[i].ax].begin(), branchy[E[i].ax].end(), E[i].y) - branchy[E[i].ax].begin();
int pb = lower_bound(branchy[E[i].bx].begin(), branchy[E[i].bx].end(), E[i].y) - branchy[E[i].bx].begin();
int ia = branchsum[E[i].ax] + pa;
int ib = branchsum[E[i].bx] + pb;
G[ia].push_back(ib);
G[ib].push_back(ia);
}
priority_queue<state> que;
que.push(state{branchsum[s], 0});
vector<long long> dist(V, inf);
dist[branchsum[s]] = 0;
vector<bool> vis(V, false);
while(!que.empty()) {
int u = que.top().pos; que.pop();
if(vis[u]) continue;
vis[u] = true;
for(int i : G[u]) {
long long nd = dist[u] + abs(vx[i] - vx[u]) + abs(vy[i] - vy[u]);
if(dist[i] > nd) {
dist[i] = nd;
que.push(state{i, dist[i]});
}
}
}
return (dist[branchsum[g]] != inf ? dist[branchsum[g]] : -1LL);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
733 ms |
73356 KB |
Output is correct |
4 |
Correct |
954 ms |
93924 KB |
Output is correct |
5 |
Correct |
595 ms |
84060 KB |
Output is correct |
6 |
Correct |
537 ms |
75500 KB |
Output is correct |
7 |
Correct |
595 ms |
82788 KB |
Output is correct |
8 |
Correct |
922 ms |
89924 KB |
Output is correct |
9 |
Correct |
733 ms |
81188 KB |
Output is correct |
10 |
Correct |
1238 ms |
119760 KB |
Output is correct |
11 |
Correct |
496 ms |
48008 KB |
Output is correct |
12 |
Correct |
493 ms |
48996 KB |
Output is correct |
13 |
Correct |
1084 ms |
108624 KB |
Output is correct |
14 |
Correct |
319 ms |
44644 KB |
Output is correct |
15 |
Correct |
319 ms |
40036 KB |
Output is correct |
16 |
Correct |
281 ms |
38592 KB |
Output is correct |
17 |
Correct |
280 ms |
37008 KB |
Output is correct |
18 |
Correct |
231 ms |
44772 KB |
Output is correct |
19 |
Correct |
10 ms |
2432 KB |
Output is correct |
20 |
Correct |
107 ms |
21104 KB |
Output is correct |
21 |
Correct |
285 ms |
34004 KB |
Output is correct |
22 |
Correct |
334 ms |
38952 KB |
Output is correct |
23 |
Correct |
333 ms |
49000 KB |
Output is correct |
24 |
Correct |
290 ms |
38140 KB |
Output is correct |
25 |
Correct |
294 ms |
36372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
9584 KB |
Output is correct |
2 |
Execution timed out |
4054 ms |
468852 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
9584 KB |
Output is correct |
2 |
Execution timed out |
4054 ms |
468852 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
733 ms |
73356 KB |
Output is correct |
21 |
Correct |
954 ms |
93924 KB |
Output is correct |
22 |
Correct |
595 ms |
84060 KB |
Output is correct |
23 |
Correct |
537 ms |
75500 KB |
Output is correct |
24 |
Correct |
595 ms |
82788 KB |
Output is correct |
25 |
Correct |
922 ms |
89924 KB |
Output is correct |
26 |
Correct |
733 ms |
81188 KB |
Output is correct |
27 |
Correct |
1238 ms |
119760 KB |
Output is correct |
28 |
Correct |
496 ms |
48008 KB |
Output is correct |
29 |
Correct |
493 ms |
48996 KB |
Output is correct |
30 |
Correct |
1084 ms |
108624 KB |
Output is correct |
31 |
Correct |
319 ms |
44644 KB |
Output is correct |
32 |
Correct |
319 ms |
40036 KB |
Output is correct |
33 |
Correct |
281 ms |
38592 KB |
Output is correct |
34 |
Correct |
280 ms |
37008 KB |
Output is correct |
35 |
Correct |
231 ms |
44772 KB |
Output is correct |
36 |
Correct |
10 ms |
2432 KB |
Output is correct |
37 |
Correct |
107 ms |
21104 KB |
Output is correct |
38 |
Correct |
285 ms |
34004 KB |
Output is correct |
39 |
Correct |
334 ms |
38952 KB |
Output is correct |
40 |
Correct |
333 ms |
49000 KB |
Output is correct |
41 |
Correct |
290 ms |
38140 KB |
Output is correct |
42 |
Correct |
294 ms |
36372 KB |
Output is correct |
43 |
Correct |
75 ms |
9584 KB |
Output is correct |
44 |
Execution timed out |
4054 ms |
468852 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |