#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[100100];
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]]=max(lft[h[i]],i);
for(int i=pos;i<n;i++)
if(rt.count(h[i]))
rt[h[i]]=min(rt[h[i]],i);
else rt[h[i]]=i;
auto it=lft.end();
while(--it!=lft.begin())
if(it->second>=prev(it)->second)
it=lft.erase(prev(it));
it=rt.end();
while(--it!=rt.begin())
if(it->second<=prev(it)->second)
it=rt.erase(prev(it));
}
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 |
6 ms |
42584 KB |
Output is correct |
2 |
Correct |
5 ms |
42588 KB |
Output is correct |
3 |
Correct |
5 ms |
42588 KB |
Output is correct |
4 |
Correct |
5 ms |
42588 KB |
Output is correct |
5 |
Runtime error |
26 ms |
73564 KB |
Execution killed with signal 6 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
42588 KB |
Output is correct |
2 |
Correct |
5 ms |
42588 KB |
Output is correct |
3 |
Runtime error |
44 ms |
79660 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
53956 KB |
Output is correct |
2 |
Correct |
689 ms |
111016 KB |
Output is correct |
3 |
Correct |
629 ms |
111264 KB |
Output is correct |
4 |
Correct |
557 ms |
108580 KB |
Output is correct |
5 |
Correct |
614 ms |
110532 KB |
Output is correct |
6 |
Correct |
564 ms |
105668 KB |
Output is correct |
7 |
Correct |
235 ms |
74952 KB |
Output is correct |
8 |
Correct |
135 ms |
68292 KB |
Output is correct |
9 |
Correct |
574 ms |
100624 KB |
Output is correct |
10 |
Correct |
294 ms |
73928 KB |
Output is correct |
11 |
Correct |
11 ms |
43416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
53956 KB |
Output is correct |
2 |
Correct |
689 ms |
111016 KB |
Output is correct |
3 |
Correct |
629 ms |
111264 KB |
Output is correct |
4 |
Correct |
557 ms |
108580 KB |
Output is correct |
5 |
Correct |
614 ms |
110532 KB |
Output is correct |
6 |
Correct |
564 ms |
105668 KB |
Output is correct |
7 |
Correct |
235 ms |
74952 KB |
Output is correct |
8 |
Correct |
135 ms |
68292 KB |
Output is correct |
9 |
Correct |
574 ms |
100624 KB |
Output is correct |
10 |
Correct |
294 ms |
73928 KB |
Output is correct |
11 |
Correct |
11 ms |
43416 KB |
Output is correct |
12 |
Correct |
627 ms |
111364 KB |
Output is correct |
13 |
Correct |
523 ms |
109604 KB |
Output is correct |
14 |
Correct |
665 ms |
113448 KB |
Output is correct |
15 |
Correct |
410 ms |
99008 KB |
Output is correct |
16 |
Correct |
449 ms |
98336 KB |
Output is correct |
17 |
Correct |
521 ms |
110016 KB |
Output is correct |
18 |
Correct |
396 ms |
99008 KB |
Output is correct |
19 |
Correct |
452 ms |
98332 KB |
Output is correct |
20 |
Correct |
298 ms |
76916 KB |
Output is correct |
21 |
Correct |
81 ms |
49312 KB |
Output is correct |
22 |
Correct |
386 ms |
95680 KB |
Output is correct |
23 |
Correct |
324 ms |
92708 KB |
Output is correct |
24 |
Correct |
214 ms |
78656 KB |
Output is correct |
25 |
Correct |
301 ms |
89064 KB |
Output is correct |
26 |
Correct |
171 ms |
70080 KB |
Output is correct |
27 |
Correct |
666 ms |
112228 KB |
Output is correct |
28 |
Correct |
501 ms |
109140 KB |
Output is correct |
29 |
Correct |
652 ms |
105920 KB |
Output is correct |
30 |
Correct |
288 ms |
78024 KB |
Output is correct |
31 |
Correct |
647 ms |
103616 KB |
Output is correct |
32 |
Correct |
222 ms |
72436 KB |
Output is correct |
33 |
Correct |
201 ms |
71100 KB |
Output is correct |
34 |
Correct |
309 ms |
77760 KB |
Output is correct |
35 |
Correct |
267 ms |
79828 KB |
Output is correct |
36 |
Correct |
196 ms |
73956 KB |
Output is correct |
37 |
Correct |
157 ms |
70944 KB |
Output is correct |
38 |
Correct |
172 ms |
68388 KB |
Output is correct |
39 |
Correct |
374 ms |
82368 KB |
Output is correct |
40 |
Correct |
168 ms |
70564 KB |
Output is correct |
41 |
Correct |
217 ms |
70900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
42584 KB |
Output is correct |
2 |
Correct |
5 ms |
42588 KB |
Output is correct |
3 |
Correct |
5 ms |
42588 KB |
Output is correct |
4 |
Correct |
5 ms |
42588 KB |
Output is correct |
5 |
Runtime error |
26 ms |
73564 KB |
Execution killed with signal 6 |
6 |
Halted |
0 ms |
0 KB |
- |