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 simplify the original query in the task with
queries, one for each side of the rectangle. Each of these
queries can be solved in the same way independently. Let us solve the vertical left side
–
for example.
We start by sorting all oaks by their
coordinate first, breaking ties by sorting on
second. It is easy to see that all points on the vertical left side of the rectangle will be in sequence. Since the sequence is sorted, using binary search we can easily determine where points
and
would be in the sequence. The number of items between those two points in the sequence is then the number of oaks that intersect that vertical side. Care should be taken to account for cases where oaks are in points
and
.
The other vertical side is solved by using corresponding points. Horizontal sides are solved by sorting by
first, using
as the tiebreaker.
Time complexity is
.
Comments