#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef vector<P> vp;
typedef vector<vp> vvp;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,k,n) for(ll i=(ll)(k);i<(ll)(n);i++)
#define pb emplace_back
#define all(a) a.begin(),a.end()
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
#define SPQ(a) priority_queue<a,vector<a>,greater<a>>
#define fi first
#define se second
#define rsort(a) {sort(all(a));reverse(all(a));}
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> void outvp(T v){rep(i,v.size()){cout<<'('<<v[i].fi<<','<<v[i].se<<')';}cout<<endl;}
const ll inf=1001001001001001001;
ll dijkstra(ll n,vector<PP> edge,ll from,ll to){
vvp g(n);
for(auto x:edge){
ll a,b,c;tie(a,b,c)=x;
g[a].pb(b,c);g[b].pb(a,c);
}
SPQ(P) pq;pq.emplace(0,from);
vi dis(n,inf);dis[from]=0;
while(!pq.empty()){
auto t=pq.top();pq.pop();
if(dis[t.se]!=t.fi)continue;
for(auto x:g[t.se])if(chmin(dis[x.fi],t.fi+x.se))pq.emplace(dis[x.fi],x.fi);
}
if(dis[to]==inf)return -1;
return dis[to];
}
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 from, int to) {
ll n=X.size(),m=l.size();
set<P> S;
vvi L(n),R(n);
rep(i,m)L[l[i]].pb(i);
rep(i,m)R[r[i]].pb(i);
ll cnt=0,F,T;
vvp yoko(m);
vector<PP> edge;
vvp sp2(n);
vp srt(m);
rep(i,m)srt[i]=P(y[i],i);
rsort(srt);
set<ll> S2;
priority_queue<P> pq;
rep(i,n)pq.emplace(h[i],i);
for(auto x:srt){
while(pq.size()&&pq.top().fi>=x.fi){
S2.insert(pq.top().se);pq.pop();
}
auto itr=S2.lower_bound(from);
if(itr!=S2.begin())itr--;
rep(t,3){
if(itr==S2.end())break;
if(l[x.se]<=*itr&&*itr<=r[x.se])sp2[*itr].pb(x);
itr++;
}
itr=S2.lower_bound(to);
if(itr!=S2.begin())itr--;
rep(t,3){
if(itr==S2.end())break;
if(l[x.se]<=*itr&&*itr<=r[x.se])sp2[*itr].pb(x);
itr++;
}
}
rep(i,n){
vp sp;
for(ll t:L[i])S.insert(P(y[t],t));
for(ll t:L[i]){
sp.pb(y[t],t);
auto itr=S.lower_bound(P(y[t],t));
if(itr!=S.begin()){
itr--;sp.pb(*itr);
}
}
for(ll t:R[i]){
sp.pb(y[t],t);
auto itr=S.lower_bound(P(y[t],t));
if(itr!=S.begin()){
itr--;sp.pb(*itr);
}
}
if(S.size())sp.pb(*S.begin());
sp.pb(0,-1);
for(auto x:sp2[i])sp.pb(x);
for(ll t:R[i])S.erase(P(y[t],t));
dupli(sp);
while(sp.size()&&sp.back().fi>h[i])sp.pop_back();
rep(j,sp.size()-1){
edge.pb(cnt+j,cnt+j+1,sp[j+1].fi-sp[j].fi);
}
REP(j,1,sp.size())yoko[sp[j].se].pb(i,cnt+j);
if(from==i)F=cnt;
if(to==i)T=cnt;
cnt+=sp.size();
}
rep(i,m)rep(j,yoko[i].size()-1)edge.pb(yoko[i][j].se,yoko[i][j+1].se,X[yoko[i][j+1].fi]-X[yoko[i][j].fi]);
return dijkstra(cnt,edge,F,T);
}
Compilation message
walk.cpp: In function 'long long int min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:107:17: warning: 'T' may be used uninitialized in this function [-Wmaybe-uninitialized]
107 | return dijkstra(cnt,edge,F,T);
| ~~~~~~~~^~~~~~~~~~~~~~
walk.cpp:107:17: warning: 'F' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
296 KB |
Output is correct |
9 |
Correct |
1 ms |
296 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
296 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
497 ms |
89020 KB |
Output is correct |
4 |
Correct |
528 ms |
122640 KB |
Output is correct |
5 |
Correct |
344 ms |
99856 KB |
Output is correct |
6 |
Correct |
347 ms |
95116 KB |
Output is correct |
7 |
Correct |
376 ms |
99968 KB |
Output is correct |
8 |
Correct |
496 ms |
93264 KB |
Output is correct |
9 |
Correct |
447 ms |
108064 KB |
Output is correct |
10 |
Correct |
567 ms |
125604 KB |
Output is correct |
11 |
Correct |
401 ms |
82732 KB |
Output is correct |
12 |
Correct |
315 ms |
78656 KB |
Output is correct |
13 |
Correct |
583 ms |
129888 KB |
Output is correct |
14 |
Correct |
346 ms |
77168 KB |
Output is correct |
15 |
Correct |
357 ms |
79516 KB |
Output is correct |
16 |
Correct |
344 ms |
77496 KB |
Output is correct |
17 |
Correct |
296 ms |
74744 KB |
Output is correct |
18 |
Correct |
408 ms |
82292 KB |
Output is correct |
19 |
Correct |
10 ms |
4112 KB |
Output is correct |
20 |
Correct |
116 ms |
38656 KB |
Output is correct |
21 |
Correct |
300 ms |
71024 KB |
Output is correct |
22 |
Correct |
311 ms |
77784 KB |
Output is correct |
23 |
Correct |
498 ms |
92692 KB |
Output is correct |
24 |
Correct |
320 ms |
76456 KB |
Output is correct |
25 |
Correct |
310 ms |
74636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
127 ms |
29504 KB |
Output is correct |
2 |
Correct |
574 ms |
98112 KB |
Output is correct |
3 |
Correct |
537 ms |
104112 KB |
Output is correct |
4 |
Correct |
754 ms |
144972 KB |
Output is correct |
5 |
Correct |
996 ms |
147928 KB |
Output is correct |
6 |
Correct |
865 ms |
147608 KB |
Output is correct |
7 |
Correct |
396 ms |
95736 KB |
Output is correct |
8 |
Correct |
317 ms |
74524 KB |
Output is correct |
9 |
Correct |
921 ms |
147692 KB |
Output is correct |
10 |
Correct |
471 ms |
100120 KB |
Output is correct |
11 |
Correct |
37 ms |
10596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
127 ms |
29504 KB |
Output is correct |
2 |
Correct |
574 ms |
98112 KB |
Output is correct |
3 |
Correct |
537 ms |
104112 KB |
Output is correct |
4 |
Correct |
754 ms |
144972 KB |
Output is correct |
5 |
Correct |
996 ms |
147928 KB |
Output is correct |
6 |
Correct |
865 ms |
147608 KB |
Output is correct |
7 |
Correct |
396 ms |
95736 KB |
Output is correct |
8 |
Correct |
317 ms |
74524 KB |
Output is correct |
9 |
Correct |
921 ms |
147692 KB |
Output is correct |
10 |
Correct |
471 ms |
100120 KB |
Output is correct |
11 |
Correct |
37 ms |
10596 KB |
Output is correct |
12 |
Correct |
554 ms |
104008 KB |
Output is correct |
13 |
Correct |
617 ms |
143408 KB |
Output is correct |
14 |
Correct |
922 ms |
147488 KB |
Output is correct |
15 |
Correct |
607 ms |
115632 KB |
Output is correct |
16 |
Correct |
635 ms |
128384 KB |
Output is correct |
17 |
Correct |
688 ms |
144796 KB |
Output is correct |
18 |
Correct |
633 ms |
115556 KB |
Output is correct |
19 |
Correct |
634 ms |
127920 KB |
Output is correct |
20 |
Correct |
422 ms |
94036 KB |
Output is correct |
21 |
Correct |
140 ms |
47564 KB |
Output is correct |
22 |
Correct |
450 ms |
122852 KB |
Output is correct |
23 |
Correct |
439 ms |
116832 KB |
Output is correct |
24 |
Correct |
334 ms |
87104 KB |
Output is correct |
25 |
Correct |
396 ms |
107100 KB |
Output is correct |
26 |
Correct |
303 ms |
69180 KB |
Output is correct |
27 |
Correct |
986 ms |
146828 KB |
Output is correct |
28 |
Correct |
564 ms |
142560 KB |
Output is correct |
29 |
Correct |
848 ms |
146228 KB |
Output is correct |
30 |
Correct |
404 ms |
95264 KB |
Output is correct |
31 |
Correct |
864 ms |
146052 KB |
Output is correct |
32 |
Correct |
453 ms |
83532 KB |
Output is correct |
33 |
Correct |
347 ms |
83340 KB |
Output is correct |
34 |
Correct |
451 ms |
91132 KB |
Output is correct |
35 |
Correct |
399 ms |
90752 KB |
Output is correct |
36 |
Correct |
337 ms |
78584 KB |
Output is correct |
37 |
Correct |
283 ms |
68312 KB |
Output is correct |
38 |
Correct |
332 ms |
74624 KB |
Output is correct |
39 |
Correct |
499 ms |
89908 KB |
Output is correct |
40 |
Correct |
320 ms |
73556 KB |
Output is correct |
41 |
Correct |
330 ms |
71848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
296 KB |
Output is correct |
9 |
Correct |
1 ms |
296 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
296 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
296 KB |
Output is correct |
20 |
Correct |
497 ms |
89020 KB |
Output is correct |
21 |
Correct |
528 ms |
122640 KB |
Output is correct |
22 |
Correct |
344 ms |
99856 KB |
Output is correct |
23 |
Correct |
347 ms |
95116 KB |
Output is correct |
24 |
Correct |
376 ms |
99968 KB |
Output is correct |
25 |
Correct |
496 ms |
93264 KB |
Output is correct |
26 |
Correct |
447 ms |
108064 KB |
Output is correct |
27 |
Correct |
567 ms |
125604 KB |
Output is correct |
28 |
Correct |
401 ms |
82732 KB |
Output is correct |
29 |
Correct |
315 ms |
78656 KB |
Output is correct |
30 |
Correct |
583 ms |
129888 KB |
Output is correct |
31 |
Correct |
346 ms |
77168 KB |
Output is correct |
32 |
Correct |
357 ms |
79516 KB |
Output is correct |
33 |
Correct |
344 ms |
77496 KB |
Output is correct |
34 |
Correct |
296 ms |
74744 KB |
Output is correct |
35 |
Correct |
408 ms |
82292 KB |
Output is correct |
36 |
Correct |
10 ms |
4112 KB |
Output is correct |
37 |
Correct |
116 ms |
38656 KB |
Output is correct |
38 |
Correct |
300 ms |
71024 KB |
Output is correct |
39 |
Correct |
311 ms |
77784 KB |
Output is correct |
40 |
Correct |
498 ms |
92692 KB |
Output is correct |
41 |
Correct |
320 ms |
76456 KB |
Output is correct |
42 |
Correct |
310 ms |
74636 KB |
Output is correct |
43 |
Correct |
127 ms |
29504 KB |
Output is correct |
44 |
Correct |
574 ms |
98112 KB |
Output is correct |
45 |
Correct |
537 ms |
104112 KB |
Output is correct |
46 |
Correct |
754 ms |
144972 KB |
Output is correct |
47 |
Correct |
996 ms |
147928 KB |
Output is correct |
48 |
Correct |
865 ms |
147608 KB |
Output is correct |
49 |
Correct |
396 ms |
95736 KB |
Output is correct |
50 |
Correct |
317 ms |
74524 KB |
Output is correct |
51 |
Correct |
921 ms |
147692 KB |
Output is correct |
52 |
Correct |
471 ms |
100120 KB |
Output is correct |
53 |
Correct |
37 ms |
10596 KB |
Output is correct |
54 |
Correct |
554 ms |
104008 KB |
Output is correct |
55 |
Correct |
617 ms |
143408 KB |
Output is correct |
56 |
Correct |
922 ms |
147488 KB |
Output is correct |
57 |
Correct |
607 ms |
115632 KB |
Output is correct |
58 |
Correct |
635 ms |
128384 KB |
Output is correct |
59 |
Correct |
688 ms |
144796 KB |
Output is correct |
60 |
Correct |
633 ms |
115556 KB |
Output is correct |
61 |
Correct |
634 ms |
127920 KB |
Output is correct |
62 |
Correct |
422 ms |
94036 KB |
Output is correct |
63 |
Correct |
140 ms |
47564 KB |
Output is correct |
64 |
Correct |
450 ms |
122852 KB |
Output is correct |
65 |
Correct |
439 ms |
116832 KB |
Output is correct |
66 |
Correct |
334 ms |
87104 KB |
Output is correct |
67 |
Correct |
396 ms |
107100 KB |
Output is correct |
68 |
Correct |
303 ms |
69180 KB |
Output is correct |
69 |
Correct |
986 ms |
146828 KB |
Output is correct |
70 |
Correct |
564 ms |
142560 KB |
Output is correct |
71 |
Correct |
848 ms |
146228 KB |
Output is correct |
72 |
Correct |
404 ms |
95264 KB |
Output is correct |
73 |
Correct |
864 ms |
146052 KB |
Output is correct |
74 |
Correct |
453 ms |
83532 KB |
Output is correct |
75 |
Correct |
347 ms |
83340 KB |
Output is correct |
76 |
Correct |
451 ms |
91132 KB |
Output is correct |
77 |
Correct |
399 ms |
90752 KB |
Output is correct |
78 |
Correct |
337 ms |
78584 KB |
Output is correct |
79 |
Correct |
283 ms |
68312 KB |
Output is correct |
80 |
Correct |
332 ms |
74624 KB |
Output is correct |
81 |
Correct |
499 ms |
89908 KB |
Output is correct |
82 |
Correct |
320 ms |
73556 KB |
Output is correct |
83 |
Correct |
330 ms |
71848 KB |
Output is correct |
84 |
Correct |
103 ms |
24316 KB |
Output is correct |
85 |
Correct |
584 ms |
114384 KB |
Output is correct |
86 |
Correct |
1144 ms |
208968 KB |
Output is correct |
87 |
Correct |
144 ms |
55448 KB |
Output is correct |
88 |
Correct |
179 ms |
56748 KB |
Output is correct |
89 |
Correct |
176 ms |
55488 KB |
Output is correct |
90 |
Correct |
29 ms |
10148 KB |
Output is correct |
91 |
Correct |
2 ms |
952 KB |
Output is correct |
92 |
Correct |
30 ms |
8620 KB |
Output is correct |
93 |
Correct |
356 ms |
83480 KB |
Output is correct |
94 |
Correct |
137 ms |
50124 KB |
Output is correct |
95 |
Correct |
496 ms |
131188 KB |
Output is correct |
96 |
Correct |
432 ms |
121544 KB |
Output is correct |
97 |
Correct |
362 ms |
93772 KB |
Output is correct |
98 |
Correct |
381 ms |
110820 KB |
Output is correct |
99 |
Correct |
1515 ms |
264060 KB |
Output is correct |
100 |
Correct |
718 ms |
150240 KB |
Output is correct |
101 |
Correct |
1036 ms |
190708 KB |
Output is correct |
102 |
Correct |
461 ms |
99680 KB |
Output is correct |
103 |
Correct |
338 ms |
83560 KB |
Output is correct |
104 |
Correct |
337 ms |
83156 KB |
Output is correct |
105 |
Correct |
440 ms |
90396 KB |
Output is correct |
106 |
Correct |
363 ms |
87376 KB |
Output is correct |
107 |
Correct |
420 ms |
85168 KB |
Output is correct |
108 |
Correct |
36 ms |
12396 KB |
Output is correct |
109 |
Correct |
676 ms |
123392 KB |
Output is correct |
110 |
Correct |
606 ms |
147488 KB |
Output is correct |
111 |
Correct |
583 ms |
149888 KB |
Output is correct |
112 |
Correct |
405 ms |
92448 KB |
Output is correct |
113 |
Correct |
398 ms |
87988 KB |
Output is correct |