loj2043 「CQOI2016」K 远点对
2024-09-18 07:05:54
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;
}
最新文章
- 使用FileZilla等软件搭建ftp服务器
- UVA 439 Knight Moves --DFS or BFS
- Android 画布绘图
- C# List 用法与示例
- HDOJ-1002 A + B Problem II (非负大整数相加)
- Week11(11月19日):补课
- redis源码分析之事务Transaction(上)
- java中获取项目在tomcat目录下的路径方法
- Hashmap误区
- Java ASM 技术简介
- HPU组队赛J:Ball King(线段树)
- c#语言函数
- RecyclerView--添加头部和底部
- Jquery 让contains不区分大小写
- [UE4]集合:TSet容器
- September 20th 2017 Week 38th Wednesday
- 峰Spring4学习(8)spring对事务的支持
- Ambari搭建hadoop错误记录
- BZOJ3994:约数个数和(莫比乌斯反演:求[1,N]*[1,M]的矩阵的因子个数)
- 管理Entity Framework中的树结构
热门文章
- win10+asp+access 父路径开启无效
- css最佳实践(reset.css)
- 【extjs6学习笔记】1.12 初始: Working with DOM
- JS整数验证
- pat乙级1059
- IOS enum(枚举)使用
- Ubuntu中文乱码问题
- 1.redis 安装
- question 002: dev c++ 当中如何调整字体大小?How to get the first program with C++? c++属于什么软件?
- 【NTT】bzoj3992: [SDOI2015]序列统计