1

Consider $N$ points that are located on a 2D plane where the $i$-th point’s location is denoted as $(x_i, y_i)$.

Is there any efficient algorithm that can compute $d_i$ that is defined as the number of points with $x<x_i$ and $y<y_i$ for all $i$? In other words, we would like to count the number of points on the lower left of point $i$. To simplify, assume there’s no pair of points that have the exact same $x$ and $y$.

By “efficient”, I mean the complexity should be $O(N\log N)$ (in runtime and memory) or better.

EDIT: Adding the specification for the algorithm: The input to the algorithm is a list of the coordinate of the points (not necessarily sorted in any order) and the output will be a list where the $i$-th element is the number of points in the lower left of the $i$-th points.

D.W.
  • 167,959
  • 22
  • 232
  • 500
Firman
  • 163
  • 9

1 Answers1

2

Sort the points from left to right. Then scan from left to right, keeping the points met sorted by ordinate in a balanced search tree.

Now every time you insert a new point, the tree contains all points to its left and by finding the insertion point in the tree you obtain the number of lower points.

These two steps each take time $O(n\log n)$. You cannot do better because the solution to this problem would allow you to sort a list of reals $x_i$ by processing the points $(x_i,x_i)$.