#include "walk.h"
#include<bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
map<int,int>pos[100100];
vector<pair<int,int>> adj[1<<20];
int CC;
void AE(int a,int b,int c){
adj[a].push_back({b,c});
adj[b].push_back({a,c});
}
int whatpos(int i,int j){
if(pos[i].count(j))
return pos[i][j];
pos[i][j]=++CC;
auto it=pos[i].find(j);
if(it!=pos[i].begin())
AE(prev(it)->second,CC,it->first-prev(it)->first);
if(next(it)!=pos[i].end())
AE(next(it)->second,CC,next(it)->first-it->first);
return CC;
}
long long dis[1<<20];
long long djik(int a,int b){
memset(dis,9,sizeof dis);
priority_queue<pair<long long,int>,vector<pair<long long,int>>,greater<>> pq;
pq.push({0,a});
dis[a]=0;
while(pq.size()){
auto[d,x]=pq.top();
pq.pop();
if(d-dis[x])continue;
for(auto[v,w]:adj[x])
if(dis[v]>d+w)
pq.push({dis[v]=d+w,v});
}
return dis[b]<dis[0]?dis[b]:-1;
}
set<int>intersection[1<<20];
map<int,int>lft,rt;
void makestuf(VI h,int pos){
lft.clear();
rt.clear();
int n=h.size();
for(int i=0;i<=pos;i++) {
lft[h[i]]=i;
while(lft.begin()->first<h[i])
lft.erase(lft.begin());
}
for(int i=n;i-->pos;){
rt[h[i]]=i;
while(rt.begin()->first<h[i])
rt.erase(rt.begin());
}
}
long long min_distance(VI x,VI h,VI l,VI r,VI y,int s,int g) {
int m=l.size();
makestuf(h,s);
for(int i=0;i<m;i++)
if(l[i]<s&&r[i]>s) {
int A=lft.lower_bound(y[i])->second;
int B=rt.lower_bound(y[i])->second;
assert(A>=l[i]);
assert(B<=r[i]);
if(A-l[i])
l.push_back(l[i]),
r.push_back(l[i]=A),
y.push_back(y[i]);
if(B-r[i])
r.push_back(r[i]),
l.push_back(r[i]=B),
y.push_back(y[i]);
}
m=l.size();
makestuf(h,g);
for(int i=0;i<m;i++)
if(l[i]<g&&r[i]>g) {
int A=lft.lower_bound(y[i])->second;
int B=rt.lower_bound(y[i])->second;
assert(A>=l[i]);
assert(B<=r[i]);
if(A-l[i])
l.push_back(l[i]),
r.push_back(l[i]=A),
y.push_back(y[i]);
if(B-r[i])
r.push_back(r[i]),
l.push_back(r[i]=B),
y.push_back(y[i]);
}
m=l.size();
vector<pair<int,int>> endpts;
for(int i=0;i<m;i++) if(l[i]-r[i])
endpts.push_back({l[i],i}),
endpts.push_back({r[i],i}),
whatpos(l[i],y[i]),whatpos(r[i],y[i]);
sort(endpts.begin(),endpts.end());
set<pair<int,int>> active;
for(auto[b,i]:endpts){
int pos=y[i];
if(active.lower_bound({pos,0})!=active.begin()){
auto it=--active.lower_bound({pos,0});
intersection[it->second].insert(b);
whatpos(b,it->first);
}
if(active.count({pos,i}))
active.erase({pos,i});
else active.insert({pos,i});
}
for(int i=0;i<m;i++){
if(l[i]==r[i])continue;
intersection[i].erase(l[i]);
intersection[i].insert(r[i]);
int prvind=whatpos(l[i],y[i]),prvpos=x[l[i]];
for(auto j:intersection[i])
AE(prvind,whatpos(j,y[i]),x[j]-prvpos),
prvind=whatpos(j,y[i]),prvpos=x[j];
}
return djik(whatpos(s,0),whatpos(g,0));
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
87132 KB |
Output is correct |
2 |
Correct |
11 ms |
87132 KB |
Output is correct |
3 |
Correct |
11 ms |
87164 KB |
Output is correct |
4 |
Correct |
11 ms |
87132 KB |
Output is correct |
5 |
Correct |
11 ms |
87132 KB |
Output is correct |
6 |
Correct |
11 ms |
87180 KB |
Output is correct |
7 |
Correct |
12 ms |
87132 KB |
Output is correct |
8 |
Correct |
14 ms |
87188 KB |
Output is correct |
9 |
Correct |
13 ms |
87132 KB |
Output is correct |
10 |
Correct |
11 ms |
87132 KB |
Output is correct |
11 |
Correct |
13 ms |
87172 KB |
Output is correct |
12 |
Correct |
12 ms |
87132 KB |
Output is correct |
13 |
Correct |
12 ms |
87132 KB |
Output is correct |
14 |
Correct |
12 ms |
87132 KB |
Output is correct |
15 |
Correct |
11 ms |
87132 KB |
Output is correct |
16 |
Correct |
11 ms |
87128 KB |
Output is correct |
17 |
Correct |
11 ms |
87128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
87132 KB |
Output is correct |
2 |
Correct |
11 ms |
87208 KB |
Output is correct |
3 |
Correct |
500 ms |
144560 KB |
Output is correct |
4 |
Correct |
368 ms |
142916 KB |
Output is correct |
5 |
Correct |
227 ms |
129160 KB |
Output is correct |
6 |
Correct |
233 ms |
127132 KB |
Output is correct |
7 |
Correct |
256 ms |
129216 KB |
Output is correct |
8 |
Correct |
516 ms |
147868 KB |
Output is correct |
9 |
Correct |
312 ms |
139888 KB |
Output is correct |
10 |
Correct |
380 ms |
144684 KB |
Output is correct |
11 |
Correct |
307 ms |
133128 KB |
Output is correct |
12 |
Correct |
146 ms |
116928 KB |
Output is correct |
13 |
Correct |
373 ms |
145816 KB |
Output is correct |
14 |
Correct |
197 ms |
118792 KB |
Output is correct |
15 |
Correct |
203 ms |
118644 KB |
Output is correct |
16 |
Correct |
183 ms |
120008 KB |
Output is correct |
17 |
Correct |
181 ms |
118404 KB |
Output is correct |
18 |
Correct |
461 ms |
127684 KB |
Output is correct |
19 |
Correct |
17 ms |
88692 KB |
Output is correct |
20 |
Correct |
65 ms |
102636 KB |
Output is correct |
21 |
Correct |
162 ms |
118336 KB |
Output is correct |
22 |
Correct |
136 ms |
116156 KB |
Output is correct |
23 |
Correct |
353 ms |
127424 KB |
Output is correct |
24 |
Correct |
172 ms |
117984 KB |
Output is correct |
25 |
Correct |
174 ms |
118260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
99012 KB |
Output is correct |
2 |
Correct |
669 ms |
157376 KB |
Output is correct |
3 |
Correct |
709 ms |
157632 KB |
Output is correct |
4 |
Correct |
565 ms |
156952 KB |
Output is correct |
5 |
Correct |
619 ms |
159104 KB |
Output is correct |
6 |
Correct |
620 ms |
154400 KB |
Output is correct |
7 |
Correct |
239 ms |
122344 KB |
Output is correct |
8 |
Correct |
216 ms |
116932 KB |
Output is correct |
9 |
Correct |
618 ms |
149140 KB |
Output is correct |
10 |
Correct |
327 ms |
121872 KB |
Output is correct |
11 |
Correct |
17 ms |
88984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
99012 KB |
Output is correct |
2 |
Correct |
669 ms |
157376 KB |
Output is correct |
3 |
Correct |
709 ms |
157632 KB |
Output is correct |
4 |
Correct |
565 ms |
156952 KB |
Output is correct |
5 |
Correct |
619 ms |
159104 KB |
Output is correct |
6 |
Correct |
620 ms |
154400 KB |
Output is correct |
7 |
Correct |
239 ms |
122344 KB |
Output is correct |
8 |
Correct |
216 ms |
116932 KB |
Output is correct |
9 |
Correct |
618 ms |
149140 KB |
Output is correct |
10 |
Correct |
327 ms |
121872 KB |
Output is correct |
11 |
Correct |
17 ms |
88984 KB |
Output is correct |
12 |
Correct |
723 ms |
157500 KB |
Output is correct |
13 |
Correct |
467 ms |
155032 KB |
Output is correct |
14 |
Correct |
695 ms |
158888 KB |
Output is correct |
15 |
Correct |
433 ms |
147396 KB |
Output is correct |
16 |
Correct |
418 ms |
143808 KB |
Output is correct |
17 |
Correct |
484 ms |
155588 KB |
Output is correct |
18 |
Correct |
412 ms |
147392 KB |
Output is correct |
19 |
Correct |
398 ms |
143812 KB |
Output is correct |
20 |
Correct |
260 ms |
121540 KB |
Output is correct |
21 |
Correct |
34 ms |
91260 KB |
Output is correct |
22 |
Correct |
373 ms |
141752 KB |
Output is correct |
23 |
Correct |
318 ms |
138948 KB |
Output is correct |
24 |
Correct |
245 ms |
124608 KB |
Output is correct |
25 |
Correct |
319 ms |
134084 KB |
Output is correct |
26 |
Correct |
151 ms |
115648 KB |
Output is correct |
27 |
Correct |
744 ms |
158704 KB |
Output is correct |
28 |
Correct |
528 ms |
154548 KB |
Output is correct |
29 |
Correct |
679 ms |
153024 KB |
Output is correct |
30 |
Correct |
239 ms |
122592 KB |
Output is correct |
31 |
Correct |
649 ms |
149188 KB |
Output is correct |
32 |
Correct |
218 ms |
120044 KB |
Output is correct |
33 |
Correct |
252 ms |
118972 KB |
Output is correct |
34 |
Correct |
337 ms |
124056 KB |
Output is correct |
35 |
Correct |
268 ms |
126688 KB |
Output is correct |
36 |
Correct |
200 ms |
121056 KB |
Output is correct |
37 |
Correct |
174 ms |
118316 KB |
Output is correct |
38 |
Correct |
156 ms |
116164 KB |
Output is correct |
39 |
Correct |
346 ms |
127424 KB |
Output is correct |
40 |
Correct |
152 ms |
117984 KB |
Output is correct |
41 |
Correct |
158 ms |
118188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
87132 KB |
Output is correct |
2 |
Correct |
11 ms |
87132 KB |
Output is correct |
3 |
Correct |
11 ms |
87164 KB |
Output is correct |
4 |
Correct |
11 ms |
87132 KB |
Output is correct |
5 |
Correct |
11 ms |
87132 KB |
Output is correct |
6 |
Correct |
11 ms |
87180 KB |
Output is correct |
7 |
Correct |
12 ms |
87132 KB |
Output is correct |
8 |
Correct |
14 ms |
87188 KB |
Output is correct |
9 |
Correct |
13 ms |
87132 KB |
Output is correct |
10 |
Correct |
11 ms |
87132 KB |
Output is correct |
11 |
Correct |
13 ms |
87172 KB |
Output is correct |
12 |
Correct |
12 ms |
87132 KB |
Output is correct |
13 |
Correct |
12 ms |
87132 KB |
Output is correct |
14 |
Correct |
12 ms |
87132 KB |
Output is correct |
15 |
Correct |
11 ms |
87132 KB |
Output is correct |
16 |
Correct |
11 ms |
87128 KB |
Output is correct |
17 |
Correct |
11 ms |
87128 KB |
Output is correct |
18 |
Correct |
11 ms |
87132 KB |
Output is correct |
19 |
Correct |
11 ms |
87208 KB |
Output is correct |
20 |
Correct |
500 ms |
144560 KB |
Output is correct |
21 |
Correct |
368 ms |
142916 KB |
Output is correct |
22 |
Correct |
227 ms |
129160 KB |
Output is correct |
23 |
Correct |
233 ms |
127132 KB |
Output is correct |
24 |
Correct |
256 ms |
129216 KB |
Output is correct |
25 |
Correct |
516 ms |
147868 KB |
Output is correct |
26 |
Correct |
312 ms |
139888 KB |
Output is correct |
27 |
Correct |
380 ms |
144684 KB |
Output is correct |
28 |
Correct |
307 ms |
133128 KB |
Output is correct |
29 |
Correct |
146 ms |
116928 KB |
Output is correct |
30 |
Correct |
373 ms |
145816 KB |
Output is correct |
31 |
Correct |
197 ms |
118792 KB |
Output is correct |
32 |
Correct |
203 ms |
118644 KB |
Output is correct |
33 |
Correct |
183 ms |
120008 KB |
Output is correct |
34 |
Correct |
181 ms |
118404 KB |
Output is correct |
35 |
Correct |
461 ms |
127684 KB |
Output is correct |
36 |
Correct |
17 ms |
88692 KB |
Output is correct |
37 |
Correct |
65 ms |
102636 KB |
Output is correct |
38 |
Correct |
162 ms |
118336 KB |
Output is correct |
39 |
Correct |
136 ms |
116156 KB |
Output is correct |
40 |
Correct |
353 ms |
127424 KB |
Output is correct |
41 |
Correct |
172 ms |
117984 KB |
Output is correct |
42 |
Correct |
174 ms |
118260 KB |
Output is correct |
43 |
Correct |
70 ms |
99012 KB |
Output is correct |
44 |
Correct |
669 ms |
157376 KB |
Output is correct |
45 |
Correct |
709 ms |
157632 KB |
Output is correct |
46 |
Correct |
565 ms |
156952 KB |
Output is correct |
47 |
Correct |
619 ms |
159104 KB |
Output is correct |
48 |
Correct |
620 ms |
154400 KB |
Output is correct |
49 |
Correct |
239 ms |
122344 KB |
Output is correct |
50 |
Correct |
216 ms |
116932 KB |
Output is correct |
51 |
Correct |
618 ms |
149140 KB |
Output is correct |
52 |
Correct |
327 ms |
121872 KB |
Output is correct |
53 |
Correct |
17 ms |
88984 KB |
Output is correct |
54 |
Correct |
723 ms |
157500 KB |
Output is correct |
55 |
Correct |
467 ms |
155032 KB |
Output is correct |
56 |
Correct |
695 ms |
158888 KB |
Output is correct |
57 |
Correct |
433 ms |
147396 KB |
Output is correct |
58 |
Correct |
418 ms |
143808 KB |
Output is correct |
59 |
Correct |
484 ms |
155588 KB |
Output is correct |
60 |
Correct |
412 ms |
147392 KB |
Output is correct |
61 |
Correct |
398 ms |
143812 KB |
Output is correct |
62 |
Correct |
260 ms |
121540 KB |
Output is correct |
63 |
Correct |
34 ms |
91260 KB |
Output is correct |
64 |
Correct |
373 ms |
141752 KB |
Output is correct |
65 |
Correct |
318 ms |
138948 KB |
Output is correct |
66 |
Correct |
245 ms |
124608 KB |
Output is correct |
67 |
Correct |
319 ms |
134084 KB |
Output is correct |
68 |
Correct |
151 ms |
115648 KB |
Output is correct |
69 |
Correct |
744 ms |
158704 KB |
Output is correct |
70 |
Correct |
528 ms |
154548 KB |
Output is correct |
71 |
Correct |
679 ms |
153024 KB |
Output is correct |
72 |
Correct |
239 ms |
122592 KB |
Output is correct |
73 |
Correct |
649 ms |
149188 KB |
Output is correct |
74 |
Correct |
218 ms |
120044 KB |
Output is correct |
75 |
Correct |
252 ms |
118972 KB |
Output is correct |
76 |
Correct |
337 ms |
124056 KB |
Output is correct |
77 |
Correct |
268 ms |
126688 KB |
Output is correct |
78 |
Correct |
200 ms |
121056 KB |
Output is correct |
79 |
Correct |
174 ms |
118316 KB |
Output is correct |
80 |
Correct |
156 ms |
116164 KB |
Output is correct |
81 |
Correct |
346 ms |
127424 KB |
Output is correct |
82 |
Correct |
152 ms |
117984 KB |
Output is correct |
83 |
Correct |
158 ms |
118188 KB |
Output is correct |
84 |
Correct |
57 ms |
96640 KB |
Output is correct |
85 |
Correct |
667 ms |
160592 KB |
Output is correct |
86 |
Correct |
969 ms |
178972 KB |
Output is correct |
87 |
Correct |
43 ms |
95944 KB |
Output is correct |
88 |
Correct |
48 ms |
95432 KB |
Output is correct |
89 |
Correct |
44 ms |
96020 KB |
Output is correct |
90 |
Correct |
26 ms |
90316 KB |
Output is correct |
91 |
Correct |
13 ms |
87132 KB |
Output is correct |
92 |
Correct |
23 ms |
89692 KB |
Output is correct |
93 |
Correct |
158 ms |
111832 KB |
Output is correct |
94 |
Correct |
37 ms |
91564 KB |
Output is correct |
95 |
Correct |
383 ms |
144832 KB |
Output is correct |
96 |
Correct |
303 ms |
139456 KB |
Output is correct |
97 |
Correct |
228 ms |
126144 KB |
Output is correct |
98 |
Correct |
266 ms |
134148 KB |
Output is correct |
99 |
Correct |
1593 ms |
202404 KB |
Output is correct |
100 |
Correct |
565 ms |
157200 KB |
Output is correct |
101 |
Correct |
877 ms |
167864 KB |
Output is correct |
102 |
Correct |
236 ms |
122824 KB |
Output is correct |
103 |
Correct |
212 ms |
120052 KB |
Output is correct |
104 |
Correct |
217 ms |
118716 KB |
Output is correct |
105 |
Correct |
301 ms |
124212 KB |
Output is correct |
106 |
Correct |
259 ms |
121268 KB |
Output is correct |
107 |
Correct |
266 ms |
124328 KB |
Output is correct |
108 |
Correct |
35 ms |
92248 KB |
Output is correct |
109 |
Correct |
415 ms |
140516 KB |
Output is correct |
110 |
Correct |
483 ms |
155780 KB |
Output is correct |
111 |
Correct |
449 ms |
157972 KB |
Output is correct |
112 |
Correct |
265 ms |
126424 KB |
Output is correct |
113 |
Correct |
226 ms |
124156 KB |
Output is correct |