#include "walk.h"
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF=1e18;
priority_queue <pair <int, int>, vector <pair <int, int>>, greater <pair <int, int>>> q;
vector <int> a[100001],b[1000001],V;
vector <int32_t> x,h,l,r,y;
vector <pair <int, int>> ke[1000001],ve;
vector <pair <int32_t, int32_t>> nodes;
int n,m,d[1000001];
set <int> S;
void add(int i, int j){
a[i].push_back(j);
nodes.push_back({x[i],(j==-1?0:y[j])});
}
int f(int i, int j){
return lower_bound(nodes.begin(),nodes.end(),make_pair(x[i],(j==-1?0:y[j])))-nodes.begin();
}
void reconnect(int32_t s, int mode){
vector <pair <int32_t, int32_t>> tmp,changes;
for (int i=s;(mode?i<n:i>=0);i+=mode*2-1)
if (tmp.empty()||h[i]>tmp.back().first)
tmp.push_back({h[i],i});
for (int i=0;i<l.size();i++)
if (l[i]<s&&s<r[i]){
auto a=tmp[lower_bound(tmp.begin(),tmp.end(),make_pair(y[i],-1))-tmp.begin()];
changes.push_back({i,a.second});
}
for (auto [i,j]:changes){
if (j==l[i]||j==r[i])
continue;
if (mode)
swap(l,r);
l.push_back(j);
r.push_back(r[i]);
y.push_back(y[i]);
r[i]=j;
if (mode)
swap(l,r);
}
}
int min_distance(vector<int32_t> X, vector<int32_t> H, vector<int32_t> L, vector<int32_t> R, vector<int32_t> Y, int32_t s, int32_t g){
x=X,h=H,l=L,r=R,y=Y,n=x.size();
if (s>g)
swap(s,g);
reconnect(s,0);
reconnect(s,1);
reconnect(g,0);
reconnect(g,1);
m=l.size();
for (int i=0;i<m;i++){
ve.push_back({l[i],i});
ve.push_back({r[i],i});
}
sort(ve.begin(),ve.end(),[](pair <int, int> a, pair <int, int> b){
if (a.first!=b.first)
return a.first<b.first;
bool p=(a.first==r[a.second]),q=(b.first==r[b.second]);
if (p!=q)
return p<q;
if (p)
return y[a.second]>y[b.second];
return y[a.second]<y[b.second];
});
set <pair <int, int>> cur;
for (auto [i,j]:ve){
add(i,j);
b[j].push_back(i);
if (i==l[j])
cur.insert({y[j],j});
else
cur.erase({y[j],j});
if (!cur.empty()){
auto it=cur.lower_bound(make_pair(y[j],-1));
if (it!=cur.begin()){
it--;
add(i,(*it).second);
b[(*it).second].push_back(i);
}
}
}
for (int i=0;i<n;i++)
sort(a[i].begin(),a[i].end(),[](int i, int j){return y[i]>y[j];});
for (int i=0;i<m;i++)
sort(b[i].begin(),b[i].end(),[](int i, int j){return x[i]<x[j];});
add(s,-1);
add(g,-1);
sort(nodes.begin(),nodes.end());
nodes.resize(unique(nodes.begin(),nodes.end())-nodes.begin());
for (int i=0;i<n;i++)
for (int j=0;j<(int)a[i].size()-1;j++){
int u=f(i,a[i][j]),v=f(i,a[i][j+1]),w=(a[i][j]>=0?y[a[i][j]]:0)-(a[i][j+1]>=0?y[a[i][j+1]]:0);
if (u==v)
continue;
ke[u].push_back({v,w});
ke[v].push_back({u,w});
}
for (int i=0;i<m;i++)
for (int j=0;j<(int)b[i].size()-1;j++){
int u=f(b[i][j],i),v=f(b[i][j+1],i),w=x[b[i][j+1]]-x[b[i][j]];
if (u==v)
continue;
ke[u].push_back({v,w});
ke[v].push_back({u,w});
}
fill(d,d+nodes.size(),INF);
d[f(s,-1)]=0;
q.push({0,f(s,-1)});
while (!q.empty()){
auto [du,u]=q.top();
q.pop();
if (du!=d[u])
continue;
for (auto [v,w]:ke[u])
if (d[v]>d[u]+w){
d[v]=d[u]+w;
q.push({d[v],v});
}
}
return (d[f(g,-1)]==INF?-1:d[f(g,-1)]);
}
Compilation message
walk.cpp: In function 'void reconnect(int32_t, long long int)':
walk.cpp:25:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (int i=0;i<l.size();i++)
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
51036 KB |
Output is correct |
2 |
Correct |
13 ms |
51292 KB |
Output is correct |
3 |
Correct |
11 ms |
51036 KB |
Output is correct |
4 |
Correct |
11 ms |
51036 KB |
Output is correct |
5 |
Correct |
11 ms |
51188 KB |
Output is correct |
6 |
Correct |
11 ms |
51292 KB |
Output is correct |
7 |
Correct |
11 ms |
51208 KB |
Output is correct |
8 |
Correct |
11 ms |
51292 KB |
Output is correct |
9 |
Correct |
11 ms |
51036 KB |
Output is correct |
10 |
Correct |
12 ms |
51292 KB |
Output is correct |
11 |
Correct |
11 ms |
51208 KB |
Output is correct |
12 |
Correct |
11 ms |
51288 KB |
Output is correct |
13 |
Correct |
11 ms |
51032 KB |
Output is correct |
14 |
Correct |
11 ms |
51220 KB |
Output is correct |
15 |
Correct |
12 ms |
51032 KB |
Output is correct |
16 |
Correct |
12 ms |
51032 KB |
Output is correct |
17 |
Correct |
11 ms |
51292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
51036 KB |
Output is correct |
2 |
Correct |
12 ms |
51036 KB |
Output is correct |
3 |
Correct |
525 ms |
105028 KB |
Output is correct |
4 |
Correct |
487 ms |
107516 KB |
Output is correct |
5 |
Correct |
280 ms |
93268 KB |
Output is correct |
6 |
Correct |
283 ms |
93684 KB |
Output is correct |
7 |
Correct |
279 ms |
96472 KB |
Output is correct |
8 |
Correct |
550 ms |
110460 KB |
Output is correct |
9 |
Correct |
474 ms |
105832 KB |
Output is correct |
10 |
Correct |
488 ms |
108436 KB |
Output is correct |
11 |
Correct |
421 ms |
100972 KB |
Output is correct |
12 |
Correct |
274 ms |
88280 KB |
Output is correct |
13 |
Correct |
477 ms |
110304 KB |
Output is correct |
14 |
Correct |
290 ms |
88248 KB |
Output is correct |
15 |
Correct |
284 ms |
87812 KB |
Output is correct |
16 |
Correct |
342 ms |
92052 KB |
Output is correct |
17 |
Correct |
265 ms |
88536 KB |
Output is correct |
18 |
Correct |
408 ms |
97412 KB |
Output is correct |
19 |
Correct |
20 ms |
52924 KB |
Output is correct |
20 |
Correct |
99 ms |
70100 KB |
Output is correct |
21 |
Correct |
268 ms |
90908 KB |
Output is correct |
22 |
Correct |
250 ms |
87988 KB |
Output is correct |
23 |
Correct |
379 ms |
94912 KB |
Output is correct |
24 |
Correct |
272 ms |
90568 KB |
Output is correct |
25 |
Correct |
282 ms |
88248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
113 ms |
67652 KB |
Output is correct |
2 |
Correct |
620 ms |
111748 KB |
Output is correct |
3 |
Correct |
626 ms |
112316 KB |
Output is correct |
4 |
Correct |
683 ms |
111292 KB |
Output is correct |
5 |
Correct |
797 ms |
116156 KB |
Output is correct |
6 |
Correct |
659 ms |
111504 KB |
Output is correct |
7 |
Correct |
267 ms |
81652 KB |
Output is correct |
8 |
Correct |
254 ms |
84992 KB |
Output is correct |
9 |
Correct |
623 ms |
109904 KB |
Output is correct |
10 |
Correct |
307 ms |
89788 KB |
Output is correct |
11 |
Correct |
19 ms |
52316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
113 ms |
67652 KB |
Output is correct |
2 |
Correct |
620 ms |
111748 KB |
Output is correct |
3 |
Correct |
626 ms |
112316 KB |
Output is correct |
4 |
Correct |
683 ms |
111292 KB |
Output is correct |
5 |
Correct |
797 ms |
116156 KB |
Output is correct |
6 |
Correct |
659 ms |
111504 KB |
Output is correct |
7 |
Correct |
267 ms |
81652 KB |
Output is correct |
8 |
Correct |
254 ms |
84992 KB |
Output is correct |
9 |
Correct |
623 ms |
109904 KB |
Output is correct |
10 |
Correct |
307 ms |
89788 KB |
Output is correct |
11 |
Correct |
19 ms |
52316 KB |
Output is correct |
12 |
Correct |
642 ms |
111472 KB |
Output is correct |
13 |
Correct |
496 ms |
110684 KB |
Output is correct |
14 |
Correct |
730 ms |
116848 KB |
Output is correct |
15 |
Correct |
477 ms |
104560 KB |
Output is correct |
16 |
Correct |
499 ms |
106232 KB |
Output is correct |
17 |
Correct |
528 ms |
111040 KB |
Output is correct |
18 |
Correct |
464 ms |
103140 KB |
Output is correct |
19 |
Correct |
510 ms |
107220 KB |
Output is correct |
20 |
Correct |
273 ms |
81380 KB |
Output is correct |
21 |
Correct |
32 ms |
54100 KB |
Output is correct |
22 |
Correct |
393 ms |
100584 KB |
Output is correct |
23 |
Correct |
398 ms |
99784 KB |
Output is correct |
24 |
Correct |
300 ms |
88800 KB |
Output is correct |
25 |
Correct |
333 ms |
95488 KB |
Output is correct |
26 |
Correct |
267 ms |
86416 KB |
Output is correct |
27 |
Correct |
784 ms |
116916 KB |
Output is correct |
28 |
Correct |
464 ms |
110104 KB |
Output is correct |
29 |
Correct |
750 ms |
111600 KB |
Output is correct |
30 |
Correct |
320 ms |
81668 KB |
Output is correct |
31 |
Correct |
727 ms |
110020 KB |
Output is correct |
32 |
Correct |
320 ms |
87012 KB |
Output is correct |
33 |
Correct |
339 ms |
85252 KB |
Output is correct |
34 |
Correct |
394 ms |
91348 KB |
Output is correct |
35 |
Correct |
380 ms |
90916 KB |
Output is correct |
36 |
Correct |
307 ms |
89424 KB |
Output is correct |
37 |
Correct |
293 ms |
86268 KB |
Output is correct |
38 |
Correct |
266 ms |
83384 KB |
Output is correct |
39 |
Correct |
421 ms |
92856 KB |
Output is correct |
40 |
Correct |
279 ms |
87464 KB |
Output is correct |
41 |
Correct |
302 ms |
87396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
51036 KB |
Output is correct |
2 |
Correct |
13 ms |
51292 KB |
Output is correct |
3 |
Correct |
11 ms |
51036 KB |
Output is correct |
4 |
Correct |
11 ms |
51036 KB |
Output is correct |
5 |
Correct |
11 ms |
51188 KB |
Output is correct |
6 |
Correct |
11 ms |
51292 KB |
Output is correct |
7 |
Correct |
11 ms |
51208 KB |
Output is correct |
8 |
Correct |
11 ms |
51292 KB |
Output is correct |
9 |
Correct |
11 ms |
51036 KB |
Output is correct |
10 |
Correct |
12 ms |
51292 KB |
Output is correct |
11 |
Correct |
11 ms |
51208 KB |
Output is correct |
12 |
Correct |
11 ms |
51288 KB |
Output is correct |
13 |
Correct |
11 ms |
51032 KB |
Output is correct |
14 |
Correct |
11 ms |
51220 KB |
Output is correct |
15 |
Correct |
12 ms |
51032 KB |
Output is correct |
16 |
Correct |
12 ms |
51032 KB |
Output is correct |
17 |
Correct |
11 ms |
51292 KB |
Output is correct |
18 |
Correct |
11 ms |
51036 KB |
Output is correct |
19 |
Correct |
12 ms |
51036 KB |
Output is correct |
20 |
Correct |
525 ms |
105028 KB |
Output is correct |
21 |
Correct |
487 ms |
107516 KB |
Output is correct |
22 |
Correct |
280 ms |
93268 KB |
Output is correct |
23 |
Correct |
283 ms |
93684 KB |
Output is correct |
24 |
Correct |
279 ms |
96472 KB |
Output is correct |
25 |
Correct |
550 ms |
110460 KB |
Output is correct |
26 |
Correct |
474 ms |
105832 KB |
Output is correct |
27 |
Correct |
488 ms |
108436 KB |
Output is correct |
28 |
Correct |
421 ms |
100972 KB |
Output is correct |
29 |
Correct |
274 ms |
88280 KB |
Output is correct |
30 |
Correct |
477 ms |
110304 KB |
Output is correct |
31 |
Correct |
290 ms |
88248 KB |
Output is correct |
32 |
Correct |
284 ms |
87812 KB |
Output is correct |
33 |
Correct |
342 ms |
92052 KB |
Output is correct |
34 |
Correct |
265 ms |
88536 KB |
Output is correct |
35 |
Correct |
408 ms |
97412 KB |
Output is correct |
36 |
Correct |
20 ms |
52924 KB |
Output is correct |
37 |
Correct |
99 ms |
70100 KB |
Output is correct |
38 |
Correct |
268 ms |
90908 KB |
Output is correct |
39 |
Correct |
250 ms |
87988 KB |
Output is correct |
40 |
Correct |
379 ms |
94912 KB |
Output is correct |
41 |
Correct |
272 ms |
90568 KB |
Output is correct |
42 |
Correct |
282 ms |
88248 KB |
Output is correct |
43 |
Correct |
113 ms |
67652 KB |
Output is correct |
44 |
Correct |
620 ms |
111748 KB |
Output is correct |
45 |
Correct |
626 ms |
112316 KB |
Output is correct |
46 |
Correct |
683 ms |
111292 KB |
Output is correct |
47 |
Correct |
797 ms |
116156 KB |
Output is correct |
48 |
Correct |
659 ms |
111504 KB |
Output is correct |
49 |
Correct |
267 ms |
81652 KB |
Output is correct |
50 |
Correct |
254 ms |
84992 KB |
Output is correct |
51 |
Correct |
623 ms |
109904 KB |
Output is correct |
52 |
Correct |
307 ms |
89788 KB |
Output is correct |
53 |
Correct |
19 ms |
52316 KB |
Output is correct |
54 |
Correct |
642 ms |
111472 KB |
Output is correct |
55 |
Correct |
496 ms |
110684 KB |
Output is correct |
56 |
Correct |
730 ms |
116848 KB |
Output is correct |
57 |
Correct |
477 ms |
104560 KB |
Output is correct |
58 |
Correct |
499 ms |
106232 KB |
Output is correct |
59 |
Correct |
528 ms |
111040 KB |
Output is correct |
60 |
Correct |
464 ms |
103140 KB |
Output is correct |
61 |
Correct |
510 ms |
107220 KB |
Output is correct |
62 |
Correct |
273 ms |
81380 KB |
Output is correct |
63 |
Correct |
32 ms |
54100 KB |
Output is correct |
64 |
Correct |
393 ms |
100584 KB |
Output is correct |
65 |
Correct |
398 ms |
99784 KB |
Output is correct |
66 |
Correct |
300 ms |
88800 KB |
Output is correct |
67 |
Correct |
333 ms |
95488 KB |
Output is correct |
68 |
Correct |
267 ms |
86416 KB |
Output is correct |
69 |
Correct |
784 ms |
116916 KB |
Output is correct |
70 |
Correct |
464 ms |
110104 KB |
Output is correct |
71 |
Correct |
750 ms |
111600 KB |
Output is correct |
72 |
Correct |
320 ms |
81668 KB |
Output is correct |
73 |
Correct |
727 ms |
110020 KB |
Output is correct |
74 |
Correct |
320 ms |
87012 KB |
Output is correct |
75 |
Correct |
339 ms |
85252 KB |
Output is correct |
76 |
Correct |
394 ms |
91348 KB |
Output is correct |
77 |
Correct |
380 ms |
90916 KB |
Output is correct |
78 |
Correct |
307 ms |
89424 KB |
Output is correct |
79 |
Correct |
293 ms |
86268 KB |
Output is correct |
80 |
Correct |
266 ms |
83384 KB |
Output is correct |
81 |
Correct |
421 ms |
92856 KB |
Output is correct |
82 |
Correct |
279 ms |
87464 KB |
Output is correct |
83 |
Correct |
302 ms |
87396 KB |
Output is correct |
84 |
Correct |
102 ms |
65084 KB |
Output is correct |
85 |
Correct |
693 ms |
117748 KB |
Output is correct |
86 |
Correct |
1110 ms |
144740 KB |
Output is correct |
87 |
Correct |
58 ms |
62376 KB |
Output is correct |
88 |
Correct |
58 ms |
62564 KB |
Output is correct |
89 |
Correct |
54 ms |
62188 KB |
Output is correct |
90 |
Correct |
34 ms |
54728 KB |
Output is correct |
91 |
Correct |
12 ms |
51288 KB |
Output is correct |
92 |
Correct |
25 ms |
53588 KB |
Output is correct |
93 |
Correct |
189 ms |
76348 KB |
Output is correct |
94 |
Correct |
39 ms |
56632 KB |
Output is correct |
95 |
Correct |
474 ms |
106780 KB |
Output is correct |
96 |
Correct |
374 ms |
104316 KB |
Output is correct |
97 |
Correct |
338 ms |
94800 KB |
Output is correct |
98 |
Correct |
330 ms |
98172 KB |
Output is correct |
99 |
Correct |
1493 ms |
179956 KB |
Output is correct |
100 |
Correct |
672 ms |
115696 KB |
Output is correct |
101 |
Correct |
964 ms |
136712 KB |
Output is correct |
102 |
Correct |
308 ms |
85396 KB |
Output is correct |
103 |
Correct |
304 ms |
89596 KB |
Output is correct |
104 |
Correct |
346 ms |
88760 KB |
Output is correct |
105 |
Correct |
365 ms |
92228 KB |
Output is correct |
106 |
Correct |
307 ms |
89712 KB |
Output is correct |
107 |
Correct |
349 ms |
91832 KB |
Output is correct |
108 |
Correct |
42 ms |
58060 KB |
Output is correct |
109 |
Correct |
479 ms |
105772 KB |
Output is correct |
110 |
Correct |
536 ms |
116780 KB |
Output is correct |
111 |
Correct |
530 ms |
118520 KB |
Output is correct |
112 |
Correct |
357 ms |
94908 KB |
Output is correct |
113 |
Correct |
300 ms |
92360 KB |
Output is correct |