bzoj4520
KD-tree+堆
多年大坑
KD-tree已经是半年前学的了,忘记了。这道题当时一直T,今天重新抄了一遍,A了
KD-tree过程:1.建树:每次依次按x,y划分平面,像二叉搜索树一样建树,每个点维护一些东西;
2.查询:直接查太暴力了,我们用估价函数减值,每个点维护最小最大的x和y,每次计算能够造成的最大距离,如果有价值就递归。注意关于dl,dr的大小判断,这里query顺序不一样,因为先query大的会使大的先进堆,这样更快一些,相当于一个剪枝。其实KD-tree就是一个暴力剪枝的过程。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
const int N = 1e5 + ;
int n, k, root, d;
struct data {
ll mx_x, mn_x, mx_y, mn_y, x, y, lc, rc;
bool friend operator < (const data &a, const data &b) {
if(d == ) return a.x == b.x ? a.y < b.y : a.x < b.x;
if(d == ) return a.y == b.y ? a.x < b.x : a.y < b.y;
}
} a[N];
priority_queue<ll, vector<ll>, greater<ll> > q;
ll sqr(ll x)
{
return x * x;
}
ll dis(int k, ll x, ll y)
{
return max(sqr(a[k].mn_x - x), sqr(a[k].mx_x - x)) + max(sqr(a[k].mn_y - y), sqr(a[k].mx_y - y));
}
void update(int x)
{
a[x].mn_x = min(a[x].x, min(a[a[x].lc].mn_x, a[a[x].rc].mn_x));
a[x].mx_x = max(a[x].x, max(a[a[x].lc].mx_x, a[a[x].rc].mx_x));
a[x].mn_y = min(a[x].y, min(a[a[x].lc].mn_y, a[a[x].rc].mn_y));
a[x].mx_y = max(a[x].y, max(a[a[x].lc].mx_y, a[a[x].rc].mx_y));
}
int build(int l, int r, int now)
{
if(l > r) return ;
int mid = (l + r) >> ;
d = now;
nth_element(a + l, a + mid, a + r + );
a[mid].mx_x = a[mid].mn_x = a[mid].x;
a[mid].mx_y = a[mid].mn_y = a[mid].y;
a[mid].lc = build(l, mid - , now ^ );
a[mid].rc = build(mid + , r, now ^ );
update(mid);
return mid;
}
void query(int k, ll x, ll y)
{
ll tmp = sqr(x - a[k].x) + sqr(y - a[k].y), dl = a[k].lc ? dis(a[k].lc, x, y) : -inf, dr = a[k].rc ? dis(a[k].rc, x, y) : -inf;
if(tmp > q.top()) q.pop(), q.push(tmp);
if(dl < dr)
{
if(dr > q.top()) query(a[k].rc, x, y);
if(dl > q.top()) query(a[k].lc, x, y);
}
else
{
if(dl > q.top()) query(a[k].lc, x, y);
if(dr > q.top()) query(a[k].rc, x, y);
}
}
int main()
{
scanf("%d%d", &n, &k);
a[].mn_x = inf;
a[].mx_x = -inf;
a[].mn_y = inf;
a[].mx_y = -inf;
for(int i = ; i <= n; ++i) scanf("%d%d", &a[i].x, &a[i].y);
root = build(, n, );
for(int i = ; i <= * k; ++i) q.push();
for(int i = ; i <= n; ++i) query(root, a[i].x, a[i].y);
printf("%lld\n", q.top());
return ;
}
bzoj4520的更多相关文章
- BZOJ4520 CQOI2016K远点对(KD-Tree+堆)
堆维护第k大,每个点KD-Tree上A*式查询较远点,跑得飞快,复杂度玄学. #include<iostream> #include<cstdio> #include<c ...
- 【bzoj4520】 Cqoi2016—K远点对
http://www.lydsy.com/JudgeOnline/problem.php?id=4520 (题目链接) 题意 求平面内第K远点对的距离. Solution 左转题解:jump 细节 刚 ...
- 【bzoj4520】K远点对
Portal --> bzoj4520 Description 给你平面内\(n\)个点的坐标,求欧氏距离下第\(k\)远的点对 Solution 因为kd其实..严格来说挺不熟的用的太少了qw ...
- 【BZOJ4520】K远点对(KD-Tree)
[BZOJ4520]K远点对(KD-Tree) 题面 BZOJ 洛谷 题解 考虑暴力. 维护一个大小为\(K\)的小根堆,然后每次把两个点之间的距离插进去,然后弹出堆顶 这样子可以用\(KD-Tree ...
- 【BZOJ4520】[Cqoi2016]K远点对 kd-tree+堆
[BZOJ4520][Cqoi2016]K远点对 Description 已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对. Input 输入文件第一行为用空格隔开的两个整数 N, K.接下来 ...
- [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 ...
- BZOJ4520 [Cqoi2016]K远点对
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...
- 【BZOJ-4520】K远点对 KD-Tree + 堆
4520: [Cqoi2016]K远点对 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 490 Solved: 237[Submit][Status ...
- bzoj4520【CQOI2016】K远点对
题解: kd-tree裸题 对每个点维护最近的k个开个堆维护一下
- BZOJ4520:[CQOI2016]K远点对(K-D Tree)
Description 已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对. Input 输入文件第一行为用空格隔开的两个整数 N, K.接下来 N 行,每行两个整数 X,Y,表示一个点 的坐标 ...
随机推荐
- 关于 AlphaGo 论文的阅读笔记
这是Deepmind 公司在2016年1月28日Nature 杂志发表论文 <Mastering the game of Go with deep neural networks and tre ...
- dos alias/cname address
diego@localhost sdk/include/Poco/Net]# dig b.wpss.cn ; <<>> DiG - <<>> b.wps ...
- 每日一支TED——帕特里夏·瑞安:不要固执于英语
瑞安讲述了她在科威特教学英语30年最大的 关于语言的一个感受:英语在迅速的在全世界传播,而其它的语言在逐渐的消失. 瑞安想要说的是.拥有一种国际性的语言,大家都能够理解,让全部人的 ...
- RobotFramework --RIDE介绍
RIDE是robotframework的图形操作前端,我们在RIDE上进行测试用例设计和编写测试脚本,并执行自动化测试.下面来全面的认识下这个操作工具. 在右边编辑页面有三大模块,Edit,TextE ...
- React系列-ES6
ES6新特性概览 React系列代码(非本人) 5 Projects to Help You Learn React 本人对javascript并不擅长,只是在工作中会时常用到,而且以jQuery居多 ...
- Redis 过期键的设置、获取和删除过期时间
Redis 过期键的设置.获取和删除过期时间 转自http://blog.51cto.com/littledevil/1813956 设置过期 默认情况下键是没有生存时间的,也就是永不过期,除非清空内 ...
- 平滑的滚动listview到一个指定位
http://blog.csdn.net/w8320273/article/details/9043339
- Android 监听返回键退出程序的两种实现
1.Android 双击返回键退出程序 思路:用户按下返回键时设定一个定时器来监控是否2秒内实现了退出,如果用户没有接着按返回键,则清除第一次按返回键的效果,使程序还原到第一次按下返回键之前的状态.定 ...
- python自动化运维九:Playbook
playbook:playbook 由一个或多个 ‘plays’ 组成.它的内容是一个以 ‘plays’ 为元素的列表.在 play 之中,一组机器被映射为定义好的角色.在 ansible 中,pla ...
- CentOS-7-64bit 下为firefox安装flashplayer
最近更新了Centos 7 还是有一些不习惯的 给ff安flashplayer插件时,按centos 6.x的方法时都无法成功,后来find了一下,才知道firefox还有一个64bit的文件夹: 解 ...