Editorial for COCI '06 Contest 6 #6 Prostor


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

We'll say a rectangle with coordinates (x_1, y_1, z_1, x_2, y_2, z_2) is of type X if it is "thin" in the x dimension, i.e. if x_1 = x_2. Rectangles of types Y and Z are similarly defined.

Define a function count(X, Y) that does the following:

Select all rectangles of types X and Y and count how many pairs of rectangles there are such that at least one of them is of type X and they share a point.

The overall solution is count(X, Y) + count(Y, Z) + count(Z, X).

Algorithm for count(X, Y):

Imagine that the y coordinate represents time. Let time flow and observe the (x, z) plane representing space at some moment in time. Rectangles of type X become vertical line segments while rectangles of type Y remain rectangles.

Rectangles of type X have some positive lifetime (because y_1 < y_2), while rectangles of type Y exist at only one instant (because y_1 = y_2).

As time flows, three types of events can occur:

  1. A type X rectangle starts
  2. A type Y rectangle starts and ends immediately
  3. A type X rectangle ends

We sort the events by time and process them one by one.

To count all pairs of rectangles sharing a point, such that one is of type X and another is of type Y, we need to, whenever a rectangle (x_1, y, z_1, x_2, y, z_2) of type Y comes to life, answer the question "how many vertical line segments in a set intersect the rectangle (x_1, z_1, x_2, z_2)?".

To count all pairs of rectangles sharing a point, such that one is of type X and another is of type X, we need to, whenever a rectangle (x_1, y, z_1, x_2, y, z_2) of type X comes to life, answer the question "how many vertical line segments in a set intersect the (degenerate) rectangle (x, z_1, x, z_2)?".

Both questions can be quickly answered if we store the starting and ending points of all vertical segments in a 2-dimensional Fenwick tree (a la IOI 2001 mobiles).

Time complexity: \mathcal O(N \log N + N \log^2 M) where M is the largest allowed coordinate (999 in this problem).


Comments

There are no comments at the moment.