#include "walk.h"
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
using namespace std;
using lint = long long;
using pi = pair<lint, int>;
const int MAXN = 100005;
const int MAXV = 2000005;
struct intv{
int s, e, x;
bool operator<(const intv &i)const{
return x < i.x;
}
};
struct point{
int x, y, idx;
};
int n, m;
vector<int> witness[MAXN];
vector<pi> event[MAXN];
vector<pi> gph[MAXV];
lint dist[MAXV];
lint dijkstra(int s, int e){
priority_queue<pi, vector<pi>, greater<pi> > pq;
memset(dist, 0x3f, sizeof(dist));
dist[s] = 0;
pq.emplace(0, s);
while(!pq.empty()){
auto x = pq.top();
pq.pop();
if(x.second == e) return x.first;
if(dist[x.second] != x.first) continue;
for(auto &j : gph[x.second]){
if(dist[j.second] > x.first + j.first){
dist[j.second] = x.first + j.first;
pq.emplace(dist[j.second], j.second);
}
}
}
return -1;
}
void add_edge(point x, point y){
int dist = abs(x.x - y.x) + abs(x.y - y.y);
gph[x.idx].emplace_back(dist, y.idx);
gph[y.idx].emplace_back(dist, x.idx);
}
void make_vertex(vector<intv> v){
for(auto &i : v){
event[i.s].emplace_back(i.x, +1);
event[i.e+1].emplace_back(i.x, -1);
}
multiset<int> swp;
for(int i=0; i<n; i++){
for(auto &j : event[i]){
if(j.second == +1) swp.insert(j.first);
else swp.erase(swp.find(j.first));
}
vector<int> nxt = witness[i];
for(auto &j : witness[i]){
if(j == 0) continue;
auto l = swp.upper_bound(j);
if(l != swp.end()) nxt.push_back(*l);
l = swp.lower_bound(j);
if(l != swp.begin()) nxt.push_back(*prev(l));
}
sort(nxt.begin(), nxt.end());
nxt.resize(unique(nxt.begin(), nxt.end()) - nxt.begin());
witness[i] = nxt;
}
}
long long min_distance(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int s, int e) {
n = sz(x);
m = sz(l);
for(int i=0; i<m; i++){
witness[l[i]].push_back(y[i]);
witness[r[i]].push_back(y[i]);
}
witness[s].push_back(0);
witness[e].push_back(0);
vector<pi> points;
vector<intv> hors;
set<int> alive;
for(int i=0; i<n; i++){
points.emplace_back(h[i], i);
alive.insert(i);
}
for(int i=0; i<m; i++) hors.push_back({l[i], r[i], y[i]});
sort(points.begin(), points.end());
sort(hors.begin(), hors.end());
int ptr = 0;
for(auto &i : hors){
while(ptr < sz(points) && points[ptr].first < i.x){
alive.erase(points[ptr++].second);
}
if(i.s <= s && s <= i.e){
auto it = alive.lower_bound(s);
witness[*it].push_back(i.x);
if(*it != s) witness[*prev(it)].push_back(i.x);
}
if(i.s <= e && e <= i.e){
auto it = alive.lower_bound(e);
witness[*it].push_back(i.x);
if(*it != e) witness[*prev(it)].push_back(i.x);
}
}
make_vertex(hors);
vector<point> ans;
for(int i=0; i<n; i++){
for(auto &j : witness[i]){
int num = ans.size();
ans.push_back({x[i], j, num});
}
}
auto cmpx = [&](const point &x, const point &y) { return pi(x.x, x.y) < pi(y.x, y.y); };
auto cmpy = [&](const point &x, const point &y) { return pi(x.y, x.x) < pi(y.y, y.x); };
sort(ans.begin(), ans.end(), cmpx);
for(int i=0; i<n; i++){
int st = lower_bound(ans.begin(), ans.end(), (point){x[i], 0, -1}, cmpx) - ans.begin();
int ed = upper_bound(ans.begin(), ans.end(), (point){x[i], h[i], -1}, cmpx) - ans.begin();
for(int j=st+1; j<ed; j++){
add_edge(ans[j-1], ans[j]);
}
}
sort(ans.begin(), ans.end(), cmpy);
for(int i=0; i<m; i++){
int st = lower_bound(ans.begin(), ans.end(), (point){x[l[i]], y[i], -1}, cmpy) - ans.begin();
int ed = upper_bound(ans.begin(), ans.end(), (point){x[r[i]], y[i], -1}, cmpy) - ans.begin();
for(int j=st+1; j<ed; j++){
add_edge(ans[j-1], ans[j]);
}
}
s = lower_bound(ans.begin(), ans.end(), (point){x[s], 0, -1}, cmpy)->idx;
e = lower_bound(ans.begin(), ans.end(), (point){x[e], 0, -1}, cmpy)->idx;
return dijkstra(s, e);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
67704 KB |
Output is correct |
2 |
Correct |
63 ms |
67704 KB |
Output is correct |
3 |
Correct |
63 ms |
67576 KB |
Output is correct |
4 |
Correct |
63 ms |
67580 KB |
Output is correct |
5 |
Correct |
63 ms |
67704 KB |
Output is correct |
6 |
Correct |
63 ms |
67704 KB |
Output is correct |
7 |
Correct |
64 ms |
67644 KB |
Output is correct |
8 |
Correct |
62 ms |
67704 KB |
Output is correct |
9 |
Correct |
63 ms |
67660 KB |
Output is correct |
10 |
Correct |
69 ms |
67756 KB |
Output is correct |
11 |
Correct |
69 ms |
67660 KB |
Output is correct |
12 |
Correct |
63 ms |
67704 KB |
Output is correct |
13 |
Correct |
63 ms |
67688 KB |
Output is correct |
14 |
Correct |
63 ms |
67704 KB |
Output is correct |
15 |
Correct |
63 ms |
67704 KB |
Output is correct |
16 |
Correct |
64 ms |
67576 KB |
Output is correct |
17 |
Correct |
63 ms |
67680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
67704 KB |
Output is correct |
2 |
Correct |
66 ms |
67660 KB |
Output is correct |
3 |
Correct |
698 ms |
121684 KB |
Output is correct |
4 |
Correct |
920 ms |
126524 KB |
Output is correct |
5 |
Correct |
632 ms |
113884 KB |
Output is correct |
6 |
Correct |
623 ms |
111100 KB |
Output is correct |
7 |
Correct |
628 ms |
114092 KB |
Output is correct |
8 |
Correct |
766 ms |
125640 KB |
Output is correct |
9 |
Correct |
770 ms |
121996 KB |
Output is correct |
10 |
Correct |
947 ms |
128836 KB |
Output is correct |
11 |
Correct |
703 ms |
111176 KB |
Output is correct |
12 |
Correct |
558 ms |
96672 KB |
Output is correct |
13 |
Correct |
989 ms |
129668 KB |
Output is correct |
14 |
Correct |
585 ms |
102972 KB |
Output is correct |
15 |
Correct |
603 ms |
104216 KB |
Output is correct |
16 |
Correct |
589 ms |
103900 KB |
Output is correct |
17 |
Correct |
577 ms |
102384 KB |
Output is correct |
18 |
Correct |
776 ms |
104764 KB |
Output is correct |
19 |
Correct |
78 ms |
69448 KB |
Output is correct |
20 |
Correct |
256 ms |
84468 KB |
Output is correct |
21 |
Correct |
460 ms |
100580 KB |
Output is correct |
22 |
Correct |
502 ms |
102628 KB |
Output is correct |
23 |
Correct |
888 ms |
110908 KB |
Output is correct |
24 |
Correct |
551 ms |
103132 KB |
Output is correct |
25 |
Correct |
556 ms |
101500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
201 ms |
79876 KB |
Output is correct |
2 |
Correct |
1031 ms |
138092 KB |
Output is correct |
3 |
Correct |
1184 ms |
140856 KB |
Output is correct |
4 |
Correct |
1243 ms |
150308 KB |
Output is correct |
5 |
Correct |
1552 ms |
152956 KB |
Output is correct |
6 |
Correct |
1325 ms |
146636 KB |
Output is correct |
7 |
Correct |
679 ms |
113892 KB |
Output is correct |
8 |
Correct |
484 ms |
102352 KB |
Output is correct |
9 |
Correct |
1317 ms |
141100 KB |
Output is correct |
10 |
Correct |
637 ms |
114260 KB |
Output is correct |
11 |
Correct |
93 ms |
72300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
201 ms |
79876 KB |
Output is correct |
2 |
Correct |
1031 ms |
138092 KB |
Output is correct |
3 |
Correct |
1184 ms |
140856 KB |
Output is correct |
4 |
Correct |
1243 ms |
150308 KB |
Output is correct |
5 |
Correct |
1552 ms |
152956 KB |
Output is correct |
6 |
Correct |
1325 ms |
146636 KB |
Output is correct |
7 |
Correct |
679 ms |
113892 KB |
Output is correct |
8 |
Correct |
484 ms |
102352 KB |
Output is correct |
9 |
Correct |
1317 ms |
141100 KB |
Output is correct |
10 |
Correct |
637 ms |
114260 KB |
Output is correct |
11 |
Correct |
93 ms |
72300 KB |
Output is correct |
12 |
Correct |
1170 ms |
140580 KB |
Output is correct |
13 |
Correct |
1112 ms |
145284 KB |
Output is correct |
14 |
Correct |
1533 ms |
149064 KB |
Output is correct |
15 |
Correct |
994 ms |
133200 KB |
Output is correct |
16 |
Correct |
1118 ms |
131952 KB |
Output is correct |
17 |
Correct |
1229 ms |
145040 KB |
Output is correct |
18 |
Correct |
991 ms |
132956 KB |
Output is correct |
19 |
Correct |
1061 ms |
131648 KB |
Output is correct |
20 |
Correct |
737 ms |
108256 KB |
Output is correct |
21 |
Correct |
193 ms |
77032 KB |
Output is correct |
22 |
Correct |
790 ms |
128080 KB |
Output is correct |
23 |
Correct |
775 ms |
124504 KB |
Output is correct |
24 |
Correct |
614 ms |
108124 KB |
Output is correct |
25 |
Correct |
720 ms |
120028 KB |
Output is correct |
26 |
Correct |
544 ms |
101624 KB |
Output is correct |
27 |
Correct |
1411 ms |
150328 KB |
Output is correct |
28 |
Correct |
1086 ms |
144268 KB |
Output is correct |
29 |
Correct |
1608 ms |
142196 KB |
Output is correct |
30 |
Correct |
710 ms |
109664 KB |
Output is correct |
31 |
Correct |
1351 ms |
136540 KB |
Output is correct |
32 |
Correct |
558 ms |
107468 KB |
Output is correct |
33 |
Correct |
581 ms |
106952 KB |
Output is correct |
34 |
Correct |
775 ms |
111744 KB |
Output is correct |
35 |
Correct |
755 ms |
112604 KB |
Output is correct |
36 |
Correct |
579 ms |
105180 KB |
Output is correct |
37 |
Correct |
479 ms |
100540 KB |
Output is correct |
38 |
Correct |
491 ms |
102220 KB |
Output is correct |
39 |
Correct |
753 ms |
110864 KB |
Output is correct |
40 |
Correct |
548 ms |
102800 KB |
Output is correct |
41 |
Correct |
570 ms |
101732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
67704 KB |
Output is correct |
2 |
Correct |
63 ms |
67704 KB |
Output is correct |
3 |
Correct |
63 ms |
67576 KB |
Output is correct |
4 |
Correct |
63 ms |
67580 KB |
Output is correct |
5 |
Correct |
63 ms |
67704 KB |
Output is correct |
6 |
Correct |
63 ms |
67704 KB |
Output is correct |
7 |
Correct |
64 ms |
67644 KB |
Output is correct |
8 |
Correct |
62 ms |
67704 KB |
Output is correct |
9 |
Correct |
63 ms |
67660 KB |
Output is correct |
10 |
Correct |
69 ms |
67756 KB |
Output is correct |
11 |
Correct |
69 ms |
67660 KB |
Output is correct |
12 |
Correct |
63 ms |
67704 KB |
Output is correct |
13 |
Correct |
63 ms |
67688 KB |
Output is correct |
14 |
Correct |
63 ms |
67704 KB |
Output is correct |
15 |
Correct |
63 ms |
67704 KB |
Output is correct |
16 |
Correct |
64 ms |
67576 KB |
Output is correct |
17 |
Correct |
63 ms |
67680 KB |
Output is correct |
18 |
Correct |
65 ms |
67704 KB |
Output is correct |
19 |
Correct |
66 ms |
67660 KB |
Output is correct |
20 |
Correct |
698 ms |
121684 KB |
Output is correct |
21 |
Correct |
920 ms |
126524 KB |
Output is correct |
22 |
Correct |
632 ms |
113884 KB |
Output is correct |
23 |
Correct |
623 ms |
111100 KB |
Output is correct |
24 |
Correct |
628 ms |
114092 KB |
Output is correct |
25 |
Correct |
766 ms |
125640 KB |
Output is correct |
26 |
Correct |
770 ms |
121996 KB |
Output is correct |
27 |
Correct |
947 ms |
128836 KB |
Output is correct |
28 |
Correct |
703 ms |
111176 KB |
Output is correct |
29 |
Correct |
558 ms |
96672 KB |
Output is correct |
30 |
Correct |
989 ms |
129668 KB |
Output is correct |
31 |
Correct |
585 ms |
102972 KB |
Output is correct |
32 |
Correct |
603 ms |
104216 KB |
Output is correct |
33 |
Correct |
589 ms |
103900 KB |
Output is correct |
34 |
Correct |
577 ms |
102384 KB |
Output is correct |
35 |
Correct |
776 ms |
104764 KB |
Output is correct |
36 |
Correct |
78 ms |
69448 KB |
Output is correct |
37 |
Correct |
256 ms |
84468 KB |
Output is correct |
38 |
Correct |
460 ms |
100580 KB |
Output is correct |
39 |
Correct |
502 ms |
102628 KB |
Output is correct |
40 |
Correct |
888 ms |
110908 KB |
Output is correct |
41 |
Correct |
551 ms |
103132 KB |
Output is correct |
42 |
Correct |
556 ms |
101500 KB |
Output is correct |
43 |
Correct |
201 ms |
79876 KB |
Output is correct |
44 |
Correct |
1031 ms |
138092 KB |
Output is correct |
45 |
Correct |
1184 ms |
140856 KB |
Output is correct |
46 |
Correct |
1243 ms |
150308 KB |
Output is correct |
47 |
Correct |
1552 ms |
152956 KB |
Output is correct |
48 |
Correct |
1325 ms |
146636 KB |
Output is correct |
49 |
Correct |
679 ms |
113892 KB |
Output is correct |
50 |
Correct |
484 ms |
102352 KB |
Output is correct |
51 |
Correct |
1317 ms |
141100 KB |
Output is correct |
52 |
Correct |
637 ms |
114260 KB |
Output is correct |
53 |
Correct |
93 ms |
72300 KB |
Output is correct |
54 |
Correct |
1170 ms |
140580 KB |
Output is correct |
55 |
Correct |
1112 ms |
145284 KB |
Output is correct |
56 |
Correct |
1533 ms |
149064 KB |
Output is correct |
57 |
Correct |
994 ms |
133200 KB |
Output is correct |
58 |
Correct |
1118 ms |
131952 KB |
Output is correct |
59 |
Correct |
1229 ms |
145040 KB |
Output is correct |
60 |
Correct |
991 ms |
132956 KB |
Output is correct |
61 |
Correct |
1061 ms |
131648 KB |
Output is correct |
62 |
Correct |
737 ms |
108256 KB |
Output is correct |
63 |
Correct |
193 ms |
77032 KB |
Output is correct |
64 |
Correct |
790 ms |
128080 KB |
Output is correct |
65 |
Correct |
775 ms |
124504 KB |
Output is correct |
66 |
Correct |
614 ms |
108124 KB |
Output is correct |
67 |
Correct |
720 ms |
120028 KB |
Output is correct |
68 |
Correct |
544 ms |
101624 KB |
Output is correct |
69 |
Correct |
1411 ms |
150328 KB |
Output is correct |
70 |
Correct |
1086 ms |
144268 KB |
Output is correct |
71 |
Correct |
1608 ms |
142196 KB |
Output is correct |
72 |
Correct |
710 ms |
109664 KB |
Output is correct |
73 |
Correct |
1351 ms |
136540 KB |
Output is correct |
74 |
Correct |
558 ms |
107468 KB |
Output is correct |
75 |
Correct |
581 ms |
106952 KB |
Output is correct |
76 |
Correct |
775 ms |
111744 KB |
Output is correct |
77 |
Correct |
755 ms |
112604 KB |
Output is correct |
78 |
Correct |
579 ms |
105180 KB |
Output is correct |
79 |
Correct |
479 ms |
100540 KB |
Output is correct |
80 |
Correct |
491 ms |
102220 KB |
Output is correct |
81 |
Correct |
753 ms |
110864 KB |
Output is correct |
82 |
Correct |
548 ms |
102800 KB |
Output is correct |
83 |
Correct |
570 ms |
101732 KB |
Output is correct |
84 |
Correct |
179 ms |
77612 KB |
Output is correct |
85 |
Correct |
1112 ms |
142516 KB |
Output is correct |
86 |
Correct |
1631 ms |
158008 KB |
Output is correct |
87 |
Correct |
181 ms |
81768 KB |
Output is correct |
88 |
Correct |
237 ms |
79336 KB |
Output is correct |
89 |
Correct |
183 ms |
81896 KB |
Output is correct |
90 |
Correct |
104 ms |
71984 KB |
Output is correct |
91 |
Correct |
66 ms |
67836 KB |
Output is correct |
92 |
Correct |
95 ms |
70792 KB |
Output is correct |
93 |
Correct |
474 ms |
97332 KB |
Output is correct |
94 |
Correct |
199 ms |
77388 KB |
Output is correct |
95 |
Correct |
858 ms |
131704 KB |
Output is correct |
96 |
Correct |
759 ms |
125148 KB |
Output is correct |
97 |
Correct |
631 ms |
108508 KB |
Output is correct |
98 |
Correct |
690 ms |
120220 KB |
Output is correct |
99 |
Correct |
1723 ms |
170732 KB |
Output is correct |
100 |
Correct |
1219 ms |
146064 KB |
Output is correct |
101 |
Correct |
1452 ms |
148260 KB |
Output is correct |
102 |
Correct |
667 ms |
109920 KB |
Output is correct |
103 |
Correct |
588 ms |
104412 KB |
Output is correct |
104 |
Correct |
647 ms |
104740 KB |
Output is correct |
105 |
Correct |
666 ms |
108956 KB |
Output is correct |
106 |
Correct |
625 ms |
106060 KB |
Output is correct |
107 |
Correct |
708 ms |
107612 KB |
Output is correct |
108 |
Correct |
132 ms |
73756 KB |
Output is correct |
109 |
Correct |
1073 ms |
129620 KB |
Output is correct |
110 |
Correct |
1067 ms |
145144 KB |
Output is correct |
111 |
Correct |
1103 ms |
144716 KB |
Output is correct |
112 |
Correct |
706 ms |
111804 KB |
Output is correct |
113 |
Correct |
783 ms |
108872 KB |
Output is correct |