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,sxitxisyityi.

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 ≤ xysxtxsyty ≤ 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树的更多相关文章

  1. 利用KD树进行异常检测

    软件安全课程的一次实验,整理之后发出来共享. 什么是KD树 要说KD树,我们得先说一下什么是KNN算法. KNN是k-NearestNeighbor的简称,原理很简单:当你有一堆已经标注好的数据时,你 ...

  2. 2016 ICPC青岛站---k题 Finding Hotels(K-D树)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5992 Problem Description There are N hotels all over ...

  3. kd树和knn算法的c语言实现

    基于kd树的knn的实现原理可以参考文末的链接,都是一些好文章. 这里参考了别人的代码.用c语言写的包括kd树的构建与查找k近邻的程序. code: #include<stdio.h> # ...

  4. PCL点云库:Kd树

    Kd树按空间划分生成叶子节点,各个叶子节点里存放点数据,其可以按半径搜索或邻区搜索.PCL中的Kd tree的基础数据结构使用了FLANN以便可以快速的进行邻区搜索.FLANN is a librar ...

  5. KNN算法与Kd树

    最近邻法和k-近邻法 下面图片中只有三种豆,有三个豆是未知的种类,如何判定他们的种类? 提供一种思路,即:未知的豆离哪种豆最近就认为未知豆和该豆是同一种类.由此,我们引出最近邻算法的定义:为了判定未知 ...

  6. k临近法的实现:kd树

    # coding:utf-8 import numpy as np import matplotlib.pyplot as plt T = [[2, 3], [5, 4], [9, 6], [4, 7 ...

  7. 从K近邻算法谈到KD树、SIFT+BBF算法

    转自 http://blog.csdn.net/v_july_v/article/details/8203674 ,感谢july的辛勤劳动 前言 前两日,在微博上说:“到今天为止,我至少亏欠了3篇文章 ...

  8. 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][ ...

  9. k近邻法的C++实现:kd树

    1.k近邻算法的思想 给定一个训练集,对于新的输入实例,在训练集中找到与该实例最近的k个实例,这k个实例中的多数属于某个类,就把该输入实例分为这个类. 因为要找到最近的k个实例,所以计算输入实例与训练 ...

随机推荐

  1. Unity学习之路——主要类

    学习https://blog.csdn.net/VRunSoftYanlz/article/details/78881752 1.Component类gameObject:组件附加的游戏对象.组件总是 ...

  2. 快速排序和快速选择(quickSort and quickSelect)算法

    排序算法:快速排序(quicksort)递归与非递归算法 TopK问题:快速选择(quickSelect)算法 import java.util.*; import java.lang.*; publ ...

  3. LeetCode939

    问题:最小面积矩形 给定在 xy 平面上的一组点,确定由这些点组成的矩形的最小面积,其中矩形的边平行于 x 轴和 y 轴. 如果没有任何矩形,就返回 0. 示例 1: 输入:[[1,1],[1,3], ...

  4. paper:synthesizable finite state machine design techniques using the new systemverilog 3.0 enhancements 之 standard verilog FSM conding styles(三段式)

    Three always block style with registered outputs(Good style)

  5. 【php】 phpword下载文件问题

    这个问题是在 科锐国际 工作过程中发现的 word文档的名字(有汉字和空格)在windows系统上遍历是查不到文件的,但是在linux系统上市可以的压缩包里面的中文名word文档,如果出现汉字和空格, ...

  6. 拼接Python字符串最常见的六种方式

    最常见的六种方式拼接Python字符串 字符串是所有编程语言中都有的基本变量的类型,程序员基本每天都在和字符串打交道. 每种字符串拼接方式的使用场景各不相同,我们可以在开发过程中灵活运用. 一.用逗号 ...

  7. python里字典的用法介绍

    一.什么是字典 字典是python里的一种数据类型,特点是元素的无序性,和键key的唯一性.字典的创建方法是{key:values},字典里的键key只能是不可变的数据类型(整型,字符串或者是元组), ...

  8. eclipse使用技巧的网站收集——转载(二)

    写代码离不开文本编辑器,看代码也离不开,iar和keil编辑和阅读简直一般般了,因此使用eclipse可以看看代码,提高效率.网上有几个博客的文章,这里收集一下,以备忘. 以下文章转载自:http:/ ...

  9. selenium 自动化测试 Chrome 大于 63 版本 不能重定向问题解决办法

    Chrome 一些信息: Chrome 63 以后,浏览器默认屏蔽了重定向 Chrome 63 版本,设置了禁止更新,有些情况还是会更新到最新版本 解决过程: 在博客上查到  selenium 里 给 ...

  10. Scala学习-02-方法

    算数和操作符重载 所有的操作符都是方法. a + b 是一种缩写形式  :  a .+ b “+”是方法名(操作符重载) ++和—— Scala中并没有“++”和“——”.需要使用“+=”和“-=” ...