#include "walk.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pli pair<ll,int>
#define fi first
#define se second
const ll inf = 1e18;
const int maxn = 2e6+5;
vector<pii> edge[maxn];
void add_edge(int u,int v,int w){
edge[u].push_back({v,w});
edge[v].push_back({u,w});
}
ll min_distance(vector<int> X, vector<int> H, vector<int> L, vector<int> R, vector<int> Y, int S, int T) {
int N=(int)H.size(),M=(int)Y.size();
vector<pii> p(N);
for(int i=0;i<N;i++) p[i]={H[i],i};
sort(p.begin(),p.end(),greater<pii>());
for(int x:{S,T}){
vector<pii> f(M);
for(int i=0;i<M;i++) f[i]={Y[i],i};
sort(f.begin(),f.end(),greater<pii>());
auto add = [&](int a,int b,int y){
L.push_back(a);
R.push_back(b);
Y.push_back(y);
};
set<int> ss;
int pos=0;
for(auto [y,i]:f){
if(R[i]<=x || x<=L[i]) continue;
while(pos<N && p[pos].fi>=y) ss.insert(p[pos++].se);
int b=*ss.lower_bound(x),a=*prev(ss.upper_bound(x));
if(L[i]<a) add(L[i],a,y);
if(a<b) add(a,b,y);
if(b<R[i]) add(b,R[i],y);
}
M=(int)Y.size();
}
vector<pii> f(M);
for(int i=0;i<M;i++) f[i]={Y[i],i};
sort(f.begin(),f.end(),greater<pii>());
int cnt=0,pos=0;
vector<vector<pii>> g(N);
set<int> ss;
for(auto [y,i]:f){
while(pos<N && p[pos].fi>=y) ss.insert(p[pos++].se);
int a=L[i],b=R[i];ss.insert(b);
auto it=ss.insert(a).fi;
int k=-1;
while(it!=ss.end() && *it<=b){
int j=*it;
if(!g[j].empty()) add_edge(g[j].back().se,cnt,g[j].back().fi-Y[i]);
g[j].push_back({Y[i],cnt});
if(k!=-1) add_edge(cnt-1,cnt,X[j]-X[k]);
k=j;cnt++;
if(j==a || j==b) it++;
else it=ss.erase(it);
}
}
for(int x:{S,T}){
if(g[x].empty()) return -1;
add_edge(g[x].back().se,cnt++,g[x].back().fi);
}
vector<ll> dist(cnt,inf);
priority_queue<pli,vector<pli>,greater<pli>> pq;
dist[cnt-2]=0;pq.push({0,cnt-2});
while(!pq.empty()){
auto [d,u]=pq.top();pq.pop();
if(d!=dist[u]) continue;
for(auto [v,w]:edge[u]){
if(dist[u]+w<dist[v]) pq.push({dist[v]=dist[u]+w,v});
}
}
return (dist[cnt-1]==inf?-1:dist[cnt-1]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
47192 KB |
Output is correct |
2 |
Correct |
14 ms |
47196 KB |
Output is correct |
3 |
Correct |
13 ms |
47192 KB |
Output is correct |
4 |
Correct |
13 ms |
47192 KB |
Output is correct |
5 |
Correct |
14 ms |
47212 KB |
Output is correct |
6 |
Correct |
16 ms |
47196 KB |
Output is correct |
7 |
Correct |
14 ms |
47404 KB |
Output is correct |
8 |
Correct |
13 ms |
47196 KB |
Output is correct |
9 |
Correct |
14 ms |
47364 KB |
Output is correct |
10 |
Correct |
15 ms |
47448 KB |
Output is correct |
11 |
Correct |
14 ms |
47236 KB |
Output is correct |
12 |
Correct |
18 ms |
47220 KB |
Output is correct |
13 |
Correct |
14 ms |
47192 KB |
Output is correct |
14 |
Correct |
15 ms |
47196 KB |
Output is correct |
15 |
Correct |
14 ms |
47196 KB |
Output is correct |
16 |
Correct |
14 ms |
47196 KB |
Output is correct |
17 |
Correct |
15 ms |
47196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
47192 KB |
Output is correct |
2 |
Correct |
14 ms |
47196 KB |
Output is correct |
3 |
Correct |
223 ms |
75592 KB |
Output is correct |
4 |
Correct |
376 ms |
86952 KB |
Output is correct |
5 |
Correct |
214 ms |
80796 KB |
Output is correct |
6 |
Correct |
203 ms |
83236 KB |
Output is correct |
7 |
Correct |
193 ms |
80900 KB |
Output is correct |
8 |
Correct |
213 ms |
76872 KB |
Output is correct |
9 |
Correct |
255 ms |
82912 KB |
Output is correct |
10 |
Correct |
379 ms |
88192 KB |
Output is correct |
11 |
Correct |
209 ms |
74920 KB |
Output is correct |
12 |
Correct |
183 ms |
74816 KB |
Output is correct |
13 |
Correct |
359 ms |
88644 KB |
Output is correct |
14 |
Correct |
242 ms |
92848 KB |
Output is correct |
15 |
Correct |
226 ms |
82948 KB |
Output is correct |
16 |
Correct |
151 ms |
74176 KB |
Output is correct |
17 |
Correct |
162 ms |
72744 KB |
Output is correct |
18 |
Correct |
96 ms |
73284 KB |
Output is correct |
19 |
Correct |
22 ms |
49240 KB |
Output is correct |
20 |
Correct |
103 ms |
67436 KB |
Output is correct |
21 |
Correct |
151 ms |
71920 KB |
Output is correct |
22 |
Correct |
149 ms |
73788 KB |
Output is correct |
23 |
Correct |
143 ms |
76064 KB |
Output is correct |
24 |
Correct |
137 ms |
73536 KB |
Output is correct |
25 |
Correct |
151 ms |
72784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
57528 KB |
Output is correct |
2 |
Correct |
191 ms |
77892 KB |
Output is correct |
3 |
Correct |
212 ms |
80960 KB |
Output is correct |
4 |
Correct |
377 ms |
97092 KB |
Output is correct |
5 |
Correct |
358 ms |
98112 KB |
Output is correct |
6 |
Correct |
360 ms |
96836 KB |
Output is correct |
7 |
Correct |
213 ms |
80064 KB |
Output is correct |
8 |
Correct |
204 ms |
74816 KB |
Output is correct |
9 |
Correct |
364 ms |
95808 KB |
Output is correct |
10 |
Correct |
165 ms |
84036 KB |
Output is correct |
11 |
Correct |
28 ms |
52960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
57528 KB |
Output is correct |
2 |
Correct |
191 ms |
77892 KB |
Output is correct |
3 |
Correct |
212 ms |
80960 KB |
Output is correct |
4 |
Correct |
377 ms |
97092 KB |
Output is correct |
5 |
Correct |
358 ms |
98112 KB |
Output is correct |
6 |
Correct |
360 ms |
96836 KB |
Output is correct |
7 |
Correct |
213 ms |
80064 KB |
Output is correct |
8 |
Correct |
204 ms |
74816 KB |
Output is correct |
9 |
Correct |
364 ms |
95808 KB |
Output is correct |
10 |
Correct |
165 ms |
84036 KB |
Output is correct |
11 |
Correct |
28 ms |
52960 KB |
Output is correct |
12 |
Correct |
228 ms |
80612 KB |
Output is correct |
13 |
Correct |
242 ms |
90432 KB |
Output is correct |
14 |
Correct |
339 ms |
96940 KB |
Output is correct |
15 |
Correct |
289 ms |
87984 KB |
Output is correct |
16 |
Correct |
379 ms |
91712 KB |
Output is correct |
17 |
Correct |
342 ms |
97092 KB |
Output is correct |
18 |
Correct |
273 ms |
87864 KB |
Output is correct |
19 |
Correct |
218 ms |
86804 KB |
Output is correct |
20 |
Correct |
191 ms |
79044 KB |
Output is correct |
21 |
Correct |
69 ms |
61776 KB |
Output is correct |
22 |
Correct |
198 ms |
86168 KB |
Output is correct |
23 |
Correct |
195 ms |
84868 KB |
Output is correct |
24 |
Correct |
165 ms |
77924 KB |
Output is correct |
25 |
Correct |
193 ms |
83388 KB |
Output is correct |
26 |
Correct |
131 ms |
72596 KB |
Output is correct |
27 |
Correct |
389 ms |
97092 KB |
Output is correct |
28 |
Correct |
259 ms |
89876 KB |
Output is correct |
29 |
Correct |
329 ms |
95788 KB |
Output is correct |
30 |
Correct |
202 ms |
79292 KB |
Output is correct |
31 |
Correct |
304 ms |
94400 KB |
Output is correct |
32 |
Correct |
187 ms |
78148 KB |
Output is correct |
33 |
Correct |
162 ms |
78756 KB |
Output is correct |
34 |
Correct |
141 ms |
79632 KB |
Output is correct |
35 |
Correct |
175 ms |
79424 KB |
Output is correct |
36 |
Correct |
157 ms |
75852 KB |
Output is correct |
37 |
Correct |
143 ms |
72004 KB |
Output is correct |
38 |
Correct |
145 ms |
73780 KB |
Output is correct |
39 |
Correct |
131 ms |
76216 KB |
Output is correct |
40 |
Correct |
158 ms |
73536 KB |
Output is correct |
41 |
Correct |
160 ms |
72608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
47192 KB |
Output is correct |
2 |
Correct |
14 ms |
47196 KB |
Output is correct |
3 |
Correct |
13 ms |
47192 KB |
Output is correct |
4 |
Correct |
13 ms |
47192 KB |
Output is correct |
5 |
Correct |
14 ms |
47212 KB |
Output is correct |
6 |
Correct |
16 ms |
47196 KB |
Output is correct |
7 |
Correct |
14 ms |
47404 KB |
Output is correct |
8 |
Correct |
13 ms |
47196 KB |
Output is correct |
9 |
Correct |
14 ms |
47364 KB |
Output is correct |
10 |
Correct |
15 ms |
47448 KB |
Output is correct |
11 |
Correct |
14 ms |
47236 KB |
Output is correct |
12 |
Correct |
18 ms |
47220 KB |
Output is correct |
13 |
Correct |
14 ms |
47192 KB |
Output is correct |
14 |
Correct |
15 ms |
47196 KB |
Output is correct |
15 |
Correct |
14 ms |
47196 KB |
Output is correct |
16 |
Correct |
14 ms |
47196 KB |
Output is correct |
17 |
Correct |
15 ms |
47196 KB |
Output is correct |
18 |
Correct |
14 ms |
47192 KB |
Output is correct |
19 |
Correct |
14 ms |
47196 KB |
Output is correct |
20 |
Correct |
223 ms |
75592 KB |
Output is correct |
21 |
Correct |
376 ms |
86952 KB |
Output is correct |
22 |
Correct |
214 ms |
80796 KB |
Output is correct |
23 |
Correct |
203 ms |
83236 KB |
Output is correct |
24 |
Correct |
193 ms |
80900 KB |
Output is correct |
25 |
Correct |
213 ms |
76872 KB |
Output is correct |
26 |
Correct |
255 ms |
82912 KB |
Output is correct |
27 |
Correct |
379 ms |
88192 KB |
Output is correct |
28 |
Correct |
209 ms |
74920 KB |
Output is correct |
29 |
Correct |
183 ms |
74816 KB |
Output is correct |
30 |
Correct |
359 ms |
88644 KB |
Output is correct |
31 |
Correct |
242 ms |
92848 KB |
Output is correct |
32 |
Correct |
226 ms |
82948 KB |
Output is correct |
33 |
Correct |
151 ms |
74176 KB |
Output is correct |
34 |
Correct |
162 ms |
72744 KB |
Output is correct |
35 |
Correct |
96 ms |
73284 KB |
Output is correct |
36 |
Correct |
22 ms |
49240 KB |
Output is correct |
37 |
Correct |
103 ms |
67436 KB |
Output is correct |
38 |
Correct |
151 ms |
71920 KB |
Output is correct |
39 |
Correct |
149 ms |
73788 KB |
Output is correct |
40 |
Correct |
143 ms |
76064 KB |
Output is correct |
41 |
Correct |
137 ms |
73536 KB |
Output is correct |
42 |
Correct |
151 ms |
72784 KB |
Output is correct |
43 |
Correct |
74 ms |
57528 KB |
Output is correct |
44 |
Correct |
191 ms |
77892 KB |
Output is correct |
45 |
Correct |
212 ms |
80960 KB |
Output is correct |
46 |
Correct |
377 ms |
97092 KB |
Output is correct |
47 |
Correct |
358 ms |
98112 KB |
Output is correct |
48 |
Correct |
360 ms |
96836 KB |
Output is correct |
49 |
Correct |
213 ms |
80064 KB |
Output is correct |
50 |
Correct |
204 ms |
74816 KB |
Output is correct |
51 |
Correct |
364 ms |
95808 KB |
Output is correct |
52 |
Correct |
165 ms |
84036 KB |
Output is correct |
53 |
Correct |
28 ms |
52960 KB |
Output is correct |
54 |
Correct |
228 ms |
80612 KB |
Output is correct |
55 |
Correct |
242 ms |
90432 KB |
Output is correct |
56 |
Correct |
339 ms |
96940 KB |
Output is correct |
57 |
Correct |
289 ms |
87984 KB |
Output is correct |
58 |
Correct |
379 ms |
91712 KB |
Output is correct |
59 |
Correct |
342 ms |
97092 KB |
Output is correct |
60 |
Correct |
273 ms |
87864 KB |
Output is correct |
61 |
Correct |
218 ms |
86804 KB |
Output is correct |
62 |
Correct |
191 ms |
79044 KB |
Output is correct |
63 |
Correct |
69 ms |
61776 KB |
Output is correct |
64 |
Correct |
198 ms |
86168 KB |
Output is correct |
65 |
Correct |
195 ms |
84868 KB |
Output is correct |
66 |
Correct |
165 ms |
77924 KB |
Output is correct |
67 |
Correct |
193 ms |
83388 KB |
Output is correct |
68 |
Correct |
131 ms |
72596 KB |
Output is correct |
69 |
Correct |
389 ms |
97092 KB |
Output is correct |
70 |
Correct |
259 ms |
89876 KB |
Output is correct |
71 |
Correct |
329 ms |
95788 KB |
Output is correct |
72 |
Correct |
202 ms |
79292 KB |
Output is correct |
73 |
Correct |
304 ms |
94400 KB |
Output is correct |
74 |
Correct |
187 ms |
78148 KB |
Output is correct |
75 |
Correct |
162 ms |
78756 KB |
Output is correct |
76 |
Correct |
141 ms |
79632 KB |
Output is correct |
77 |
Correct |
175 ms |
79424 KB |
Output is correct |
78 |
Correct |
157 ms |
75852 KB |
Output is correct |
79 |
Correct |
143 ms |
72004 KB |
Output is correct |
80 |
Correct |
145 ms |
73780 KB |
Output is correct |
81 |
Correct |
131 ms |
76216 KB |
Output is correct |
82 |
Correct |
158 ms |
73536 KB |
Output is correct |
83 |
Correct |
160 ms |
72608 KB |
Output is correct |
84 |
Correct |
58 ms |
55640 KB |
Output is correct |
85 |
Correct |
250 ms |
85056 KB |
Output is correct |
86 |
Correct |
595 ms |
126156 KB |
Output is correct |
87 |
Correct |
92 ms |
63828 KB |
Output is correct |
88 |
Correct |
141 ms |
65076 KB |
Output is correct |
89 |
Correct |
98 ms |
63828 KB |
Output is correct |
90 |
Correct |
31 ms |
50920 KB |
Output is correct |
91 |
Correct |
15 ms |
47452 KB |
Output is correct |
92 |
Correct |
30 ms |
50012 KB |
Output is correct |
93 |
Correct |
240 ms |
74432 KB |
Output is correct |
94 |
Correct |
108 ms |
61872 KB |
Output is correct |
95 |
Correct |
230 ms |
87740 KB |
Output is correct |
96 |
Correct |
227 ms |
86080 KB |
Output is correct |
97 |
Correct |
223 ms |
87088 KB |
Output is correct |
98 |
Correct |
213 ms |
83404 KB |
Output is correct |
99 |
Correct |
1022 ms |
188060 KB |
Output is correct |
100 |
Correct |
450 ms |
96172 KB |
Output is correct |
101 |
Correct |
596 ms |
126904 KB |
Output is correct |
102 |
Correct |
254 ms |
80340 KB |
Output is correct |
103 |
Correct |
161 ms |
78100 KB |
Output is correct |
104 |
Correct |
163 ms |
78760 KB |
Output is correct |
105 |
Correct |
160 ms |
79768 KB |
Output is correct |
106 |
Correct |
202 ms |
83452 KB |
Output is correct |
107 |
Correct |
166 ms |
79640 KB |
Output is correct |
108 |
Correct |
35 ms |
51548 KB |
Output is correct |
109 |
Correct |
335 ms |
89144 KB |
Output is correct |
110 |
Correct |
244 ms |
90464 KB |
Output is correct |
111 |
Correct |
240 ms |
90508 KB |
Output is correct |
112 |
Correct |
233 ms |
79972 KB |
Output is correct |
113 |
Correct |
215 ms |
77316 KB |
Output is correct |