【BZOJ4520】[Cqoi2016]K远点对

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

题解:我们枚举每个点在kd-tree上查询,同时维护一个小根堆,一旦某个点离当前点的距离>堆顶,就将它加入堆并弹出堆顶,最后的堆顶就是答案。

由于每个点对都被算了两边,所以k应该*2。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#define rep for(int i=0;i<=1;i++)
#define x2(_) ((_)*(_))
using namespace std;
const int maxn=100010;
typedef long long ll;
struct kd
{
ll v[2],sm[2],sn[2];
int ls,rs;
ll & operator [] (int a) {return v[a];}
kd (){}
kd (ll a,ll b){v[0]=sm[0]=sn[0]=a,v[1]=sm[1]=sn[1]=b,ls=rs=0;}
}t[maxn];
priority_queue<ll> pq;
int n,m,D,root,now;
ll A,B;
bool cmp(kd a,kd b)
{
return (a[D]==b[D])?(a[D^1]<b[D^1]):(a[D]<b[D]);
}
void pushup(int x,int y)
{
rep t[x].sm[i]=max(t[x].sm[i],t[y].sm[i]),t[x].sn[i]=min(t[x].sn[i],t[y].sn[i]);
}
int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-')f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret*f;
}
int build(int l,int r,int d)
{
if(l>r) return 0;
int mid=l+r>>1;
D=d,nth_element(t+l,t+mid,t+r+1,cmp);
t[mid].ls=build(l,mid-1,d^1),t[mid].rs=build(mid+1,r,d^1);
if(t[mid].ls) pushup(mid,t[mid].ls);
if(t[mid].rs) pushup(mid,t[mid].rs);
return mid;
}
ll getdis(int x)
{
return max(x2(t[x].sm[0]-A),x2(t[x].sn[0]-A))+max(x2(t[x].sm[1]-B),x2(t[x].sn[1]-B));
}
void query(int x)
{
if(!x||getdis(x)<=-pq.top()) return ;
if(x!=now&&x2(t[x][0]-A)+x2(t[x][1]-B)>-pq.top()) pq.push(-x2(t[x][0]-A)-x2(t[x][1]-B)),pq.pop();
if(getdis(t[x].ls)>getdis(t[x].rs)) query(t[x].ls),query(t[x].rs);
else query(t[x].rs),query(t[x].ls);
}
int main()
{
n=rd(),m=rd();
int i,a,b;
for(i=1;i<=n;i++) a=rd(),b=rd(),t[i]=kd(a,b);
for(i=1;i<=2*m;i++) pq.push(0);
root=build(1,n,0);
for(i=1;i<=n;i++) now=i,A=t[i].v[0],B=t[i].v[1],query(root);
printf("%lld\n",-pq.top());
return 0;
}

【BZOJ4520】[Cqoi2016]K远点对 kd-tree+堆的更多相关文章

  1. BZOJ4520:[CQOI2016]K远点对(K-D Tree)

    Description 已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对. Input 输入文件第一行为用空格隔开的两个整数 N, K.接下来 N 行,每行两个整数 X,Y,表示一个点 的坐标 ...

  2. BZOJ 4520: [Cqoi2016]K远点对(k-d tree)

    Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 1162  Solved: 618[Submit][Status][Discuss] Descripti ...

  3. [BZOJ4520][Cqoi2016]K远点对 kd-tree 优先队列

    4520: [Cqoi2016]K远点对 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 1285  Solved: 708[Submit][Statu ...

  4. [bzoj4520][Cqoi2016]K远点对_KD-Tree_堆

    K远点对 bzoj-4520 Cqoi-2016 题目大意:已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对. 注释:$1\le n\le 10^5$,$1\le k\le 100$,$k\l ...

  5. BZOJ4520 [Cqoi2016]K远点对

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  6. 【BZOJ-4520】K远点对 KD-Tree + 堆

    4520: [Cqoi2016]K远点对 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 490  Solved: 237[Submit][Status ...

  7. BZOJ 4520: [Cqoi2016]K远点对

    4520: [Cqoi2016]K远点对 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 638  Solved: 340[Submit][Status ...

  8. 【BZOJ4520】K远点对(KD-Tree)

    [BZOJ4520]K远点对(KD-Tree) 题面 BZOJ 洛谷 题解 考虑暴力. 维护一个大小为\(K\)的小根堆,然后每次把两个点之间的距离插进去,然后弹出堆顶 这样子可以用\(KD-Tree ...

  9. [Cqoi2016]K远点对 K-Dtree

    4520: [Cqoi2016]K远点对 链接 bzoj 思路 用K-Dtree求点的最远距离. 求的时候顺便维护一个大小为2k的小根堆. 不知道为啥一定会对. 代码 #include <bit ...

随机推荐

  1. python对象的复制问题

    list 的拷贝问题: 1, >>> a [1, 2] >>> b=a[:] >>> b [1, 2] >>> b[0]=20 ...

  2. UVA 11059 Maximum Product【三层暴力枚举起终点】

    [题意]:乘积最大的子序列.n∈[1,10],s∈[-10,10] [代码]: #include<bits/stdc++.h> using namespace std; int a[105 ...

  3. 维生素C - 坏血症

    在地理大发现时代,许多水手在远洋航行时不幸罹患一种典型航海病,患者皮肤溃烂.牙龈出血不止,不久就会危及生命,这就是大名鼎鼎的坏血症,是一种因为缺乏维生素C而产生的的皮.粘膜下出血.齿龈肿胀.关节和肌肉 ...

  4. Xamarin XAML语言教程Xamarin.Forms中活动指示器的显示隐藏

    Xamarin XAML语言教程Xamarin.Forms中活动指示器的显示隐藏 开发者除了可以在XAML中使用IsRunning属性控制指示器的显示隐藏外,还可以在代码隐藏文件中使用IsRunnin ...

  5. workflow engine Ruote初体验之二(通用属性)

    罗列一下表达式所支持的属性: :timeout :if/ unless :forget :lose :flank :on_error :on_cancel :on_timeout :tag :filt ...

  6. IntelliJ IDEA安装MongoDB的的数据操作插件

    说明:只能说效果一般,没有Robo 3T那么好用. 参考: https://intellij-support.jetbrains.com/hc/en-us/community/posts/206635 ...

  7. PHP处理Android的POST数据

    今天用PHP开发Android网络数据接口的时候,发现Thinkphp的I函数(php的$_POST)并不能获取到androidpost过来的数据 Android代码如下: Map<String ...

  8. Android Studio 中.android 文件夹移动默认位置

    转自 开发工具打造: .android 文件夹移动默认位置 .android 文件夹是用来存放 avd 模拟器文件的文件夹. 因为默认是 C盘 的. 占用空间比较大.很不爽 将它移动到其它盘其实很简单 ...

  9. IIS7.5上安装Git服务器

    系统要求: IIS 7及以上 .NET FrameWork 4.5 ASP.NET 4以上 安装步骤: 从Bonobo官网下载最新版本的BonoboService: 解压下载的zip包: 在IIS中新 ...

  10. Usage of API documented as @since1.6+

    Usage of API documented as @since1.6+ File ->Project Structure->Project Settings -> Modules ...