## Editorial for CPC '21 Contest 1 P5 - AQT and Modern Art

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.

Author: Plasmatic

All we have to do for this subtask is to query every possible rectangle and count the number of rectangles within it, taking care to subtract rectangles that were already counted in queries that are within the current query rectangle.

Time complexity: , where is the maximum possible coordinate.

Query complexity: , where is the maximum possible coordinate.

This subtask has no intended solution, but was meant to reward contestants who found a way to query single rectangles within the query limit, but spent too many queries on each rectangle to pass the final subtask.

This is a D version of the full problem.

We can first binary search for the right endpoints of each rectangle, by setting the left endpoint of each query to and varying the right endpoint (y-coordinates do not matter in this case). Then, for each right endpoint, we binary search for the left endpoints of the rectangle with that right endpoint, taking care to not count segments with a smaller right endpoint than the current one being checked. Make sure to account for duplicate endpoints, as the coordinates are not guaranteed to be distinct.

Time complexity: , where is the maximum possible coordinate.

Query complexity: , where is the maximum possible coordinate.

We can extend the subtask solution for full marks.

Start by treating the rectangles as lines just as in the previous subtask (we can do this by sending queries with and ), which will get us the left and right endpoints for every rectangle. Then, we consider each pair of left and right endpoints individually, and find the top and bottom endpoints of each rectangle with the current left and right endpoints using another iteration of the subtask solution, except this time we set the left and right endpoint of each query to be our current horizontal endpoints. Again, we must take care to not count rectangles that have left and right endpoints inside of the current horizontal endpoints being considered. One way to do this is to loop through the pairs of left and right endpoints in order of length, and to add the rectangles we find into an "ignore" list that gets subtracted out of each query.

Alternatively, we can perform the binary search on each boundary independently for each rectangle, which leads to an overall cleaner code, but the complexity is still the same.

Time complexity: , where is the maximum possible coordinate.

Query complexity: , where is the maximum possible coordinate.

Some calculations lead to a maximum of queries, where is the maximum possible coordinate. extra queries are given as headroom just in case.