把环倍长,只考虑 i i i~ i + n i+n i+n的点
发现最远点满足决策单调性
分治求解就可以了
#include<bits/stdc++.h>
using namespace std;
#define ll long long
inline int read(){
char ch=getchar();
int res=0,f=1;
while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=getchar();
return res*f;
}
const int N=500005;
inline double P(double x){
return x*x;
}
struct point{
double x,y;int idx;
friend inline double dis(const point &a,const point &b){
return P(a.x-b.x)+P(a.y-b.y);
}
}p[N];
int T,n,ans[N];
inline bool comp(int now,int p1,int p2){
ll x=dis(p[now],p[p1]),y=dis(p[now],p[p2]);
if(p1>now+n||p1<now)x=-x;
if(p2>now+n||p2<now)y=-y;
return (x==y)?p[p1].idx>p[p2].idx:x<y;
}
inline void solve(int l,int r,int st,int des){
int mid=(l+r)>>1,now=st;
for(int i=st+1;i<=des;i++)if(comp(mid,now,i))now=i;
ans[mid]=p[now].idx;
if(l<mid)solve(l,mid-1,st,now);
if(mid<r)solve(mid+1,r,now,des);
}
int main(){
T=read();
while(T--){
n=read();
for(int i=1;i<=n;i++){
p[i].x=read(),p[i].y=read(),p[i].idx=i,p[i+n]=p[i];
}
solve(1,n,1,n*2);
for(int i=1;i<=n;i++)cout<<ans[i]<<'\n';
}
}