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.
Let's denote the position of the first ship with
. The first part of the task is to find the set of points
that are visible from point
– the receiver of the Mayday message. The second part of the problem is to find the set of points
that are visible from at least one point from set
– the receivers of the Relay message. In the rest of this text, the expression to the left or right of line
is often used and means that, if the observer is at point
and is looking towards point
, something is to the right or to the left of that line. This expression directly corresponds to the geometric primitive CCW that is most often implemented using the vector product. The polygon edges are given counterclockwise, so we will use the expressions before or after point
, and will be denoted respectively with
and
.
We determine the points that are visible from
using the left and right tangent,
and
. All points to the left of
and to the right of
are directly visible. Additionally, points inside the triangle
are directly visible. The right tangent from point
is determined in a way that we find the edge
for which it holds that point
is to the right of side
and to the left of side
. The left tangent is determined in a similar way.
We find points from set
so that we first find the points that receive the Relay signal from points to the left of
, then in the same way, determine those that receive the Relay signal from points to the right of
. We will only describe the process for the left tangent. Let
denote the set of points to the left of
. In the picture, these points are
,
,
and
, and the red lines are their left tangents, in the rest of the text just tangents.
Let's observe which area is covered by point
from set
, without it being covered by the starting point
. Let
be the tangent that touches the polygon in point
and let
be the intersection of tangents
and
. This area consists of all points to the left of
and to the right of
, and of all points inside the triangle
.
Notice that the point which has the tangent that forms a smaller angle with the line
has an area that is a subset of the area of the point that has the tangent that forms a larger angle. This means that we only need to find the point that has the tangent that forms the largest angle and count the points that are visible from it. The only other problem is how to calculate the tangent to the polygon from every point. If we denote edge
with index
and denote other edges clockwise with ascending indices, then the edge in which the tangent from the required point touches the polygon will have the largest index (among points from set
). This is why we will maintain the largest index thus far and increment it with every new point until it becomes the edge in which the tangent from the current point touches the polygon. When we test whether we need to increment the index from point
, we only need to check if the next edge in the polygon is to the left of the length that connects
to the current edge. If it is, then we increment the index.
Regarding implementation, we need to pay attention to the collinearity of points and make sure that the edge points between areas are not counted multiple times. The total complexity of the algorithm is
.
For the curious reader: Solve the version of the task where in each step we give two points
and
and ask whether
will receive one of the signals if
transmits the Mayday signal. You are given
queries, all other limitations stay the same.
Comments