HDU 5992 kd-tree
还记得青岛的时候和蕾姐讨论了近三个小时也不知道这是什么东西 后来发现是kdtree 于是拖到寒假才补这个算法
写完几道模板题发现多维的kdtree查找最近也是很简单的拓展 于是很快1A了这道题 它真的只是模板 但确实是青岛的金牌题
如果如果如果如果 那么多如果 可惜没如果...
我花了很长时间去学了一个以后可能再也不会考到的算法
就当是对过去的一些..补偿吧..?
和普通的kdtree相比加了一个价钱的限制条件和一个输出最前面的id的限制条件
先建好树 然后直接查询 不用插入
对每个点判断dis的时候 如果价钱高于预期 距离就变为INF 低于预期就没有影响
如果dl 或者 dr 和当前最近的dis 相等 就继续ask以得到更小的id
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<math.h>
#include<queue>
#include<string>
using namespace std;
#define L long long
const L maxn = 200050 ;
const L INF = 999999999999999999 ;
/**
做一个三维的kdtree 进行最近距离的查找 同时记录最近的ans和最近的编号 随时进行对比
getdis函数 如果小于等于z 距离加上0 否则加上INF
*/ L n , m , cmp_d , root ;
struct node {
L l , r ;
L d[3] , Max[3] , Min[3] ;
L c ;
L id ;
}a[maxn];
bool cmp(node a , node b ){
return a.d[cmp_d] < b.d[cmp_d] ;
}
void up(L p , L k ) {
for(L i = 0; i < 3 ; i ++ ){
a[p].Min[i] = min(a[p].Min[i] , a[k].Min[i]) ;
a[p].Max[i] = max(a[p].Max[i] , a[k].Max[i]) ;
}
}
L build(L l, L r , L D) {
cmp_d = D ;
L mid = (l+r)/2 ;
nth_element(a+1+l,a+1+mid,a+1+r,cmp) ;
for(L i = 0 ;i < 3 ; i ++) a[mid].Min[i] = a[mid].Max[i] = a[mid].d[i] ;
if(l != mid) a[mid].l = build(l,mid-1,(D+1)%3) ; else a[mid].l = 0 ;
if(r != mid) a[mid].r = build(mid+1,r,(D+1)%3) ; else a[mid].r = 0 ;
if(a[mid].r)up(mid,a[mid].r);
if(a[mid].l)up(mid,a[mid].l);
return mid ;
}
L x,y,z;
L jl , ans ;
L getdis(L p) {
L res = 0 ;
if(z < a[p].Min[2]) return INF + 1 ;
if(x > a[p].Max[0]) res += (x - a[p].Max[0]) * (x - a[p].Max[0]) ;
if(x < a[p].Min[0]) res += (a[p].Min[0] - x) * (a[p].Min[0] - x) ;
if(y > a[p].Max[1]) res += (y - a[p].Max[1]) * (y - a[p].Max[1]) ;
if(y < a[p].Min[1]) res += (a[p].Min[1] - y) * (a[p].Min[1] - y) ;
return res ;
}
void ask(L p) {
L d0 = 0 ;
L dl , dr ;
if(a[p].d[2] > z)d0 += INF ;
if(d0 == 0) {
d0 += ((a[p].d[0]) - x) * ((a[p].d[0]) - x ) + ((a[p].d[1]) - y) * ((a[p].d[1]) - y ) ;
if(d0 < jl){
ans = p ;
jl = d0 ;
}
else if(d0 == jl) {
if(a[p].id < a[ans].id) {
ans = p;
}
}
}
if(a[p].l)dl = getdis(a[p].l) ; else dl = INF + 1 ;
if(a[p].r)dr = getdis(a[p].r) ; else dr = INF + 1 ;
if(dl < dr ) {
if(dl <= jl) ask(a[p].l) ;
if(dr <= jl) ask(a[p].r) ;
}
else {
if(dr <= jl) ask(a[p].r) ;
if(dl <= jl) ask(a[p].l) ;
}
}
int main(){
L t;
scanf("%lld",&t);
while(t-- ) {
scanf("%lld%lld",&n,&m) ;
for(L i=1;i<=n;i++){
for(L k=0;k<3;k++)scanf("%lld",&a[i].d[k]) ;
a[i].l = a[i].r = 0 ;
a[i].id = i ;
}
root = build(1,n,0);
for(L i=1;i<=m;i++){
scanf("%lld%lld%lld",&x,&y,&z) ;
jl = INF ;
ans = -1 ;
ask(root);
printf("%lld %lld %lld\n",a[ans].d[0],a[ans].d[1],a[ans].d[2]) ;
}
}
}
(去看了乘风破浪 感觉粉赵丽颖了QAQ
HDU 5992 kd-tree的更多相关文章
- AOJ DSL_2_C Range Search (kD Tree)
Range Search (kD Tree) The range search problem consists of a set of attributed records S to determi ...
- k-d tree 学习笔记
以下是一些奇怪的链接有兴趣的可以看看: https://blog.sengxian.com/algorithms/k-dimensional-tree http://zgjkt.blog.uoj.ac ...
- 【BZOJ-2648&2716】SJY摆棋子&天使玩偶 KD Tree
2648: SJY摆棋子 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 2459 Solved: 834[Submit][Status][Discu ...
- K-D Tree
这篇随笔是对Wikipedia上k-d tree词条的摘录, 我认为解释得相当生动详细, 是一篇不可多得的好文. Overview A \(k\)-d tree (short for \(k\)-di ...
- HDU 5513 Efficient Tree
HDU 5513 Efficient Tree 题意 给一个\(N \times M(N \le 800, M \le 7)\)矩形. 已知每个点\((i-1, j)\)和\((i,j-1)\)连边的 ...
- K-D Tree题目泛做(CXJ第二轮)
题目1: BZOJ 2716 题目大意:给出N个二维平面上的点,M个操作,分为插入一个新点和询问到一个点最近点的Manhatan距离是多少. 算法讨论: K-D Tree 裸题,有插入操作. #inc ...
- k-d Tree in TripAdvisor
Today, TripAdvisor held a tech talk in Columbia University. The topic is about k-d Tree implemented ...
- k-d tree算法
k-d树(k-dimensional树的简称),是一种分割k维数据空间的数据结构.主要应用于多维空间关键数据的搜索(如:范围搜索和最近邻搜索). 应用背景 SIFT算法中做特征点匹配的时候就会利用到k ...
- k-d tree模板练习
1. [BZOJ]1941: [Sdoi2010]Hide and Seek 题目大意:给出n个二维平面上的点,一个点的权值是它到其他点的最长距离减最短距离,距离为曼哈顿距离,求最小权值.(n< ...
- [模板] K-D Tree
K-D Tree K-D Tree可以看作二叉搜索树的高维推广, 它的第 \(k\) 层以所有点的第 \(k\) 维作为关键字对点做出划分. 为了保证划分均匀, 可以以第 \(k\) 维排名在中间的节 ...
随机推荐
- 47、ListView setSelection() 和 setSelectionFromTop()
http://blog.csdn.net/manoel/article/details/39183025 http://blog.csdn.net/a859522265/article/details ...
- 如何在MySQL客户端Navicat 上创建存储过程返回结果集
下述引用自<MySQL 必知必会> MySQL命令行客户机的分隔符 如果你使用的是MySQL命令行实用程序,应该仔细阅读此说明.默认的MySQL语句分隔符为;(正如你已经在迄今为止所使用的 ...
- TLE之前,没有一个节点叫失败!!!
我想我十八年来记忆最深刻的夜晚,大概便是在吉林大学燥热的宿舍里,穿着橘黄色的短袖,努力咽下大口大口的雪碧.那天晚上我仰躺在吉大热得几乎不能入眠的床板上,一动不动,看夏夜里打不尽的蚊子在惨白的天花板下飞 ...
- Linux 进程间通信(二)(网络IPC:套接字)
socket描述符 套接字是通信端点的抽象,创建一个套接字使用如下函数: #include <sys/socket.h> int socket(int domain, int type, ...
- A Simple Problem with Integers(线段树)
F - A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:% ...
- POJ 1789 Truck History【最小生成树简单应用】
链接: http://poj.org/problem?id=1789 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- Hystrix属性配置策略
Hystrix属性配置 Command可配参数 设置隔离策略 execution.isolation.strategy = THREAD 设置超时时间 execution.isolation.thre ...
- C#设计模式-单实例
单例模式就是保证在整个应用程序的生命周期中,在任何时刻,被指定的类只有一个实例,并为客户程序提供一个获取该实例的全局访问点. 1.经典的模式 namespace singleClass { class ...
- Lucene索引数计算
Elasticsearch默认在创建索引结束时得到5个分片及1个副本: 分片是有0-n个副本,“5个分片及1个副本”即“5个分片及5个相应分片副本”:共10个Lucene索引 副本数:指的是“单个分片 ...
- CSS3 Flex布局(容器)
一.flex-direction属性 row(默认值):主轴为水平方向,起点在左端. row-reverse:主轴为水平方向,起点在右端. column:主轴为垂直方向,起点在上沿. column-r ...