WC '15 Contest 3 J4 - LexCorp Infiltration
View as PDFWoburn Challenge 2015-16 Round 3 - Junior Division

While the dust settles in the battle between the dark knight and the last son of Krypton, the two have suddenly come to recognize the true mastermind behind it all. After General Zod's invasion on Earth, LexCorp was contracted by the U.S. government to carefully reverse-engineer leftover Kryptonian technology. As it turns out, the battle between the heroes had simply been Lex Luthor's test in his greater conspiracy to craft a powerful biotechnological weapon against Superman. At that point, it became clear to both that the world is in much graver danger than ever before. Luthor's international masquerade as a humanitarian has attracted the attention of Princess Diana of Themyscira, also known as Wonder Woman. After joining forces with Superman and Batman, the trio obtains intel of Luthor's dangerous bio-weapon project – Doomsday.
But it's too late. To destroy the reputation of Superman, Doomsday has already been deployed by Luthor and is wreaking havoc and causing wanton destruction upon Metropolis. Doomsday was originally a deadly monster born from the depths of ancient Krypton. As Luthor has reanimated the abominable legend from his own laboratories on Earth, Doomsday is actually being puppeted from the very control rooms in LexCorp Tower. To stop this vicious foe, Wonder Woman will have to infiltrate LexCorp Tower and use Batman's knockout gas to shut down these control rooms one at a time.
Using his X-ray vision, Superman has helped devise a map of the control
floor of LexCorp Tower. He knows that the floor can be represented as a
rectangular grid of  rows and 
 columns 
.
The rows are numbered 
 from north to south, while the columns
are numbered 
 from west to east. The 
-th cell in the
-th row can be referred to as cell 
. Each cell 
 is
either part of a control room or a wall, which is indicated by the value
of character 
 on the map (with 
. indicating it is part of a
control room and # indicating it is a wall). At least one cell will be
part of a control room.
The control floor will obviously consist of some number control rooms
(at least one), where each control room on the map consists of a
consecutive region of . characters. More formally, every . cell
is part of exactly one control room, and if a pair of . cells are
adjacent to one another, then they must be part of the same control
room. Two cells  and 
 are adjacent
if and only if 
 (in other
words, if they share a side). The size of a control room is the number
of 
. cells that are part of it.
Releasing knockout gas in a given room will knock out all of its
control persons, permanently preventing the room from conveying a
particular type of vital information to Doomsday's operations. Since
Doomsday is already terrorizing Metropolis at a substantial rate, the
order in which Wonder Woman infiltrates the rooms is vital in minimizing
damage done to the city. Since it's likely that larger control rooms
convey more important information, assigning ranks to rooms based on
their size may help Wonder Woman work out her plan. At any time during
the mission, the rank of control room  is an integer equal to the
number of active control rooms whose sizes are strictly larger than
control room 
's size.
All that said,  
 events will take place in the
mission, one after another. Each event 
 concerns a 
. cell
 
, and can be of
one of 
 types, indicated by the value 
 
:
- Wonder Woman assesses the situation of the control room that cell 
is part of.
 - Wonder Woman travels to cell 
and gasses the entire control room containing that cell.
 
To make her mission smooth, she has asked you to help her write a
program to keep track of her progress. Right before each event  takes
place, your program should determine the current rank of the control
room containing cell 
 and output it. However, if that
cell belongs to a control room that she has already gassed (in one of
the first 
 events), you should instead output 
-1 to remind her.
When a control room is gassed, it ceases to convey signals to Doomsday,
meaning that the ranks of other control rooms may change. On the other
hand, gassing an already gassed room has no effect. Superman's
reputation is dwindling with every moment of Metropolis's destruction,
so you'd better code fast!
In test cases worth  of the points, there will be no type-2 events.
Input Specification
The first line of input will contain three space-separated integers ,
, and 
.
The next  lines will each contain 
 characters
, for 
.
The next  lines will each contain three space-separated integers
, 
, and 
, for 
.
Output Specification
Output  lines with a single integer per line. Line 
 should contain
the rank of the control room of the cell targeted by the 
-th event
(or 
-1 if the room has already been previously gassed), for .
Sample Input
5 6 5
..#.#.
..####
.#..##
#..##.
..###.
1 1 4
1 3 4
2 1 2
2 3 1
1 1 4
Sample Output
3
0
1
-1
2
Explanation
If we label each control room cell with the rank of its control room, the map initially looks as follows:
11#3#3
11####
1#00##
#00##2
00###2
There are  control rooms. The largest one (the one that includes the
south-west cell) has size 
, so its rank is 
. The two size-1 rooms near
the top right of the map both have rank 
.
The 3rd event causes the north-west control room to be gassed, leaving the map looking as follows:
**#2#2
**####
*#00##
#00##1
00###1
Comments