[hdu4347]The Closest M Points(线段树形式kd-tree)
解题关键:kdtree模板题,距离某点最近的m个点。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<queue>
#define sq(x) (x)*(x)
using namespace std;
typedef long long ll;
const int N=; int idx,k,n,m,q;
struct node{
int x[];
bool operator<(const node &u)const{
return x[idx]<u.x[idx];
}
}P[N];
//线段树形式kd-tree
typedef pair<double,node>PDN;
priority_queue<PDN>que;
struct KD_Tree{
int sz[N<<];
node p[N<<];
void build(int rt,int l,int r,int dep){
if(l>r) return;
int mid=(l+r)>>;
idx=dep%k;sz[rt]=r-l;
sz[rt<<]=sz[rt<<|]=-;
nth_element(P+l,P+mid,P+r+);
p[rt]=P[mid];
build(rt<<,l,mid-,dep+);
build(rt<<|,mid+,r,dep+);
} void query(int i,int m,int dep,node a){//寻找m个。
if(sz[i]==-) return;
PDN tmp=PDN(,p[i]);//新建的tmp,first更新为0了
for(int j=;j<k;j++)
tmp.first+=sq(tmp.second.x[j]-a.x[j]);//距离目标点的距离
int lc=i<<,rc=i<<|,dim=dep%k,flag=;
if(a.x[dim]>=p[i].x[dim]) swap(lc,rc);
if(~sz[lc]) query(lc,m,dep+,a);//~sz,sz!=-1
if(que.size()<m) que.push(tmp),flag=;
else{
if(tmp.first<que.top().first) que.pop(),que.push(tmp);
if(sq(a.x[dim]-p[i].x[dim])<que.top().first) flag=;//求到面的距离,空间相交
}
if(~sz[rc]&&flag) query(rc,m,dep+,a);
}
}KDT; int main(){
while(~scanf("%d%d",&n,&k)){
for(int i=;i<n;i++)
for(int j=;j<k;j++)
scanf("%d",&P[i].x[j]);
KDT.build(,,n-,);
scanf("%d",&q);
while(q--){
node now;
for(int i=;i<k;i++)
scanf("%d",&now.x[i]);
scanf("%d",&m);
KDT.query(,m,,now);
node pp[];
int t=;
while(!que.empty()){
pp[++t]=que.top().second;
que.pop();
}
printf("the closest %d points are:\n",m);
for(int i=m;i>;i--) for(int j=;j<k;j++) printf("%d%c",pp[i].x[j],j==k-?'\n':' ');
}
}
return ;
}
[hdu4347]The Closest M Points(线段树形式kd-tree)的更多相关文章
- Codeforces 1140F Extending Set of Points 线段树 + 按秩合并并查集 (看题解)
Extending Set of Points 我们能发现, 如果把x轴y轴看成点, 那么答案就是在各个连通块里面的x轴的个数乘以y轴的个数之和. 然后就变成了一个并查集的问题, 但是这个题目里面有撤 ...
- CodeForces 19D Points (线段树+set)
D. Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- VK Cup 2015 - Qualification Round 1 D. Closest Equals 离线+线段树
题目链接: http://codeforces.com/problemset/problem/522/D D. Closest Equals time limit per test3 secondsm ...
- Codeforces Beta Round #19D(Points)线段树
D. Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- CF 19D - Points 线段树套平衡树
题目在这: 给出三种操作: 1.增加点(x,y) 2.删除点(x,y) 3.询问在点(x,y)右上方的点,如果有相同,输出最左边的,如果还有相同,输出最低的那个点 分析: 线段树套平衡树. 我们先离散 ...
- [Codeforces19D]Points 线段树
大致题意: 给出n个询问,每次询问有三种: 1.往平面上加一个点 2.删除平面上的一个点 3.给出一个点p,查询平面上某点q,使得q.x>p.x且q.y>p.y,输出x轴坐标最小的q,若有 ...
- [hdu4347]The Closest M Points(平衡树式kdtree)
解题关键:模板题(结合起来了) #include<iostream> #include<cstdio> #include<cstring> #include< ...
- Codeforces 1140F Extending Set of Points (线段树分治+并查集)
这题有以下几个步骤 1.离线处理出每个点的作用范围 2.根据线段树得出作用范围 3.根据分治把每个范围内的点记录和处理 #include<bits/stdc++.h> using name ...
- 【HDU6621】K-th Closest Distance【线段树】
题目大意:给你一堆数,每次询问区间[l,r]中离p第k小的|ai-p| 题解:考虑二分答案,对于每个可能的答案,我们只需要看在区间[l,r]里是否有≥k个比二分的答案还要接近于p的 考虑下标线段树,并 ...
随机推荐
- ballerina 学习十二 变量
ballerina 有两种方式进行变量的定义,类型加上名称以及初始值.,使用var 关键字 简单例子 代码 import ballerina/io; // 全局public 变量,使用类型定义 pub ...
- guaua学习,工具专题
Preconditions 1,http://www.cnblogs.com/peida/p/Guava_Preconditions.html 1 .checkArgument(boolean) : ...
- vim删除空行和注释
vim删除空行和注释 来源: http://jpuyy.com/2015/06/vim-delete-lines-using-regexp.html 删除空行 :g/^$/d 删除空行以及只有空格的 ...
- vue怎么自定义指令??
最近看看vue中自定义指令,感觉vue的指令和angular1的指令相差较大 <script> //指令钩子函数: /* bind 只调用一次,指令第一次绑定到元素的时调用 inserte ...
- 自定义DelegatingHandler为ASP.NET Web Api添加压缩与解压的功能
HTTP协议中的压缩 Http协议中使用Accept-Encoding和Content-Encoding头来表示期望Response内容的编码和当前Request的内容编码.而Http内容的压缩其实是 ...
- [转载,感觉写的非常详细]DUBBO配置方式详解
[转载,感觉写的非常详细]DUBBO配置方式详解 原文链接:http://www.cnblogs.com/chanshuyi/p/5144288.html DUBBO 是一个分布式服务框架,致力于提供 ...
- 为什么我tracert经过H3C设备的时候,老是*号,不回包
两条命令搞定 ip unreachables en ip ttl-expires enable
- 5月11日上课笔记-js简介
一.js 1.javascript简介: 是一门脚本语言(弱类型编程语言)(php是脚本语言) 编程语言如java,先编译后运行 脚本语言不需要编译 作用: 表单验证 页面特效 语法:和Java类似, ...
- java.lang.ArrayIndexOutOfBoundsException异常分析及解决
这是一个非常常见的异常,从名字上看是数组下标越界错误,解决方法就是查看为什么下标越界. 下面是一个错误示例: Exception in thread "main" java.lan ...
- python开发_自己开发的一个小游戏
先看看游戏的运行效果: 看完游戏的运行情况,你可能对游戏有了一定了了解: #运行游戏后,玩家首先要进行语音的选择,1选择英语,2选择汉语,其他则默认选择英语 #根据玩家选择的语音,进入不同的语音环境 ...