#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[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());
set <pair <int, int>> cur;
for (auto [i,j]:ve){
j=abs(j);
add(i,j);
b[j].push_back(i);
if (i==l[j])
cur.insert({y[j],j});
else
cur.erase({y[j],j});
auto it=cur.lower_bound(make_pair(y[i],-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=abs((a[i][j]>=0?y[a[i][j]]:0)-(a[i][j+1]>=0?y[a[i][j+1]]:0));
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]];
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 |
7 ms |
30040 KB |
Output is correct |
2 |
Correct |
8 ms |
30044 KB |
Output is correct |
3 |
Correct |
7 ms |
30044 KB |
Output is correct |
4 |
Correct |
6 ms |
30040 KB |
Output is correct |
5 |
Correct |
8 ms |
30044 KB |
Output is correct |
6 |
Incorrect |
7 ms |
30084 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
30044 KB |
Output is correct |
2 |
Correct |
8 ms |
30044 KB |
Output is correct |
3 |
Runtime error |
116 ms |
92168 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
143 ms |
57860 KB |
Output is correct |
2 |
Incorrect |
380 ms |
87720 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
143 ms |
57860 KB |
Output is correct |
2 |
Incorrect |
380 ms |
87720 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
30040 KB |
Output is correct |
2 |
Correct |
8 ms |
30044 KB |
Output is correct |
3 |
Correct |
7 ms |
30044 KB |
Output is correct |
4 |
Correct |
6 ms |
30040 KB |
Output is correct |
5 |
Correct |
8 ms |
30044 KB |
Output is correct |
6 |
Incorrect |
7 ms |
30084 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |