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. MySQL - INSERT 集合

    范例1: INSERT INTO t_table SELECT ot.* FROM t_other_table ot WHERE ot.is_sent = ? and ot.insert_time & ...

  2. 什么是python中的元类

    所属网站分类: python高级 > 面向对象 作者:goodbody 原文链接: http://www.pythonheidong.com/blog/article/11/ 来源:python ...

  3. JDK1.8 HashMap$TreeNode.rotateLeft 红黑树左旋

    红黑树介绍 1.节点是红色或黑色. 2.根节点是黑色. 3.每个叶子节点都是黑色的空节点(NIL节点). 4 每个红色节点的两个子节点都是黑色.(从每个叶子到根的所有路径上不能有两个连续的红色节点) ...

  4. hdu 5459

    Problem Description I've sent Fang Fang around 201314 text messages in almost 5 years. Why can't she ...

  5. clr(Windows 运行时和公共语言运行时)

    Windows 运行时   编译器使用 COM 引用计数机制来确定对象是否不再使用并可以删除. 因为从 Windows 运行时接口派生的对象实际上是 COM 对象,所以这是可行的. 在创建或复制对象时 ...

  6. [POJ 1000] A+B Problem 经典水题 C++解题报告 JAVA解题报告

        A+B Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 311263   Accepted: 1713 ...

  7. Selenium WebDriver- 指定页面加载时间

    #encoding=utf-8 import unittest import time from selenium import webdriver from selenium.webdriver i ...

  8. 如何理解C4.5算法解决了ID3算法的偏向于选择取值较多的特征问题

    如何理解C4.5算法解决了ID3算法的偏向于选择取值较多的特征问题 考虑一个极端情况,某个属性(特征)的取值很多,以至于每一个取值对应的类别只有一个.这样根据\[H(D) - H(D|A)\]可以得知 ...

  9. Django模板导入和替换、以及对数据库的增加、查看

    静态文件引入的3中方式:例如对html模板里面对css样式的引入 STATIC_URL = '/static666/'STATICFILES_DIR=[ os.path.join(BASE_DIR,' ...

  10. 顺序表ans链性表

    #include<stdio.h>#include<malloc.h>#include<string.h>typedef int ElemType;typedef ...