Color Inversion on a Huge Chessboard
Time limit4sMemory limit1024 MB
After each row or column flip on an n by n checkerboard, report how many same-colored connected areas there are.
- Level
Medium6 of 10
- Topics
- Math, Implementation
- Solved
- No attempts yet
Problem
A set of square cells is arranged in a chessboard-like pattern with horizontal rows and vertical columns. Rows are numbered through from top to bottom, and columns are numbered through from left to right.
Initially, the cells are colored like a chessboard. The cell in row and column is black if is odd and white if is even.
Color-inversion operations are applied one after another. Each operation is one of the following two.
Invert colors of a row: Given a row number, invert the colors of all cells in that row. White cells in the row become black, and black cells become white.
Invert colors of a column: Given a column number, invert the colors of all cells in that column. White cells in the column become black, and black cells become white.
After each operation, count the number of areas. An area is a group of cells of the same color that are directly or indirectly connected. Two cells are directly connected when they share an edge.
Input
The input consists of a single test case in the following format.
The integer is the number of rows and columns (). The integer is the number of operations (). The following lines give the operations in order. Each line has one of the following forms.
ROW: invert the colors of row ().COLUMN: invert the colors of column ().
Output
Output lines. The -th line contains the number of areas after the -th operation.
Hint

Figure F.1. Illustration of the first sample input.