挑战程序设计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个实例,所以计算输入实例与训练 ...
随机推荐
- 01_13_Struts_默认Action
01_13_Struts_默认Action 1. 配置struts默认Action <package name="default" namespace="/&quo ...
- java第八次作业:课堂上发布的前5张图片(包括匿名对象、单例模式恶汉式、自动生成对象、args[]数组使用、静态关键字)
- C# 获取Google Chrome的书签
其实这个很简单,就是读取一个在用户目录里面的一个Bookmarks文件就好了. 先建立几个实体类 public class GoogleChrome_bookMark_meta_info { publ ...
- Sql Server 查询今天,昨天,近七天....数据
今天数据: 昨天数据: 7天内数据: 30天内数据: 本月数据: 本年数据: 查询今天是今年的第几天: select datepart(dayofyear,getDate()) 查询今天是本月的第几天 ...
- C#MySQL增删改查
首先在项目中添加引用 using MySql.Data.MySqlClient; 连接字符串 private string connString="server=localhost;use ...
- Ubuntu 18.04 下用命令行安装Sublime
介绍: 添加来源: $ wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key add - $ sud ...
- 面试:如何把xxx.sh使用/etc/init.d/xxx.sh start启动,并且可以用chkconfig配置开机自启动
chkconfig原理: 1.脚本放到/etc/init.d下面,并且可执行(/etc/init.d/sshd) 需要被chkconfig管理,需要添加进去chkconfig --add sshd ...
- Pytorch学习(一)—— 自动求导机制
现在对 CNN 有了一定的了解,同时在 GitHub 上找了几个 examples 来学习,对网络的搭建有了笼统地认识,但是发现有好多基础 pytorch 的知识需要补习,所以慢慢从官网 API进行学 ...
- P3391 【模板】文艺平衡树(Splay)新板子
P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转 ...
- BZOJ 3257: 树的难题
树形DP #include<cstdio> #include<algorithm> #define rep(i,x,y) for (int i=x; i<=y; i++) ...