#P9917. Grid Perimeter Queries
Grid Perimeter Queries
Grid Perimeter Queries
Given an \(n \times n\) square grid where each cell is initially red, you are given \(n\) queries that update the grid. Each query colors an entire row or column either red or white. After each query, output the perimeter of all red cells. Two queries affect the grid cumulatively and are not independent.
The perimeter of the red cells is defined as the sum of the lengths of the boundaries of all red cells which either border a white cell or lie on the edge of the grid. In other words, for each red cell, for each of its four sides, if a side is on the border of the grid or adjacent to a white cell, it contributes \(1\) to the total perimeter.
Note: Please pay careful attention to the data constraints.
inputFormat
The input begins with an integer \(n\) \( (1 \leq n \leq \text{some limit})\) representing the size of the grid and the number of queries. Following this, there are \(n\) lines, each containing a query in the format:
X i C
Where X
is a character denoting the type of query ('R' for row and 'C' for column), \(i\) \((1 \leq i \leq n)\) is the 1-indexed row or column number, and C
is a character denoting the color ('R' for red or 'W' for white).
outputFormat
For each query, output a single line containing the perimeter of all red cells in the grid after applying that query.
sample
3
R 2 W
C 3 W
R 1 R
16
12
14
</p>