题意:平面上有一些蚁巢,每只蚂蚁在任意蚁巢都会走距离自己最近的蚁巢,如果有多个那么就优先x最小,再到y最小。现在有多组询问,每组询问代表两只蚂蚁所在巢穴,问最终他们会不会相遇。
KD树+并查集
对于任意一个起点,到最后必定会在两个点之间徘徊,所以可以用并查集,KD树找出每个点最近的那个点就可以了。
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
#define LL long long
const int maxn = 200080;
const int K = 2;
int num, nownum;
LL ans;
struct kdNode {
LL x[K];//维度
int div, id;
bool lef;//是否为叶子
};
int Ans[maxn];
struct Node {
kdNode a;
LL dis;//表示和目标点的距离
bool operator<(const Node & other) const {
return (dis < other.dis) || (dis == other.dis && a.x[0] < other.a.x[0])
|| (dis == other.dis && a.x[0] == other.a.x[0] && a.x[1] < other.a.x[1]);
}
Node() {}
Node(kdNode &tmp, LL d) {
a = tmp;
dis = d;
}
};
kdNode p[5*maxn], q;
priority_queue <Node> qq;
int cmpNo, fa[maxn], fx, fy, xx, yy;
bool cmp(kdNode a, kdNode b) {
return a.x[cmpNo] < b.x[cmpNo];
}
LL max(LL a,LL b) {
return a>b?a:b;
}
LL dis(kdNode a, kdNode b, int k) {
LL res = 0;
for(int i = 0;i < k;i++)
res += (a.x[i] - b.x[i])*(a.x[i] - b.x[i]);
return res;
}
void buildKD(int l, int r, kdNode *p, int d, int k) {
if (l > r) return;
int mid = (l+r)/2;
cmpNo = d;
nth_element(p+l, p+mid, p+r+1, cmp);
p[mid].div = d;
if (l == r) {
p[mid].lef = 1;
return;
}
buildKD(l, mid-1, p, (d+1)%k, k);
buildKD(mid+1, r, p, (d+1)%k, k);
}
void findKD(int l, int r, kdNode &tar, kdNode *p, int k) {
if (l > r) return;
int mid = (l+r)/2;
LL d = dis(p[mid], tar, k);
if(p[mid].lef) {//如果是叶子
if(nownum < num) {
nownum++;
ans = max(ans, d);
qq.push(Node(p[mid], d));
} else if ((ans > d)||(ans == d && p[mid].x[0] < qq.top().a.x[0])||(ans == d && p[mid].x[0] == qq.top().a.x[0] && p[mid].x[1] < qq.top().a.x[1])) {
qq.pop();
qq.push(Node(p[mid], d));
ans = qq.top().dis;
}
return;
}
LL t = tar.x[p[mid].div] - p[mid].x[p[mid].div];
if(t > 0) {//右子树
findKD(mid+1, r, tar, p, k);
if (nownum < num) {
qq.push(Node(p[mid], d));
nownum++;
ans = qq.top().dis;
findKD(l, mid-1, tar, p, k);
} else {
if ((ans > d)||(ans == d && p[mid].x[0] < qq.top().a.x[0])||(ans == d && p[mid].x[0] == qq.top().a.x[0] && p[mid].x[1] < qq.top().a.x[1])) {
qq.pop();
qq.push(Node(p[mid], d));
ans = qq.top().dis;
}
if (ans >= t*t) findKD(l, mid-1, tar, p, k);
}
} else {//左子树
findKD(l, mid-1, tar, p, k);
if (nownum < num) {
qq.push(Node(p[mid],d));
nownum++;
ans = qq.top().dis;
findKD(mid+1, r, tar, p, k);
} else {
if ((ans > d)||(ans == d && p[mid].x[0] < qq.top().a.x[0])||(ans == d && p[mid].x[0] == qq.top().a.x[0] && p[mid].x[1] < qq.top().a.x[1])) {
qq.pop();
qq.push(Node(p[mid],d));
ans = qq.top().dis;
}
if (ans >= t*t) findKD(mid+1, r, tar, p, k);
}
}
}
int getfa(int x) {
if (fa[x] == x) return x;
fa[x] = getfa(fa[x]);
return fa[x];
}
int main() {
int n, m, k, T;
k = 2;
scanf("%d", &T);
for (int cases = 1; cases <= T; cases++) {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
scanf("%lld", &p[i].x[j]);
}
p[i].lef = 0;
p[i].id = i;
}
buildKD(0, n-1, p, k-1, k);
memset(Ans, 0, sizeof(Ans));
for (int i = 0; i < n; i++) {
ans = -1;
nownum = 0;
while (!qq.empty()) qq.pop();
num = 2;
q = p[i];
findKD(0, n-1, q, p, k);
Ans[p[i].id] = qq.top().a.id;
}
for (int i = 0; i < n; i++) fa[i] = i;
for (int i = 0; i < n; i++) {
fx = getfa(fa[i]);
fy = getfa(fa[Ans[i]]);
if (fx != fy) fa[fx] = fy;
}
printf("Case #%d:\n", cases);
for (int i = 0; i < m; i++) {
scanf("%d%d", &xx, &yy);
xx--; yy--;
fx = getfa(fa[xx]);
fy = getfa(fa[yy]);
if (fx == fy) printf("YES\n");
else printf("NO\n");
}
}
return 0;
}