#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[100001],V;
vector <int32_t> x,h,l,r,y;
vector <pair <int, int>> ke[12000001],ve;
vector <pair <int32_t, int32_t>> nodes;
int n,m,d[12000001];
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++)
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
288340 KB |
Output is correct |
2 |
Correct |
68 ms |
288340 KB |
Output is correct |
3 |
Correct |
61 ms |
288340 KB |
Output is correct |
4 |
Correct |
62 ms |
288336 KB |
Output is correct |
5 |
Correct |
64 ms |
288376 KB |
Output is correct |
6 |
Correct |
62 ms |
288336 KB |
Output is correct |
7 |
Correct |
65 ms |
288156 KB |
Output is correct |
8 |
Correct |
61 ms |
288316 KB |
Output is correct |
9 |
Correct |
61 ms |
288292 KB |
Output is correct |
10 |
Correct |
62 ms |
288340 KB |
Output is correct |
11 |
Correct |
61 ms |
288320 KB |
Output is correct |
12 |
Correct |
63 ms |
288292 KB |
Output is correct |
13 |
Correct |
62 ms |
288336 KB |
Output is correct |
14 |
Correct |
64 ms |
288340 KB |
Output is correct |
15 |
Correct |
61 ms |
288336 KB |
Output is correct |
16 |
Correct |
61 ms |
288340 KB |
Output is correct |
17 |
Correct |
65 ms |
288340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
288316 KB |
Output is correct |
2 |
Correct |
65 ms |
288340 KB |
Output is correct |
3 |
Runtime error |
406 ms |
625732 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
304732 KB |
Output is correct |
2 |
Correct |
655 ms |
347580 KB |
Output is correct |
3 |
Correct |
687 ms |
348600 KB |
Output is correct |
4 |
Correct |
706 ms |
348608 KB |
Output is correct |
5 |
Correct |
848 ms |
354060 KB |
Output is correct |
6 |
Correct |
804 ms |
349756 KB |
Output is correct |
7 |
Correct |
336 ms |
318824 KB |
Output is correct |
8 |
Correct |
306 ms |
321976 KB |
Output is correct |
9 |
Correct |
691 ms |
348752 KB |
Output is correct |
10 |
Correct |
385 ms |
324796 KB |
Output is correct |
11 |
Correct |
71 ms |
289648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
304732 KB |
Output is correct |
2 |
Correct |
655 ms |
347580 KB |
Output is correct |
3 |
Correct |
687 ms |
348600 KB |
Output is correct |
4 |
Correct |
706 ms |
348608 KB |
Output is correct |
5 |
Correct |
848 ms |
354060 KB |
Output is correct |
6 |
Correct |
804 ms |
349756 KB |
Output is correct |
7 |
Correct |
336 ms |
318824 KB |
Output is correct |
8 |
Correct |
306 ms |
321976 KB |
Output is correct |
9 |
Correct |
691 ms |
348752 KB |
Output is correct |
10 |
Correct |
385 ms |
324796 KB |
Output is correct |
11 |
Correct |
71 ms |
289648 KB |
Output is correct |
12 |
Correct |
689 ms |
348456 KB |
Output is correct |
13 |
Correct |
552 ms |
347616 KB |
Output is correct |
14 |
Correct |
808 ms |
353380 KB |
Output is correct |
15 |
Correct |
572 ms |
342092 KB |
Output is correct |
16 |
Correct |
594 ms |
343740 KB |
Output is correct |
17 |
Correct |
618 ms |
349888 KB |
Output is correct |
18 |
Correct |
525 ms |
340480 KB |
Output is correct |
19 |
Correct |
567 ms |
343484 KB |
Output is correct |
20 |
Correct |
351 ms |
318468 KB |
Output is correct |
21 |
Correct |
81 ms |
291188 KB |
Output is correct |
22 |
Correct |
439 ms |
339640 KB |
Output is correct |
23 |
Correct |
466 ms |
338636 KB |
Output is correct |
24 |
Correct |
337 ms |
326556 KB |
Output is correct |
25 |
Correct |
364 ms |
331036 KB |
Output is correct |
26 |
Correct |
293 ms |
321720 KB |
Output is correct |
27 |
Correct |
805 ms |
353240 KB |
Output is correct |
28 |
Correct |
531 ms |
347180 KB |
Output is correct |
29 |
Correct |
767 ms |
348712 KB |
Output is correct |
30 |
Correct |
358 ms |
318940 KB |
Output is correct |
31 |
Correct |
746 ms |
346576 KB |
Output is correct |
32 |
Correct |
358 ms |
324580 KB |
Output is correct |
33 |
Correct |
344 ms |
322520 KB |
Output is correct |
34 |
Correct |
426 ms |
326452 KB |
Output is correct |
35 |
Correct |
431 ms |
327900 KB |
Output is correct |
36 |
Correct |
358 ms |
326588 KB |
Output is correct |
37 |
Correct |
348 ms |
323560 KB |
Output is correct |
38 |
Correct |
307 ms |
320804 KB |
Output is correct |
39 |
Correct |
440 ms |
329160 KB |
Output is correct |
40 |
Correct |
358 ms |
323564 KB |
Output is correct |
41 |
Correct |
305 ms |
322744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
288340 KB |
Output is correct |
2 |
Correct |
68 ms |
288340 KB |
Output is correct |
3 |
Correct |
61 ms |
288340 KB |
Output is correct |
4 |
Correct |
62 ms |
288336 KB |
Output is correct |
5 |
Correct |
64 ms |
288376 KB |
Output is correct |
6 |
Correct |
62 ms |
288336 KB |
Output is correct |
7 |
Correct |
65 ms |
288156 KB |
Output is correct |
8 |
Correct |
61 ms |
288316 KB |
Output is correct |
9 |
Correct |
61 ms |
288292 KB |
Output is correct |
10 |
Correct |
62 ms |
288340 KB |
Output is correct |
11 |
Correct |
61 ms |
288320 KB |
Output is correct |
12 |
Correct |
63 ms |
288292 KB |
Output is correct |
13 |
Correct |
62 ms |
288336 KB |
Output is correct |
14 |
Correct |
64 ms |
288340 KB |
Output is correct |
15 |
Correct |
61 ms |
288336 KB |
Output is correct |
16 |
Correct |
61 ms |
288340 KB |
Output is correct |
17 |
Correct |
65 ms |
288340 KB |
Output is correct |
18 |
Correct |
61 ms |
288316 KB |
Output is correct |
19 |
Correct |
65 ms |
288340 KB |
Output is correct |
20 |
Runtime error |
406 ms |
625732 KB |
Execution killed with signal 11 |
21 |
Halted |
0 ms |
0 KB |
- |