# 洛谷4631 [APIO2018] Circle selection 选圆圈 （KD树）

qwq纪念AC450

qwqq

cos -sin
sin cos


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#define pb push_back
#define mk make_pair
#define ll long long
#define lson ch[x][0]
#define rson ch[x][1]
#define double long double
#define int long long

using namespace std;

{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)) {if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}

const int maxn = 4e5+1e2;
const double phi = 1.04719755116;
const double eps = 1e-4;

struct KD{
double d[2],mx[2],mn[2];
double bj,mr;
int l,r;
int num;
};

KD now,t[maxn];
int root,ymh,n,m;
int ans[maxn];

struct Node{
double x,y;
double r;
int num;
};

Node a[maxn];

bool cmp(Node a,Node b)
{
if (a.r==b.r) return a.num<b.num;
return a.r>b.r;
}

bool operator <(KD a,KD b)
{
return a.d[ymh]<b.d[ymh];
}

void up(int root)
{
for (int i=0;i<=1;i++)
{
if (t[root].l) t[root].mn[i]=min(t[root].mn[i],t[t[root].l].mn[i]);
if (t[root].l) t[root].mx[i]=max(t[root].mx[i],t[t[root].l].mx[i]);
if (t[root].l) t[root].mr=max(t[root].mr,t[t[root].l].mr);
if (t[root].r) t[root].mr=max(t[root].mr,t[t[root].r].mr);
if (t[root].r) t[root].mn[i]=min(t[root].mn[i],t[t[root].r].mn[i]);
if (t[root].r) t[root].mx[i]=max(t[root].mx[i],t[t[root].r].mx[i]);
}
}

void build(int &x,int l,int r,int dd)
{
ymh = dd;
int mid = l+r >> 1;
x=mid;
nth_element(t+l,t+mid,t+r+1);
for (int i=0;i<=1;i++) t[x].mn[i]=t[x].mx[i]=t[x].d[i];
t[x].mr=t[x].bj;
if (l<x) build(t[x].l,l,mid-1,dd^1);
if (x<r) build(t[x].r,mid+1,r,dd^1);
up(x);
}

inline double getdis(int x)
{
double ans=0;
for (int i=0;i<=1;i++) ans=ans+(t[x].d[i]-now.d[i])*(t[x].d[i]-now.d[i]);
return ans;
}

inline double calc(int x)
{
double ans=0;
for (int i=0;i<=1;i++)
{
double tmp=0;
if (now.d[i]>=t[x].mn[i] && now.d[i]<=t[x].mx[i]) tmp=0;
else tmp=min((t[x].mn[i]-now.d[i])*(t[x].mn[i]-now.d[i]),(t[x].mx[i]-now.d[i])*(t[x].mx[i]-now.d[i]));
ans=ans+tmp;
}
return ans;
}

void query(int x)
{
//cout<<now.num<<endl;
//cout<<x<<" "<<t[x].l<<" "<<t[x].r<<" "<<t[x].mr<<" "<<t[x].d[1]<<endl;;
if (!x) return;
double d = getdis(x);
double d1 = calc(t[x].l);
double d2 = calc(t[x].r);
//cout<<d1<<" "<<(t[t[x].l].mr+now.bj)<<" "<<d2<<endl;
if (!ans[t[x].num] && (t[x].bj+now.bj)*(t[x].bj+now.bj)-d>=-eps) ans[t[x].num]=now.num;
if ((t[t[x].l].mr+now.bj)*(t[t[x].l].mr+now.bj)-d1>=-eps) query(t[x].l);
if ((t[t[x].r].mr+now.bj)*(t[t[x].r].mr+now.bj)-d2>=-eps) query(t[x].r);
}

signed main()
{
for (int i=1;i<=n;i++)
{
double tmp = x;
x=cos(phi)*x-sin(phi)*y;
y=sin(phi)*tmp+cos(phi)*y;
t[i].num=i;
t[i].d[0]=x;
t[i].d[1]=y;
t[i].bj=r;
a[i].x=x;
a[i].y=y;
a[i].num=i;
a[i].r=r;
//printf("%.2lf %.2lf %.2lf\n",x,y,r);
//cout<<endl;
}
build(root,1,n,0);
//cout<<"********"<<endl;
sort(a+1,a+1+n,cmp);
for (int i=1;i<=n;i++)
{
if (ans[a[i].num]) continue;
now.d[0]=a[i].x;
now.d[1]=a[i].y;
now.bj=a[i].r;
now.num=a[i].num;
query(root);
//out<<"*******"<<endl;
}
for (int i=1;i<=n;i++) cout<<ans[i]<<" ";
return 0;
}


posted @ 2019-01-21 09:43  y_immortal  阅读(257)  评论(0编辑  收藏  举报