4520: [Cqoi2016]K远点对

4520: [Cqoi2016]K远点对

Time Limit: 30 Sec   Memory Limit: 512 MB
Submit: 594   Solved: 314
[ Submit][ Status][ Discuss]

Description

已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对。

Input

输入文件第一行为用空格隔开的两个整数 N, K。接下来 N 行,每行两个整数 X,Y,表示一个点
的坐标。1 < =  N < =  100000, 1 < =  K < =  100, K < =  N*(N−1)/2 , 0 < =  X, Y < 2^31。

Output

输出文件第一行为一个整数,表示第 K 远点对的距离的平方(一定是个整数)。

Sample Input

10 5
0 0
0 1
1 0
1 1
2 0
2 1
1 2
0 2
3 0
3 1

Sample Output

9

HINT

Source

[ Submit][ Status][ Discuss]



建立一棵K-D tree,对于n个点,每次查询其前k远点对,开个堆维护一下,最后输出来就行

这样每个点对会被查询两次,所以维护大小为2*k的堆

这是一篇很不错的blog


建好树以后,,如何查询每个点的前k远点对?

对于每个子树,分别维护二维坐标上的min值和max值,这样查询的时候粗略估计,如果往下找可能找到一个挺大的值那就递归去查询

记住二维一定要分开维护--不然效率差得太多了!!!!!

(苟蒻亲自TLE一发)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;

const int maxn = 1E5 + 10;
typedef long long LL;
typedef double DB;
const int INF = ~0U>>1;
typedef __gnu_pbds::priority_queue<LL,greater<LL>,__gnu_pbds::pairing_heap_tag> Heap;

struct Point{
	LL x,y;
	Point(){}
	Point(LL x,LL y): x(x),y(y){}
}p[maxn];

int n,m,k,cnt,Root,ch[maxn][2];
LL tx,ty,D[maxn][2],ma[maxn][2],mi[maxn][2],Dt[2];
Heap Q;

DB sqrDB(DB t) {return t*t;}
LL sqrLL(LL t) {return t*t;}
bool cmpx(const Point &a,const Point &b) {return a.x < b.x;}
bool cmpy(const Point &a,const Point &b) {return a.y < b.y;}
int getint()
{
	char ch = getchar();
	int ret = 0;
	while (ch < '0' || '9' < ch) ch = getchar();
	while ('0' <= ch && ch <= '9')
		ret = ret*10 + ch - '0',ch = getchar();
	return ret;
}

int Build(int l,int r)
{
	if (l > r) return 0;
	int ret = ++cnt;
	DB arx,ary,sqx,sqy;
	arx = ary = sqx = sqy = 0;
	for (int i = l; i <= r; i++) {
		arx += (DB)(p[i].x)/(DB)(n);
		ary += (DB)(p[i].y)/(DB)(n);
	}
	for (int i = l; i <= r; i++) {
		sqx += sqrDB((DB)(p[i].x) - arx)/(DB)(n);
		sqy += sqrDB((DB)(p[i].y) - ary)/(DB)(n);
	}
	if (sqx > sqy) sort(p + l,p + r + 1,cmpx);
	else sort(p + l,p + r + 1,cmpy);
	int mid = (l + r) >> 1;
	D[ret][0] = p[mid].x;
	D[ret][1] = p[mid].y;
	for (int i = 0; i < 2; i++) {
		ma[ret][i] = D[ret][i];
		mi[ret][i] = D[ret][i];
	}
	ch[ret][0] = Build(l,mid-1);
	ch[ret][1] = Build(mid+1,r);
	for (int i = 0; i < 2; i++) 
		if (ch[ret][i])
			for (int j = 0; j < 2; j++) {
				ma[ret][j] = max(ma[ret][j],ma[ch[ret][i]][j]);
				mi[ret][j] = min(mi[ret][j],mi[ch[ret][i]][j]);
			}
	return ret;
}

void Query(int Now)
{
	LL d[2],dn;
	dn = d[0] = d[1] = 0;
	for (int i = 0; i < 2; i++)
		dn += sqrLL(Dt[i] - D[Now][i]);
	for (int i = 0; i < 2; i++)
		if (ch[Now][i])
			for (int j = 0; j < 2; j++)
				d[i] += max(sqrLL(Dt[j] - ma[ch[Now][i]][j]),sqrLL(Dt[j] - mi[ch[Now][i]][j]));
	if (dn > Q.top()) Q.pop(),Q.push(dn);
	if (d[0] > d[1]) {
		if (ch[Now][0] && d[0] > Q.top()) Query(ch[Now][0]);
		if (ch[Now][1] && d[1] > Q.top()) Query(ch[Now][1]);
	}
	else {
		if (ch[Now][1] && d[1] > Q.top()) Query(ch[Now][1]);
		if (ch[Now][0] && d[0] > Q.top()) Query(ch[Now][0]);
	}
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	n = getint(); k = getint();
	for (int i = 1; i <= n; i++) 
		p[i] = Point(getint(),getint());
	Root = Build(1,n);
	for (int i = 1; i <= 2*k; i++)
		Q.push(0);
	for (int i = 1; i <= n; i++) {
		Dt[0] = p[i].x; Dt[1] = p[i].y;
		Query(Root);
	}
	cout << Q.top();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值