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

Subtask 1

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: \mathcal{O}(m^4), where m=10 is the maximum possible coordinate.

Query complexity: \mathcal{O}(m^4), where m=10 is the maximum possible coordinate.

Subtask 2

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.

Subtask 3

This is a 1D 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 1 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: \mathcal{O}(N^2 \log m), where m=10^9 is the maximum possible coordinate.

Query complexity: \mathcal{O}(N \log m), where m=10^9 is the maximum possible coordinate.

Subtask 4

We can extend the subtask 3 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 c=1 and d=10^9), 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 3 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: \mathcal{O}(N^2 \log m), where m=10^9 is the maximum possible coordinate.

Query complexity: \mathcal{O}(N \log m), where m=10^9 is the maximum possible coordinate.

Some calculations lead to a maximum of 4N \log m \approx 60\,000 queries, where m=10^9 is the maximum possible coordinate. 1\,000 extra queries are given as headroom just in case.


Comments

There are no comments at the moment.