[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的 考虑下标线段树,并 ...
随机推荐
- 记录一些WPF常用样式方便以后复用(转)
TextBox文本框 <Style x:Key="TextBoxStyle1" BasedOn="{x:Null}" TargetType="{ ...
- Java中的静态方法能否被重写
能重写,但没有多态:https://blog.csdn.net/ghgzczxcvxv/article/details/43966243
- Eclipse 创建类时添加继承
eclipse 中类的继承创建有两种方式: 1.手动敲代码通过 extends 关键字来继承 public class A extends B { } 2.在创建类的时候就选择好继承关系 点击 Fin ...
- php项目有负载,$_SERVER['HTTP_X_FORWARDED_FOR']函数在不同系统中获取到的值形式不一样,ios系统苹果手机只能获取到一个ip(113.87.214.xxx),而安卓手机获取到的是2个ip中间逗号隔开的形式(113.87.214.xxx , xxx.xxx.xxx.xxx)
这次由于有个抽奖活动功能,苹果手机每次都抽奖失败,安卓手机每次都抽奖失败(5台ios手机,8台Android手机). 错误日志查看是因为,抽奖用户的ip记录进数据库时出错,之前都是拿到ip直接插入数据 ...
- Jquery 监听浏览器前进后退
jQuery(document).ready(function () { if (window.history && window.history.pushState) { $(win ...
- 报错:Can't find a source file at "xxxxx“Locate the file or edit the source lookup path to include its location.
调试问题: Can't find a source file at "/tmp/TI_MKLIB6sLCzz/SRC/exit.c" Locate the file or edit ...
- 北京师范大学第十六届程序设计竞赛决赛 I 如何办好比赛
链接:https://www.nowcoder.com/acm/contest/117/I来源:牛客网 如何办好比赛 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他 ...
- [转]MySQL 经验集
-- my.ini -> 在 [mysqld] 节点下加入一行 skip-grant-tables 然后重启服务 -- 接下来无密码登录到 mysql 执行以下命令 use mysql show ...
- Oracle通过JDBC插入数据时,自增ID如何自动增长
一.通过触发器的方式 CREATE OR REPLACE TRIGGER tg_test BEFORE INSERT ON Userinfo FOR EACH ROW WHEN (new.userNo ...
- Vim编辑器基本操作学习(二)
操作符+位移 x命令可以删除一个字符,4x可以删除4个字符. dw可以删除一个word,w事实上是向后移动一个word的命令:dw可以接上一个任意一个位移命令,它将删除从当前光标开始到位移终点处的文本 ...