This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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[1000001],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(),[](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 (stderr)
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++)
| ~^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |