BZOJ 4520: [Cqoi2016]K远点对 KDtree + 估价函数 + 堆
Description
已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对。
Input
Output
输出文件第一行为一个整数,表示第 K 远点对的距离的平方(一定是个整数)
题解: 用堆维护前 $k$ 远的距离,依次枚举每个点来更新即可.
#include<bits/stdc++.h>
#define ll long long
#define maxn 200000
#define inf 100000000000000
#define lson (t[x].ch[0])
#define rson (t[x].ch[1])
#define mid ((l+r)>>1)
using namespace std;
void debug()
{
printf("no_problem\n");
}
int n,K,d,root;
void setIO(string s)
{
string in=s+".in";
freopen(in.c_str(),"r",stdin);
}
priority_queue<ll,vector<ll>, greater<ll> >Q;
struct Node
{
int p[2],ch[2],minv[2],maxv[2];
}t[maxn],T;
bool cmp(Node a,Node b)
{
return a.p[d]==b.p[d]?a.p[d^1]<b.p[d^1]:a.p[d]<b.p[d];
}
void pushup(int x,int y)
{
t[x].minv[0]=min(t[x].minv[0],t[y].minv[0]);
t[x].maxv[0]=max(t[x].maxv[0],t[y].maxv[0]);
t[x].minv[1]=min(t[x].minv[1],t[y].minv[1]);
t[x].maxv[1]=max(t[x].maxv[1],t[y].maxv[1]);
}
int build(int l,int r,int o)
{
d=o;
nth_element(t+l,t+mid,t+1+r,cmp);
t[mid].minv[0]=t[mid].maxv[0]=t[mid].p[0];
t[mid].minv[1]=t[mid].maxv[1]=t[mid].p[1];
t[mid].ch[0]=t[mid].ch[1]=0;
if(mid>l)
{
t[mid].ch[0]=build(l,mid-1,o^1);
pushup(mid,t[mid].ch[0]);
}
if(r>mid)
{
t[mid].ch[1]=build(mid+1,r,o^1);
pushup(mid,t[mid].ch[1]);
}
return mid;
}
ll sq(ll x)
{
return x * x;
}
ll getmax(int x)
{
ll ans=0;
for(int i=0;i<2;++i)
{
ans+=max( sq(1ll*(t[x].minv[i]-T.p[i])), sq(1ll*(t[x].maxv[i]-T.p[i])) );
}
return ans;
}
void solve(int x,int x1,int y1)
{
ll dn=getmax(x), cur=sq(1ll*(t[x].p[0]-x1)) + sq(1ll*(t[x].p[1]-y1));
if(dn <= Q.top()) return;
if(cur>Q.top())
{
Q.pop();
Q.push(cur);
}
ll dl=lson ? getmax(lson) : -inf;
ll dr=rson ? getmax(rson) : -inf;
if(dl>dr)
{
if(dl > Q.top()) solve(lson, x1, y1);
if(dr > Q.top()) solve(rson, x1, y1);
}
else
{
if(dr > Q.top()) solve(rson, x1, y1);
if(dl > Q.top()) solve(lson, x1, y1);
}
}
int main()
{
// setIO("input");
scanf("%d%d",&n,&K);
K<<=1;
for(int i=1;i<=n;++i) scanf("%d%d",&t[i].p[0],&t[i].p[1]);
root=build(1,n,0);
for(int i=1;i<=K;++i) Q.push(0);
for(int i=1;i<=n;++i)
{
T.p[0]=t[i].p[0];
T.p[1]=t[i].p[1];
solve(root,T.p[0],T.p[1]);
}
printf("%lld\n",Q.top());
return 0;
}
BZOJ 4520: [Cqoi2016]K远点对 KDtree + 估价函数 + 堆的更多相关文章
- BZOJ 4520: [Cqoi2016]K远点对
4520: [Cqoi2016]K远点对 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 638 Solved: 340[Submit][Status ...
- BZOJ 4520 [Cqoi2016]K远点对(KD树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4520 [题目大意] 求K远点对距离 [题解] 修改估价函数为欧式上界估价,对每个点进行 ...
- BZOJ 4520: [Cqoi2016]K远点对(k-d tree)
Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1162 Solved: 618[Submit][Status][Discuss] Descripti ...
- [Cqoi2016]K远点对 K-Dtree
4520: [Cqoi2016]K远点对 链接 bzoj 思路 用K-Dtree求点的最远距离. 求的时候顺便维护一个大小为2k的小根堆. 不知道为啥一定会对. 代码 #include <bit ...
- [BZOJ4520][Cqoi2016]K远点对 kd-tree 优先队列
4520: [Cqoi2016]K远点对 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1285 Solved: 708[Submit][Statu ...
- 【BZOJ4520】[Cqoi2016]K远点对 kd-tree+堆
[BZOJ4520][Cqoi2016]K远点对 Description 已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对. Input 输入文件第一行为用空格隔开的两个整数 N, K.接下来 ...
- 【52.55%】【BZOJ 4520】K远点对
Time Limit: 30 Sec Memory Limit: 512 MB Submit: 588 Solved: 309 [Submit][Status][Discuss] Descript ...
- 【BZOJ-4520】K远点对 KD-Tree + 堆
4520: [Cqoi2016]K远点对 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 490 Solved: 237[Submit][Status ...
- P4357 [CQOI2016]K远点对(KDTree)
传送门 又一次产生了KDTree本质就是爆搜的感觉-- 大概就类似于p4169,只不过是从最近点对变成了第\(k\)远点对 我们开一个小根堆,里面放\(k\)个元素,起初全为\(0\),然后每一次都把 ...
随机推荐
- JBoss AS 7之简单安装(The Return Of The King)
1.3 JBoss As 7安装 安装JBoss As 7分为以下几个步骤: 1. 下载JBoss 下载地址: <span style="font-size:18px;&quo ...
- oracle11g 手工建库步骤
#create oracle instance parameter vi initkevin.or db_name='kevin' memory_target=0 sga_max_size=5G sg ...
- LeetCode 771. Jewels and Stones (宝石与石头)
题目标签:Hash Table 这一题很简单,题目给了两个string:J 和 S. 只要把J 里面的 char 放入HashSet,再遍历S找出有多少个石头是宝石. Java Solution: R ...
- js 实现replaceAll
须要替换到字符串里面的多个双引號,不废话,直接上代码: var filePath = '"d:/img/1.jgp"'; filePath = filePath.replace(n ...
- 深入理解7816(3)-----关于T=0 【转】
本文转载自:http://blog.sina.com.cn/s/blog_4df8400a0102vcyp.html 深入理解7816(3)-----关于T=0 卡片和终端之间的数据传输是通过命令响应 ...
- luogu 1939 【模板】矩阵加速(数列)
题目大意: a[1]=a[2]=a[3]=1 a[x]=a[x-3]+a[x-1] (x>3) 求a数列的第n项%1000000007 思路: 使用矩阵快速幂进行加速 在草稿纸上填了填数 然后就 ...
- astgo-官方功能更新日志
2014年9月 2014-9-7:更新 1.安卓.苹果客户端添加字幕广告(点击字幕跳转打开网址) 2.安卓.苹果客户端添加公告推送功能 3.修正Astgo软交换管理平台修删除充值卡.用户账号,造成整个 ...
- windows2003下svn的安装
Windows2003下svn平台搭建 编辑:dnawo 日期:2010-08-03 转自http://www.mzwu.com/article.asp?id=2557 字体大小: 小 中 大 ...
- Tyvj1305最大子序和(单调队列优化dp)
描述 输入一个长度为n的整数序列,从中找出一段不超过M的连续子序列,使得整个序列的和最大. 例如 1,-3,5,1,-2,3 当m=4时,S=5+1-2+3=7当m=2或m=3时,S=5+1=6 输入 ...
- 百度地图JavaScript API获取用户当前经纬度和详细地理位置,反之通过详细地理位置获取当前经纬度
前言: 前端时间刚好使用了百度地图的js api定位获取用户当前经纬度并获取当前详细位置和通过当前用户详细地理位置换取用户当前经纬度坐标的功能,为了方便下次找起来方便一些自己在这里记录一下,希望也能够 ...