Editorial for COCI '19 Contest 2 #5 Zvijezda
Submitting an official solution before solving the problem yourself is a bannable offence.
Let denote the (modulo ) point of our input polygon, and let denote the point from Mirko's query.
If , we will write on the side of our polygon, otherwise we'll write . Now, we need to answer the question: "Is there a pair of opposite sides such that both of them have written on them". Obviously, we cannot check what is written on each of the sides for every query because we would have an algorithm. But, we can get a more efficient algorithm by making some observations.
Observation 1: If two sides have written on them, then all sides between them (in one direction) also have written on them. We can intuitively see this by imagining that is a light source and polygon sides are walls through which the light cannot pass. We can see that illuminated sides have written on them. Since all zeroes form an interval on this cyclic array, we can deduce that all ones form an interval as well.
Observation 2: the answer is DA
if and only if an interval of ones contains at least elements. This is obvious because in successive sides, there must be two of them which are opposite.
Now, let's consider an arbitrary side and its opposite side and observe what values are written on them. If both of them have written on them, we can immediately output DA
. If both of them have written on them, we can immediately output NE
. If one of them has written on it and another one has written on it, we can cut the array into two parts, each of which starts with one of the sides we have checked. One part has the form , and the other one has the form . In one of them, we want to find the last and in the other one, we want to find the first . This can easily be done using binary search. Now we have found the endpoints of an interval of ones and can easily check if the condition from observation 2 holds.
Comments