[英]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:所以,给定一个部分持久的红黑树,这个问题很容易解决:
Now, when you need to find all points with X>a and Y>b:现在,当您需要找到所有 X>a 和 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.