#include "rail.h"
/*
* Author: Nonoze
* Created: Tuesday 28/04/2026
*/
#include <bits/stdc++.h>
using namespace std;
#ifndef DEBUG
#define dbg(...)
#else
#include "grader.cpp"
#endif
#define cout cerr
#define quit(x) return (void)(cout << x << endl)
template<typename T> void read(T& x) { cin >> x; }
template<typename T1, typename T2> void read(pair<T1, T2>& p) { read(p.first), read(p.second); }
template<typename T> void read(vector<T>& v) { for (auto& x : v) read(x); }
template<typename T1, typename T2> void read(T1& x, T2& y) { read(x), read(y); }
template<typename T1, typename T2, typename T3> void read(T1& x, T2& y, T3& z) { read(x), read(y), read(z); }
template<typename T1, typename T2, typename T3, typename T4> void read(T1& x, T2& y, T3& z, T4& zz) { read(x), read(y), read(z), read(zz); }
template<typename T> void print(vector<T>& v) { for (auto& x : v) cout << x << ' '; cout << endl; }
#define sz(x) (int)(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(v) sort(all(v)), v.erase(unique(all(v)), (v).end())
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define fi first
#define se second
#define cmin(a, b) a = min(a, b)
#define cmax(a, b) a = max(a, b)
// #define int long long
#define double long double
const int inf = numeric_limits<int>::max() / 4;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7, LOG=20;
int n;
void findLocation(int _n, int first, int location[], int stype[]) { n=_n;
vector<pair<int, int>> dist0sorted;
vector<int> dist0(n), dist1(n);
int best=-1;
for (int i=1; i<n; i++) {
dist0[i]=getDistance(0, i);
dist0sorted.push_back({dist0[i], i});
}
vector<pair<int, int>> a(n); a[0]={first, 0};
sort(all(dist0sorted)); best=dist0sorted[0].se; a[best]={first+dist0[best], 1};
for (int i=1; i<n; i++) if (i!=best) dist1[i]=getDistance(best, i);
dist1[0]=dist0[best];
int lastleft=0, lastright=best;
vector<int> alreadyleft, alreadyright; alreadyleft.pb(0), alreadyright.pb(best);
for (auto &[d, i]: dist0sorted) if (i!=best) {
if (d==dist1[i]+dist0[best]) {
if (dist1[i]<dist1[0]) {
a[i]={a[best].fi-dist1[i], 0};
alreadyleft.pb(i);
} else if (lastleft==0) {
a[i]={a[best].fi-dist1[i], 0};
lastleft=i;
alreadyleft.pb(i);
} else {
int dist=getDistance(lastleft, i);
int pos=a[lastleft].fi+dist;
bool ok=0;
for (auto &j: alreadyleft) {
if (a[j].fi<pos && dist1[j]+pos-a[j].fi==dist1[i]) {
ok=1;
break;
}
}
if (ok) {
a[i]={pos, 1};
} else {
a[i]={a[best].fi-dist1[i], 0};
lastleft=i;
alreadyleft.pb(i);
}
}
} else {
if (d<dist0[best]) {
a[i]={a[0].fi+d, 1};
alreadyright.pb(i);
} else if (lastright==best) {
a[i]={a[0].fi+d, 1};
lastright=i;
alreadyright.pb(i);
} else {
int dist=getDistance(lastright, i);
int pos=a[lastright].fi-dist;
bool ok=0;
for (auto &j: alreadyright) {
if (a[j].fi>pos && dist0[j]+a[j].fi-pos==dist0[i]) {
ok=1;
break;
}
}
if (ok) {
a[i]={pos, 0};
} else {
a[i]={a[0].fi+d, 1};
lastright=i;
alreadyright.pb(i);
}
}
}
}
for (int i=0; i<n; i++) location[i]=a[i].fi, stype[i]=a[i].se+1;
}