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. 传输途径 ath9k层到硬件层

    这里只写了ath9k层到虚拟硬件层的一些东西,mac层的没有整理. 传输途径主要从ath9k_tx() --->ath_tx_start() --->ath_tx_send_normal( ...

  2. 第二单元OO总结

    目录 前言 一.第一次作业分析 1. UML及复杂度分析 二.第二次作业分析 1. UML及复杂度分析 2. 性能优化 2.1 楼层类的实现 2.2 调度算法 3. bug分析 三.第三次作业分析 1 ...

  3. debian常用指令

    查看软件xxx安装内容 dpkg -L xxx 查找软件 apt-cache search 正则表达式 查找文件属于哪个包 dpkg -S filename apt-file search filen ...

  4. React 基础知识总结

    data-id="1190000016885142" data-license=""> 一.Node.js Node.js并不是一个JavaScript框 ...

  5. django知识分支_1

    django知识分支 1.Cookie工作流程: 浏览器向服务器发出请求,服务器接收到浏览器的请求进行处理,服务器设置一个cookie发送给浏览器,浏览器将cookie保存,当需要再次登录的时候,浏览 ...

  6. python并发编程之线程(创建线程,锁(死锁现象,递归锁),GIL锁)

    什么是线程 进程:资源分配单位 线程:cpu执行单位(实体),每一个py文件中就是一个进程,一个进程中至少有一个线程 线程的两种创建方式: 一 from threading import Thread ...

  7. debiand上安装thunderbird

    deb包下载地址 http://sourceforge.net/projects/ubuntuzilla/files/mozilla/apt/pool/main/t/thunderbird-mozil ...

  8. 64位程序调用32DLL解决方案

    最近做一个.NETCore项目,需要调用以前用VB6写的老程序,原本想重写,但由于其调用了大量32DLL,重写后还需要编译为32位才能运行,于是干脆把老代码整个封装为32DLL,然后准备在64位程序中 ...

  9. 【MySQL】MySQL基础

    一.基本语法 [MySQL目录结构]●bin目录,存储可执行文件●data目录,存储数据文件●docs,文档●include目录,存储包含的头文件●lib目录,存储库文件●share,错误信息和字符集 ...

  10. 两种图片延迟加载的方法总结jquery.scrollLoading.js与jquery.lazyload.js

    估计网上能查到的最多的两种图片延迟加载方法就是jquery.scrollLoading.js与jquery.lazyload.js了,其中jquery.lazyload.js的调用方法因为有网友爆出的 ...