挑战程序设计2 KD树
Range Query - Range Search (kD Tree)
Time Limit : 1 sec, Memory Limit : 262144 KB
Japanese version is here
Range Search (kD Tree)
The range search problem consists of a set of attributed records S to determine which records from Sintersect with a given range.
For n points on a plane, report a set of points which are within in a given range. Note that you do not need to consider insert and delete operations for the set.
Input
n
x0 y0
x1 y1
:
xn−1 yn−1
q
sx0 tx0 sy0 ty0
sx1 tx1 sy1 ty1
:
sxq−1 txq−1 syq−1 tyq−1
The first integer n is the number of points. In the following n lines, the coordinate of the i-th point is given by two integers xi and yi.
The next integer q is the number of queries. In the following q lines, each query is given by four integers,sxi, txi, syi, tyi.
Output
For each query, report IDs of points such that sxi ≤ x ≤ txi and syi ≤ y ≤ tyi. The IDs should be reported in ascending order. Print an ID in a line, and print a blank line at the end of output for the each query.
Constraints
- 0 ≤ n ≤ 500,000
- 0 ≤ q ≤ 20,000
- -1,000,000,000 ≤ x, y, sx, tx, sy, ty ≤ 1,000,000,000
- sx ≤ tx
- sy ≤ ty
- For each query, the number of points which are within the range is less than or equal to 100.
Sample Input 1
6
2 1
2 2
4 2
6 2
3 3
5 4
2
2 4 0 4
4 10 2 5
Sample Output 1
0
1
2
4 2
3
5 题意:给定二维平面内n个点的坐标,查询规定区域内的点的坐标
思路:kD树的运用
实现代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
#include<set>
#include<string>
#include<queue>
#include<cmath>
using namespace std;
#define INF 0x3f3f3f3f
const int N_MAX = ;
int n,np=;
struct point {
int x, y,id;
point() {}
point(int x,int y):x(x),y(y) {}
bool operator <(const point &p) const{
return id < p.id;
}
}P[N_MAX];
vector<point>vec;//别忘清空 struct KD_tree { int L, R, location;//location代表树中当前的节点在原来序列中的位置
KD_tree() {}
KD_tree(int L,int R,int location):L(L),R(R),location(location) {}
}T[N_MAX];
bool cmp_x(const point &a,const point&b) {
return a.x < b.x;
}
bool cmp_y(const point &a, const point&b) {
return a.y < b.y;
} int make2DTree(int l,int r,int depth) {//区间[l,r)
if (!(l < r))return -;
int mid = (l+r) / ;
if (depth & )sort(P + l, P + r, cmp_y);
else sort(P + l, P + r, cmp_x);
int t = np++;
T[t].location = mid;
T[t].L = make2DTree(l, mid, depth + );
T[t].R = make2DTree(mid + , r, depth + );
return t;
} void find(int v,int sx,int sy,int tx,int ty,int depth) {//判断节点v是否在区域内
int x = P[T[v].location].x;
int y = P[T[v].location].y;
if (x >= sx&&x <= tx&&y >= sy&&y <= ty) {
vec.push_back(P[T[v].location]);
}
if (!(depth & )) {
if (T[v].L != - && x >= sx) {
find(T[v].L, sx, sy, tx, ty, depth + );
}
if (T[v].R != - && x <= tx) {
find(T[v].R, sx, sy, tx, ty, depth + );
}
}
else{
if (T[v].L != - && y >= sy) {
find(T[v].L, sx, sy, tx, ty, depth + );
}
if (T[v].R != - && y <= ty) {
find(T[v].R, sx, sy, tx, ty, depth + );
}
}
} int main() {
while (scanf("%d",&n)!=EOF) { np = ;
for (int i = ; i < n;i++) {
scanf("%d%d",&P[i].x,&P[i].y);
P[i].id = i;
}
int root = make2DTree(, n, );
cout <<"root:"<<root << endl;
int q;
scanf("%d",&q);
while (q--) {
vec.clear();
int sx, tx, sy, ty;
scanf("%d%d%d%d",&sx,&tx,&sy,&ty);
find(root, sx, sy, tx, ty, );
sort(vec.begin(), vec.end());
for (int i = ; i < vec.size();i++) {
printf("%d\n",vec[i].id);
}
puts("");
}
}
return ;
}
挑战程序设计2 KD树的更多相关文章
- 利用KD树进行异常检测
软件安全课程的一次实验,整理之后发出来共享. 什么是KD树 要说KD树,我们得先说一下什么是KNN算法. KNN是k-NearestNeighbor的简称,原理很简单:当你有一堆已经标注好的数据时,你 ...
- 2016 ICPC青岛站---k题 Finding Hotels(K-D树)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5992 Problem Description There are N hotels all over ...
- kd树和knn算法的c语言实现
基于kd树的knn的实现原理可以参考文末的链接,都是一些好文章. 这里参考了别人的代码.用c语言写的包括kd树的构建与查找k近邻的程序. code: #include<stdio.h> # ...
- PCL点云库:Kd树
Kd树按空间划分生成叶子节点,各个叶子节点里存放点数据,其可以按半径搜索或邻区搜索.PCL中的Kd tree的基础数据结构使用了FLANN以便可以快速的进行邻区搜索.FLANN is a librar ...
- KNN算法与Kd树
最近邻法和k-近邻法 下面图片中只有三种豆,有三个豆是未知的种类,如何判定他们的种类? 提供一种思路,即:未知的豆离哪种豆最近就认为未知豆和该豆是同一种类.由此,我们引出最近邻算法的定义:为了判定未知 ...
- k临近法的实现:kd树
# coding:utf-8 import numpy as np import matplotlib.pyplot as plt T = [[2, 3], [5, 4], [9, 6], [4, 7 ...
- 从K近邻算法谈到KD树、SIFT+BBF算法
转自 http://blog.csdn.net/v_july_v/article/details/8203674 ,感谢july的辛勤劳动 前言 前两日,在微博上说:“到今天为止,我至少亏欠了3篇文章 ...
- bzoj 3489: A simple rmq problem k-d树思想大暴力
3489: A simple rmq problem Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 551 Solved: 170[Submit][ ...
- k近邻法的C++实现:kd树
1.k近邻算法的思想 给定一个训练集,对于新的输入实例,在训练集中找到与该实例最近的k个实例,这k个实例中的多数属于某个类,就把该输入实例分为这个类. 因为要找到最近的k个实例,所以计算输入实例与训练 ...
随机推荐
- java基础—接口概念
一.接口的概念 JAVA是只支持单继承的,但现实之中存在多重继承这种现象,如“金丝猴是一种动物”,金丝猴从动物这个类继承,同时“金丝猴是一种值钱的东西”,金丝猴从“值钱的东西”这个类继承,同时“金丝猴 ...
- 01_1_Struts环境搭建
01_1_Struts环境搭建 1. MyEclipse配置部分 1.1创建项目 新建new—>Project—>Web Project—>Project Name(配置项目名)—& ...
- Python学习笔记5(函数)
[摘要]本文详细介绍python中的函数,以及与之相关的参数和作用域的概念,并介绍递归的概念以及在程序中的应用. 函数定义 定义函数要用函数定义语句def.如下: def hello(name): r ...
- 【启发式拆分】bzoj4059: [Cerc2012]Non-boring sequences
这个做法名字是从武爷爷那里看到的…… Description 我们害怕把这道题题面搞得太无聊了,所以我们决定让这题超短.一个序列被称为是不无聊的,仅当它的每个连续子序列存在一个独一无二的数字,即每个子 ...
- (66)zabbix导入/导出配置文件
通过导入/导出zabbix配置文件,我们可以将自己写好的模板等配置在网络上分享,我们也可以导入网络上分享的配置文件 配置文件有两种格式,分为为xml与json,通过zabbix管理界面可以导出xml, ...
- Python爬虫,爬取实验楼全部课程
目的: 使用requests库以及xpath解析进行实验楼所有课程,存入MySQL数据 库中. 准备工作: 首先安装,requests库,lxml库,以及peewee库.在命令行模式,使用以下命令. ...
- zoj 4057
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> ...
- Python之多线程与多进程(二)
多进程 上一章:Python多线程与多进程(一) 由于GIL的存在,Python的多线程并没有实现真正的并行.因此,一些问题使用threading模块并不能解决 不过Python为并行提供了一个替代方 ...
- c#利用反射实现对类中的常量进行取值和对应常量的注释
C#利用反射实现对类中的常量进行取值和对应常量的注释 项目示例:https://gitee.com/dhclly/IceDog.GenerateErrorCode 因为业务需要,项目中有大量的错误码, ...
- VC6.0与Office2007~2010不兼容问题及解决方法
一.问题描述 启动打开文件对话框中,在 Visual C++ 使用的键盘快捷键或从文件菜单上将导致以下错误: 在 DEVSHL 中的访问冲突 (0xC0000005).在 0x5003eaed 的 D ...