Editorial for DMPG '19 G4 - Carpet Cleaning Chore


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: r3mark

For the first subtask, consider the cell furthest from (1, 1) whose subrectangle to (1, 1) will need to be toggled. Clearly, this cell must be dirty since otherwise, that subrectangle would need to be toggled again and that would only add two needless moves since they end up cancelling out. So Bob should always toggle the subrectangle whose corner is the furthest dirty cell. This can be implemented by looping from right to left and bottom to top. A 2-D prefix sum modulo 2 should be used to simulate the toggling.

Time Complexity: \mathcal{O}(QMN)

For the second subtask, the same observation is made. Note that only the ends of contiguous dirty cells will need to be toggled. In particular, a cell will only be toggled if it and the cell right of it is CD or DC. Then an update only affects at most two cells. So after preprocessing and storing whether each cell should be toggled, each update can be answered in \mathcal{O}(1).

Time Complexity: \mathcal{O}(Q+MN)

For the final subtask, this observation can be generalized. For a given cell, it should be toggled if and only if the 2 by 2 with that cell at the top-left corner has an odd number of dirty cells. This is most easily motivated by interpreting the question as a system of linear equations modulo 2. For a single dirty cell, it can be cleaned by toggling the 2 by 2 with that cell at the bottom-right. The entire carpet can then be cleaned by doing this for each dirty cell, except that there is no need to toggle a cell more than one time; only the parity is important.

So as in subtask 2, after preprocessing and storing whether each cell should be toggled, each update can be answered in \mathcal{O}(1).

Time Complexity: \mathcal{O}(Q+MN)


Comments

There are no comments at the moment.