nhtrnm's blog

By nhtrnm, history, 7 years ago, In English

Given n ≤ 106 points with integer coordinates on a 2D plane, for each point compute the number of points with coordinates greater than those of the current one.

I can only think of a solution in which sorts the points using x coordinate, and then for every point count the number of points after it with greater y coordinate. That can be done by using Fenwick tree or segment tree.

Can we do better than that? Can we do something simpler without using a segment tree or Fenwick tree?

  • Vote: I like it
  • +20
  • Vote: I do not like it

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by nhtrnm (previous revision, new revision, compare).

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by nhtrnm (previous revision, new revision, compare).

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Compress them and apply sqrt-decomposition? :D

A little bit simpler imo

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

There's a nice solution for this problem using merge sort. I'm sure it works when all coordinates are distinct, but I think we can generalize it.

When I get home I'll explain it better.

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Yeah, this problem can be solved with merge sort which I haven't thought of before: link.

»
7 years ago, # |
Rev. 2   Vote: I like it +18 Vote: I do not like it

No, you are correct that O(n log n) is the best you can do. The reason is because if you take the special case that all x-coordinates are distinct, then the problem becomes equivalent to inversion counting, which requires at least O(n log n) time.

See here for reason: https://math.stackexchange.com/a/2112513