#C4988. Matrix Row Transformation and Query Processing
Matrix Row Transformation and Query Processing
Matrix Row Transformation and Query Processing
You are given a binary matrix of size \(n \times m\) (each element is either 0 or 1) and a series of queries. Each query is one of two types:
- Switch row i: Flip all the entries in the \(i\)th row (i.e. change each 0 to 1 and each 1 to 0). This operation is defined as \(x' = 1 - x\) for each element \(x\) in the row.
- Count ones in row i: Count the number of 1's in the \(i\)th row of the current matrix.
Note: The row indices in the queries are 1-indexed. Process the queries in the order given and output the result of each "Count ones in row" query on a new line.
inputFormat
The input is given via standard input (stdin) in the following format:
- The first line contains two integers \(n\) and \(m\), representing the number of rows and columns of the matrix.
- The next \(n\) lines each contain \(m\) space-separated integers (each either 0 or 1), representing the rows of the matrix.
- The next line contains an integer \(q\), the number of queries.
- The following \(q\) lines each contain a query in one of the following formats: "Switch row i" or "Count ones in row i".
outputFormat
For each "Count ones in row i" query, output the number of 1's in the \(i\)th row on a separate line to standard output (stdout).
## sample3 3
1 0 1
0 1 0
1 1 1
5
Count ones in row 1
Switch row 2
Count ones in row 2
Switch row 3
Count ones in row 3
2
2
0
</p>