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,表示一个点 的坐标 ...
随机推荐
- 最长连续序列(Longest Consecutive Sequence)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- idea设置提示不区分大小写
- 排序&匿名函数
nums=[11,34234,23,344,123,1,23,124,523,4,12342341,423,43545] nums.sort() print(nums) #这个就是排序,从小到到 匿名 ...
- POJ 3335 Rotating Scoreboard(半平面交 多边形是否有核 模板)
题目链接:http://poj.org/problem? id=3335 Description This year, ACM/ICPC World finals will be held in a ...
- 基于flask做权限控制
和Django实现的原理类似,有时间补充
- linux cat命令(转载)
来源:http://blog.sina.com.cn/s/blog_52f6ead0010127xm.html 1.cat 显示文件连接文件内容的工具: cat 是一个文本文件查看和连接工具. 查看一 ...
- [ios]objective-c中Category类别(扩展类)专题总结
本文转载至 http://yul100887.blog.163.com/blog/static/20033613520126333344127/ objective-c类别的作用?通过类别的方式, ...
- unity 接触一个月的感受和心得
unity scrollview 遇到的问题 一个scrollview作为翻页,这样的效果调整. 一页上面有三个scrollview,这三个scrollview上的items不受下层整个页面的scro ...
- 6 Maven聚合与集成
Maven的聚合特性能够把项目的各个模块聚合在一起构件,而Maven的继承特性能够帮助抽取各个模块相同的依赖和插件等配置,简化POM的同时,还能促进各个模块配置的一致性. 1.聚合 为 ...
- java web service
1.编写服务代码 服务代码提供了两个函数,分别为sayHello和sayHelloToPerson,源代码如下: /* * File name: HelloService.java * * Versi ...