k-d tree 裸题………………

#include <algorithm>
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
int n, k, rot, nowD, din;
ll dui[205];
const ll oo=9e18;
struct Point{
int d[2], mn[2], mx[2], l, r;
int & operator[](int x){
return d[x];
}
bool operator<(const Point &x)const{
return d[nowD]<x.d[nowD];
}
}p[100005], T;
bool cmp(ll x, ll y){
return x>y;
}
struct KDTree{
Point t[100005];
void pushUp(int k){
int l=t[k].l, r=t[k].r;
for(int i=0; i<2; i++){
t[k].mn[i] = t[k].mx[i] = t[k][i];
if(l){
t[k].mn[i] = min(t[k].mn[i], t[l].mn[i]);
t[k].mx[i] = max(t[k].mx[i], t[l].mx[i]);
}
if(r){
t[k].mn[i] = min(t[k].mn[i], t[r].mn[i]);
t[k].mx[i] = max(t[k].mx[i], t[r].mx[i]);
}
}
}
int build(int l, int r, int now){
nowD = now;
int mid=(l+r)>>1;
nth_element(p+l, p+mid, p+r+1);
t[mid] = p[mid];
if(l<mid) t[mid].l = build(l, mid-1, now^1);
if(mid<r) t[mid].r = build(mid+1, r, now^1);
pushUp(mid);
return mid;
}
ll getDis(const Point &u, const Point &v){
return (ll)(u.d[1]-v.d[1])*(u.d[1]-v.d[1])+(ll)(u.d[0]-v.d[0])*(u.d[0]-v.d[0]);
}
ll simDis(int k){
ll re=0;
for(int i=0; i<2; i++){
ll tmp=max(abs(t[k].mn[i]-T[i]), abs(t[k].mx[i]-T[i]));
re += tmp * tmp;
}
return re;
}
void query(int k){
ll d=getDis(t[k], T), disl=-oo, disr=-oo;
int l=t[k].l, r=t[k].r;
if(d>dui[1]){
pop_heap(dui+1, dui+1+din, cmp);
dui[din] = d;
push_heap(dui+1, dui+1+din, cmp);
}
if(l) disl = simDis(l);
if(r) disr = simDis(r);
if(disl>disr){
if(disl>dui[1]) query(l);
if(disr>dui[1]) query(r);
}
else{
if(disr>dui[1]) query(r);
if(disl>dui[1]) query(l);
}
}
}kdt;
int main(){
cin>>n>>k;
k *= 2;
for(int i=1; i<=n; i++)
scanf("%d %d", &p[i][0], &p[i][1]);
rot = kdt.build(1, n, 0);
while(k--) dui[++din] = -oo;
for(int i=1; i<=n; i++){
T = p[i];
kdt.query(rot);
}
cout<<dui[1]<<endl;
return 0;
}

最新文章

  1. 使用FileZilla等软件搭建ftp服务器
  2. UVA 439 Knight Moves --DFS or BFS
  3. Android 画布绘图
  4. C# List 用法与示例
  5. HDOJ-1002 A + B Problem II (非负大整数相加)
  6. Week11(11月19日):补课
  7. redis源码分析之事务Transaction(上)
  8. java中获取项目在tomcat目录下的路径方法
  9. Hashmap误区
  10. Java ASM 技术简介
  11. HPU组队赛J:Ball King(线段树)
  12. c#语言函数
  13. RecyclerView--添加头部和底部
  14. Jquery 让contains不区分大小写
  15. [UE4]集合:TSet容器
  16. September 20th 2017 Week 38th Wednesday
  17. 峰Spring4学习(8)spring对事务的支持
  18. Ambari搭建hadoop错误记录
  19. BZOJ3994:约数个数和(莫比乌斯反演:求[1,N]*[1,M]的矩阵的因子个数)
  20. 管理Entity Framework中的树结构

热门文章

  1. win10+asp+access 父路径开启无效
  2. css最佳实践(reset.css)
  3. 【extjs6学习笔记】1.12 初始: Working with DOM
  4. JS整数验证
  5. pat乙级1059
  6. IOS enum(枚举)使用
  7. Ubuntu中文乱码问题
  8. 1.redis 安装
  9. question 002: dev c++ 当中如何调整字体大小?How to get the first program with C++? c++属于什么软件?
  10. 【NTT】bzoj3992: [SDOI2015]序列统计