Editorial for WC '15 Contest 3 J4 - LexCorp Infiltration


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.

If not for type-2 events requiring the grid to be constantly changed, this would be a relative standard flood-fill problem. As a first step, we can locate all of the control rooms, assigning them an index number from 1 to some integer K (note that 1 \le K \le \frac{RC}{2}), determining their sizes, and remembering which room each empty cell belongs to. This can be accomplished in \mathcal O(RC) time using either depth first search (recursion) or breadth first search. We can iterate through the cells of the grid in any order, and when we come across an empty cell which hasn't been assigned to a room yet, we should perform a search to locate its entire room by recursively expanding to adjacent, unvisited empty cells on its top, right, bottom, and left sides (if they exist). An easy trick to remove the need for range checks is to initially pad an entire 0-based grid [0 \dots R+1][0 \dots C+1] with wall tiles before inputting the actual grid into 1-based indices. This effectively creates a "border" around the grid to prevent the search from going out of bounds.

For each round of filling, we can just choose the index as the current number of rooms that have been found so far, starting with 0 for the first room. We will also keep track of the area for each room (incrementing it whenever we reach a cell). At the end of filling each room, we record the index of the room associated with its area.

Next, we should determine the initial ranks of the rooms. To do this efficiently, we must first sort the rooms in non-increasing order of size, which can be done in \mathcal O(K \log K) time. To do this in most languages, we can encapsulate the room data into pairs of (\text{area}, \text{index}) and define a comparator for the sorting function to compare by the sizes rather than the room indices. Finally, their ranks can be computed in \mathcal O(K) time with a single sweep through them, ensuring that each room i gets the same rank as the previous room if they're of equal area, or a rank of i otherwise. When there are only type-1 events, nothing more is required – each event i can be processed by looking up the index of the room that cell (A_i, B_i) is part of, and then looking up that room's rank.

However, type-2 events make things trickier, as they can cause various rooms' ranks to change. That being said, there's no need to re-compute the ranks of all rooms – it can be observed that, when a room is eliminated, the only effect is that the ranks of all rooms with strictly smaller sizes decrease by 1. Since there are \mathcal O(K) such rooms each time, processing all N events has a time complexity of \mathcal O(NK). Since N \le 3000 and K \le \frac{RC}{2} (\frac{RC}{2} is as large as \frac{1000 \times 1000}{2} = 500\,000), that yields roughly 500\,000 \times 3\,000 = 1.5 billion steps. So, even with this observation, looping over all such rooms and updating their ranks is too slow for full marks.

However, this logic can be rearranged as follows: a room's rank is equal to its initial rank, minus the number of strictly larger rooms which have been eliminated so far. Instead of keeping the ranks of every room up to date, we can instead handle each event by looping over all previous type-2 events and counting the number of larger rooms which were targeted in them (taking care to not double-count rooms which were targeted multiple times). This yields a time complexity of \mathcal O(RC+N^2). Plugging in the upper bounds for R, C and N, we are in the ballpark of 1000 \times 1000 + 3000^2, or roughly 10 million steps, which is acceptable.

Finally, it's worth noting that this problem can be solved even more efficiently, in \mathcal O(RC + N \log K) time, by using an advanced data structure such as a binary indexed tree to speed up the step of counting previously-eliminated larger rooms from \mathcal O(N) to \mathcal O(\log K) running time.


Comments

There are no comments at the moment.