题意:给出n个不同的点,求出离每个点欧几里得距离平方最近的其他点。
只需要一点小技巧即可:当当前kdtree节点坐标和询问坐标一致时,距离改成无穷大(因为不能选择自己)。
#include
#include
#include
#include
#include
#include
#include
#define Clear(x,y) memset (x,y,sizeof(x))
#define Close() ios::sync_with_stdio(0)
#define Open() freopen ("more.in", "r", stdin)
#define get_min(a,b) a = min (a, b)
#define get_max(a,b) a = max (a, b);
#define fi first
#define se second
#define pii pair
#define pli pair
#define pb push_back
#define mod 1000000007
template
inline bool scan (T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) return 0; //EOF
while (c != '-' && (c < '0' || c > '9') ) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
using namespace std;
#define maxn 100005
struct node {
int d[2], l, r;//节点的点的坐标 左右孩子
long long Max[2], Min[2];//节点中点x的最值 y的最值
int id;
}tree[maxn<<1], tmp;
int n, m;
int root, cmp_d;
bool cmp (const node &a, const node &b) {
return a.d[cmp_d] < b.d[cmp_d] || (a.d[cmp_d] == b.d[cmp_d] &&
a.d[cmp_d^1] < b.d[cmp_d^1]);
}
void push_up (int p, int pp) {
get_min (tree[p].Min[0], tree[pp].Min[0]);
get_min (tree[p].Min[1], tree[pp].Min[1]);
get_max (tree[p].Max[0], tree[pp].Max[0]);
get_max (tree[p].Max[1], tree[pp].Max[1]);
}
int build_tree (int l, int r, int D) {
int mid = (l+r)>>1;
tree[mid].l = tree[mid].r = 0;
cmp_d = D;
nth_element (tree+l+1, tree+mid+1, tree+1+r, cmp);
//按照cmp把第mid元素放在中间 比他小的放左边 比他大的放右边
tree[mid].Max[0] = tree[mid].Min[0] = tree[mid].d[0];
tree[mid].Max[1] = tree[mid].Min[1] = tree[mid].d[1];
if (l != mid) tree[mid].l = build_tree (l, mid-1, D^1);
if (r != mid) tree[mid].r = build_tree (mid+1, r, D^1);
if (tree[mid].l) push_up (mid, tree[mid].l);
if (tree[mid].r) push_up (mid, tree[mid].r);
return mid;
}
void insert (int now) {
int D = 0, p = root;
while (1) {
push_up (p, now);//先更新p节点
if (tree[now].d[D] >= tree[p].d[D]) {
if (!tree[p].r) {
tree[p].r = now;
return ;
}
else p = tree[p].r;
}
else {
if (!tree[p].l) {
tree[p].l = now;
return ;
}
else p = tree[p].l;
}
D ^= 1;
}
return ;
}
#define INF 4e18
#define sqr(a) (a)*(a)
long long ans, x, y;
long long dis (int p, int x, int y) {//点(x,y)在p的管辖范围内的可能最小值
long long ans = 0;
if (x < tree[p].Min[0]) ans += sqr (tree[p].Min[0]-x);
if (x > tree[p].Max[0]) ans += sqr (x-tree[p].Max[0]);
if (y < tree[p].Min[1]) ans += sqr (tree[p].Min[1]-y);
if (y > tree[p].Max[1]) ans += sqr (y-tree[p].Max[1]);
return ans;
}
long long distance (int i, long long x, long long y) {
long long xx = tree[i].d[0]-x, yy = tree[i].d[1]-y;
return (xx*xx+yy*yy);
}
void query (int p) {
long long dl = INF, dr = INF, d0;
d0 = distance (p, x, y);//初始答案
if (tree[p].d[0] == x && tree[p].d[1] == y) d0 = INF;//重合
get_min (ans, d0);
if (tree[p].l) dl = dis (tree[p].l, x, y);
if (tree[p].r) dr = dis (tree[p].r, x, y);
if (dl < dr) {
if (dl < ans) query (tree[p].l);
if (dr < ans) query (tree[p].r);
}
else {
if (dr < ans) query (tree[p].r);
if (dl < ans) query (tree[p].l);
}
}
long long res[maxn];
int main () {
//Open ();
int t; cin >> t;
while (t--) {
scan (n);
for (int i = 1; i <= n; i++) {
scan (tree[i].d[0]);
scan (tree[i].d[1]);
tree[i].id = i;
}
root = build_tree (1, n, 0);
for (int i = 1; i <= n; i++) {
ans = INF;
x = tree[i].d[0], y = tree[i].d[1];
query (root);
res[tree[i].id] = ans;
}
for (int i = 1; i <= n; i++) printf ("%lld\n", res[i]);
}
return 0;
}