#include "rail.h"
#include <bits/stdc++.h>
using namespace std;
map<pair<int,int>,int> mp;
int dist(int a, int b){
if (mp.count({a,b})) return mp[{a,b}];
return mp[{a,b}]=getDistance(a,b);
}
void findLocation(int N, int first, int location[], int stype[])
{
int c = 0, d = 1;
for (int i = 2; i < N; i++){
if (dist(c,i)<dist(c,d)) d=i;
}
vector<int> left;
vector<int> right;
stype[c]=1,stype[d]=2;
location[c]=first,location[d]=first+dist(c,d);
for (int i = 0; i < N; i++){
if (i==c || i==d) continue;
if (dist(c,i)==dist(c,d)+dist(d,i)){
if (dist(d,i)<dist(c,d)){
location[i]=location[d]-dist(d,i);
stype[i]=1;
}
else left.push_back(i);
}
else {
right.push_back(i);
}
}
sort(left.begin(), left.end(), [&](int a, int b){
return dist(d,a)<dist(d,b);
});
sort(right.begin(), right.end(), [&](int a, int b){
return dist(c,a)<dist(c,b);
});
if (left.size()){
vector<int> virt;
virt.push_back(left[0]);
location[virt.back()]=location[d]-dist(d,virt.back());
stype[virt.back()]=1;
for (int _ = 1; _ < left.size(); _++){
int i = left[_];
bool boolean=false;
int fiC;
for (int i = 0; i < virt.size(); i++){
fiC=virt[i];
if (dist(d,i)==dist(d,fiC)+dist(fiC,i)){
boolean=true;
break;
}
}
if (boolean){
stype[i]=2;
location[i]=location[fiC]+dist(fiC,i);
}
else {
stype[i]=1;
location[i]=location[d]-dist(d,i);
virt.push_back(i);
}
}
}
if (right.size()){
vector<int> virt;
virt.push_back(right[0]);
location[virt.back()]=location[c]+dist(c,virt.back());
stype[virt.back()]=2;
for (int _ = 1; _ < right.size(); _++){
int i = right[_];
bool boolean=false;
int fiD;
for (int i = 0; i < virt.size(); i++){
fiD=virt[i];
if (dist(c,i)==dist(c,fiD)+dist(fiD,i)){
boolean=true;
break;
}
}
if (boolean){
stype[i]=1;
location[i]=location[fiD]-dist(fiD,i);
}
else {
stype[i]=2;
location[i]=location[c]+dist(c,i);
virt.push_back(i);
}
}
}
}