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,表示一个点 的坐标 ...
随机推荐
- Irrlicht 3D Engine 笔记系列之 教程4 - Movement
作者: i_dovelemon 日期: 2014 / 12 / 16 来源: CSDN 主题: Event Receiver, Animator, Framerate independent move ...
- Solaris服务管理
远程登录协议 telnet \ssh 等.当然我们可以查看谁登录过我的系统,以及可以利用ftp记录日志. 一.SMF: 服务管理工具 优点:自动恢复意外终止的服务,支持服务的依赖关系,一个服务可以有多 ...
- create a backdoor deb package
以下介绍怎样制作包括后门的deb安装包.以tree为例进行说明.利用apt-get下载安装包.--download-only表示仅仅下载不做其它处理. root@deb:~#apt-get downl ...
- LeetCode: Binary Tree Postorder Traversal [145]
[题目] Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bi ...
- iOS开发-14款状态栏(StatusBar)开源软件
本文转载至 http://mobile.51cto.com/hot-418125.htm 之前逛街看到移动做推广,有一个定位应用挺好的,合理的利用了状态栏,做了一些消息提醒和隐藏动画,自己回家就做了一 ...
- 修改live555支持mpeg2ts RTSP拉流,附代码
在很早之前的博客<用EasyDarwin进行IPTV rtsp mpeg-ts smil流的转发和分发直播服务>中,我们介绍到如何将live555支持mpeg2ts拉流,这个在IPTV领域 ...
- css zoom 属性
oom这个属性是ie专有属性,除了设置或者检索对象的缩放比例之外,它还有可以触发ie的haslayout属性,清除浮动,清除margin重叠等作用. 不过值得注意的一点就是火狐浏览器不支持zoom属性 ...
- Delphi的RTTI(许多参考链接)
RTTI(RunTime Type Information): 运行时类型信息, 就是在程序运行后也能得到类型(譬如 TButton 类)的信息. 这在早期主要用于 IDE 设计时, 譬如把一个 Bu ...
- [通信]Linux User层和Kernel层常用的通信方式
转自:https://bbs.csdn.net/topics/390991551?page=1 netlink:https://blog.csdn.net/stone8761/article/deta ...
- Linux服务器Java输出文件中文乱码
使用下面语句查看编码: String encoding = System.getProperty("file.encoding");结果输出:ANSI_X3.4-1968,从而导致 ...