Official

Ex - Manhattan Christmas Tree Editorial by en_translator


First, rotate entire plane by \(45\) degrees.
Hereinafter we only use the transformed coordinates.

We consider solving it with parallel binary search. It is sufficient to solve the following problem.

Is the distance from \((a_i, b_i)\) to the \(K_i\)-th nearest Christmas tree to it less than \(\text{mid}_i\)? \(\cdots (*)\)

In order to answer \((*)\), we consider

The number of Christmas trees whose distance from \((a_i,b_i)\) is less than \(\text{mid}_i\).

This can be achieved by managing the number of Christmas trees on each \(y\) coordinate while inspecting them in the increasing order of their \(x\) coordinates.
(Roughly speaking, we will have to find the segment sum when \(x=a_i - \text{mid}_i\) and \(x=a_i + \text{mid}_i\).)

This way, \((*)\) can be solved in an \(O((N+Q)\log X)\) time, where \(X\) is the maximum coordinate.

Therefore, the original problem has been solved in a total of \(O((N+Q)(\log X)^2)\) time.

posted:
last update: