Editorial for COCI '13 Contest 6 #4 Kružnice


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.

Since the circles do not intersect, we can look at them as intervals. More specifically, we observe their intersection with the x-axis.

Let us construct a relation contains such that A contains B if interval B is inside of interval A, with allowed touching on the edges.

Let us construct a relation directly_contains such that A directly_contains B if A contains B and there is no other circle C for which holds that A contains C and C contains B.

Intuitively, A directly_contains B if B is one of the first smaller circles in A.

Because there is at most one circle that directly_contains an arbitrary circle, this relation is a tree.

Every circle will increase the number of regions by 1 or 2. In the case when a circle directly contains more other circles which touch along all its length, that circle is going to increase the solution by 2. In the other case, by 1.

The tree of circles can be built using the sweep algorithm where an event is at the beginning or end of a circle. The events are processed by their x coordinates. As the structure of the sweep algorithm, we will use a stack which keeps track of the current parent circle and whether we have lined up every directly contained circles next to each other. In the moment of popping a circle from the stack, the solution is increased by 2 if all the directly contained circles are lined up next to each other. In the other case, the solution is increased by 1. The final solution needs to be incremented by 1 because it represents the whole region above all the circles.

The complexity of the algorithm is \mathcal O(N \log N) where N is the number of circles.


Comments

There are no comments at the moment.