简体   繁体   中英

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)).

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. 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. This is the intuitive solution but it takes O(nlogn) time.

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. Chris Okasaki's red-black trees are popular.

So, given a partially persistent red-black tree, the problem is easy to solve:

  1. Sort in reverse order by X
  2. Insert points into the tree, sorted by Y, and keeping track of the version created for each point.

Now, when you need to find all points with X>a and Y>b:

  1. Binary search in the X-sorted point list to find the last one with X>a.
  2. Get the associated tree version, which contains only the points with X>a.
  3. Traverse the tree from max Y, stopping if you find one with 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.

Kd trees can also be used to solve this problem, and usually perform well, although they don't provide the same complexity guarantees.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM