简体   繁体   English

保存点 (x_i, y_i) 的数据结构和查找 x_i > a & y_i > b 的所有点的过程

[英]Data structure that holds points (x_i, y_i) and a procedure to find all points that x_i > a & y_i > b

Given N points on a plance (x_1, y_1), ..., (x_n, y_n), need to describe a DS that holds all the points (x_i, y_i) and a procedure Farther(a, b), that will return the number of all points in the DS that satisfy x_i > a & y_i > b, within a time complexity of O(log^2(n)).给定平面上的 N 个点 (x_1, y_1), ..., (x_n, y_n),需要描述一个包含所有点 (x_i, y_i) 的 DS 和一个过程 Farther(a, b),它将返回DS 中满足 x_i > a & y_i > b 的所有点的数量,时间复杂度为 O(log^2(n))。

What I had in mind, and tried to implement is basically a DS that has two sorted arrays of the points in the plane, whilst the 1st one is sorted according to x values, and the 2nd array, is sorted according to y values.我想到并尝试实现的基本上是一个 DS,它具有平面中点的两个排序数组,而第一个数组根据 x 值排序,第二个数组根据 y 值排序。 Then retrieving all the satisfying points is easy with binary searching the smallest satisfing x value, then checking for these points whether y values are also satisfing.然后通过二进制搜索最小的满足 x 值来检索所有满足点很容易,然后检查这些点是否 y 值也满足。 This is the intuitive solution but it takes O(nlogn) time.这是直观的解决方案,但需要 O(nlogn) 时间。

Unfortunately, didn't manage to come up with a better solution.不幸的是,没有设法提出更好的解决方案。

As long as you don't need to update the structure dynamically, I think that standard solution is a partially persistent red-black tree or similar search tree.只要您不需要动态更新结构,我认为标准解决方案是部分持久的红黑树或类似的搜索树。

A partially persistent data structure is one that can be modified without losing access to the previous versions.部分持久的数据结构是一种可以在不失去对先前版本的访问权限的情况下进行修改的数据结构。 These are very common in purely functional languages like Haskell -- since every object is immutable, "modificatons" are just new versions that share as much data as possible with the old version, and all versions can still be used.这些在像 Haskell 这样的纯函数式语言中非常常见——因为每个对象都是不可变的,“修改”只是与旧版本共享尽可能多的数据的新版本,并且所有版本仍然可以使用。 Chris Okasaki's red-black trees are popular. Chris Okasaki 的红黑树很受欢迎。

So, given a partially persistent red-black tree, the problem is easy to solve:所以,给定一个部分持久的红黑树,这个问题很容易解决:

  1. Sort in reverse order by X按 X 倒序排列
  2. Insert points into the tree, sorted by Y, and keeping track of the version created for each point.将点插入树中,按 Y 排序,并跟踪为每个点创建的版本。

Now, when you need to find all points with X>a and Y>b:现在,当您需要找到所有 X>a 和 Y>b 的点时:

  1. Binary search in the X-sorted point list to find the last one with X>a.在 X 排序的点列表中进行二分搜索以找到 X>a 的最后一个。
  2. Get the associated tree version, which contains only the points with X>a.获取关联的树版本,它只包含 X>a 的点。
  3. Traverse the tree from max Y, stopping if you find one with Y<=b.从最大 Y 遍历树,如果找到 Y<=b 则停止。

That's it -- very easy except that the tree structure itself is a little tricky, and it can be tough to find a library version in some languages.就是这样——很简单,只是树结构本身有点棘手,而且很难找到某些语言的库版本。 Each search takes O(log N) time, and the data structure itself takes either O(N) or O(N log N) space depending on the implementation.每次搜索需要 O(log N) 时间,数据结构本身需要 O(N) 或 O(N log N) 空间,具体取决于实现。

Kd trees can also be used to solve this problem, and usually perform well, although they don't provide the same complexity guarantees. Kd 树也可以用来解决这个问题,并且通常表现良好,尽管它们不能提供相同的复杂性保证。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定一部电梯,其最大重量和n个人的重量为x_i,请找出所需的最小乘车次数 - Given an elevator with max weight and n people with x_i weights, find out minimum number of rides needed 如何找到包含所有点或与y = x轴对称的点的最小矩形? - How can I find the least rect, which contains all points or the point symmetrical to the y=x axis? 如何以网格方式为两个以上的资产生成约束 \sum{x_i} = 1 的权重? - How to generate weights with constraint \sum{x_i} = 1 for more than two assets in a meshgrid fashion? 如何在二进制图像中找到线段的x,y的中间点? - How can I find the middle points of x, y of a line segment in a binary image? 如何从一堆点中找到包含三角形的最小点P(x,y)? - How do i find the smallest point P(x,y) containing triangle from a bunch of points? 给定两个未排序的 arrays 找到 A[i] &gt; X 和 B[i] &gt; Y 的对数 - Given two unsorted arrays find the number of pairs where A[i] > X and B[i] > Y 如何在 C# 中将四个 2D 网格点与平滑曲线连接起来,并在某个时间返回 x 和 y 位置? - How do I connect four 2D grid points with a smooth curve in C# and give the x and y location back at a certain time? 如何找到通过给定点(a,b)和(x,0)的线的最大y轴截距? - How to find largest y-intercept of lines through given points (a, b) and (x, 0)? 给定10个函数y = a + bx和1000个(x,y)数据点四舍五入到整数,如何导出10个最佳(a,b)元组? - given 10 functions y=a+bx and 1000's of (x,y) data points rounded to ints, how to derive 10 best (a,b) tuples? 找到一个圆的中心(x和y位置),只有2个随机点和凸起 - Find the center of a circle (x and y position) with only 2 random points and bulge
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM