4520: [Cqoi2016]K远点对

Time Limit: 30 Sec  Memory Limit: 512 MB
Submit: 1285  Solved: 708
[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

kd-tree可以查询每个点到其他点的距离。

我们求出每两个点间的距离,随后求出第2*k大值即可。

用优先队列维护前2*k大值,在kd-tree上查询并修改即可。

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#define ll long long
#define maxn 100005
using namespace std;
inline int read() {
int x=,f=;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-;
for(;isdigit(ch);ch=getchar()) x=x*+ch-'';
return x*f;
}
int nowd=,rt,n,k;
struct node {ll d[],mx[],mn[],l,r;}t[maxn];
bool cmp(node t1,node t2) {return t1.d[nowd]<t2.d[nowd];}
priority_queue<ll,vector<ll>,greater<ll> >q;ll cnt=;
void pushup(int x) {
int l=t[x].l,r=t[x].r;
if(l) {
t[x].mx[]=max(t[x].mx[],t[l].mx[]);
t[x].mx[]=max(t[x].mx[],t[l].mx[]);
t[x].mn[]=min(t[x].mn[],t[l].mn[]);
t[x].mn[]=min(t[x].mn[],t[l].mn[]);
}
if(r) {
t[x].mx[]=max(t[x].mx[],t[r].mx[]);
t[x].mx[]=max(t[x].mx[],t[r].mx[]);
t[x].mn[]=min(t[x].mn[],t[r].mn[]);
t[x].mn[]=min(t[x].mn[],t[r].mn[]);
}
}
int build(int l,int r,bool D) {
int mid=l+r>>;nowd=D;
nth_element(t+l,t+mid,t+r+,cmp);
if(l<mid) t[mid].l=build(l,mid-,!D);
if(r>mid) t[mid].r=build(mid+,r,!D);
t[mid].mx[]=t[mid].mn[]=t[mid].d[];
t[mid].mx[]=t[mid].mn[]=t[mid].d[];
pushup(mid);
return mid;
}
ll dis(int x,int y) {return (t[x].d[]-t[y].d[])*(t[x].d[]-t[y].d[])+(t[x].d[]-t[y].d[])*(t[x].d[]-t[y].d[]);}
ll gdis(int x,int y) {
return max((t[x].mn[]-t[y].d[])*(t[x].mn[]-t[y].d[]),(t[x].mx[]-t[y].d[])*(t[x].mx[]-t[y].d[]))+max((t[x].mn[]-t[y].d[])*(t[x].mn[]-t[y].d[]),(t[x].mx[]-t[y].d[])*(t[x].mx[]-t[y].d[]));
}
void query(int x,int y) {
if(!x) return;
ll now=dis(x,y);
if(now>q.top()) {q.pop();q.push(now);}
ll dl=,dr=;
if(t[x].l) dl=gdis(t[x].l,y);if(t[x].r) dr=gdis(t[x].r,y);
if(dl>dr) {
if(dl>q.top()) query(t[x].l,y);
if(dr>q.top()) query(t[x].r,y);
}
else {
if(dr>q.top()) query(t[x].r,y);
if(dl>q.top()) query(t[x].l,y);
}
}
int main() {
n=read();k=read();
for(int i=;i<=n;i++) t[i].d[]=read(),t[i].d[]=read();
rt=build(,n,);
for(int i=;i<=k*;i++) q.push();
for(int i=;i<=n;i++) query(rt,i);
printf("%lld\n",q.top());
}

[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_堆

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

  4. BZOJ4520 [Cqoi2016]K远点对

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

  5. 【BZOJ4520】[Cqoi2016]K远点对 kd-tree+堆

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

  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. 封装 RabbitMQ.NET

    这篇文章内容会很短,主要是想给大家分享下我最近在做一个简单的rabbitmq客户端类库的封装的经验总结,说是简单其实一点都不简单.为了节省时间我主要按照Library的执行顺序来介绍,在你看来这里仅仅 ...

  2. nodejs安装错误

    network错误: npm ERR! network tunneling socket could not be established, cause=connect ECONNREFUSED 12 ...

  3. 常见的位运算技巧总结(膜wys)

    看了wys的论文,感觉获得了不少新姿势 这里总结一下 #include <iostream> using namespace std; typedef unsigned int u32; ...

  4. [CF107D]Crime Management

    题目大意:有一种长度为$n(n\leqslant 10^{18})$的字符串,给定$m(m\leqslant10^3)$种限制,即字符$c$出现的次数为$cnt$,若一个字符有多种限制,则满足任意一个 ...

  5. POJ2155 Matrix 【二维线段树】

    题目链接 POJ2155 题解 二维线段树水题,蒟蒻本想拿来养生一下 数据结构真的是有毒啊,, TM这题卡常 动态开点线段树会TLE[也不知道为什么] 直接开个二维数组反倒能过 #include< ...

  6. Robot POJ - 1376

    The Robot Moving Institute is using a robot in their local store to transport different items. Of co ...

  7. bzoj 5099 [POI2018]Pionek 计算几何 极角排序

    [POI2018]Pionek Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 269  Solved: 80[Submit][Status][Disc ...

  8. 马上给Meltdown和Spectre漏洞打补丁

    元旦之后的第一个工作日可谓是惊喜不断,4号就传来了 Google Project Zero 等团队和个人报告的 Meltdown 和 Spectre 内核漏洞的消息,首先简单介绍一下这两个内核漏洞. ...

  9. is

    MyType a = null; if (a is MyType) == False

  10. linux查看操作系统是多少位

    有三种方法: 1.echo $HOSTTYPE 2.getconf LONG_BIT,此处不应该是getconf WORD_BIT命令,在64位系统中显示的是32 3.uname -a 出现" ...