There is a square table composed of N*N Cells. Initially all cell is filled with a number 0.
Two types of Operations can be performed on the table.
R i k: All the numbers in the cells on ith row has been changed to k.
C i k: All the numbers in the cells on ith column has been changed to k.
Please Note : k ∈ {0, 1} (ie k = 0 or 1).
At any time ,Alice is interested to know the total number of 0's on some row or total number of 0's on some column.
Our task is to answer Alice Query.
Input
First line contains a single integer N as described above.Next line contains a single integer Q representing the total operations and queries from Alice. Then Followed Q lines , containing operations or queries in following format:
R i k: All the numbers in the cells ith row has been changed to k.
C i k: All the numbers in the cells ith column has been changed to k.
qc i :How many 0's are there in ith row?
qr i:How many cols are there in ith col?
Examples: Input:
3
6
qr 1
C 1 1
qr 1
qc 1
R 1 0
qc 1
Output:
3
2
0
1
I tried a Naive solution.But it got time out. The constraint is quite large and the time limit is just 0.55 Seconds.
**1 ≤ N, Q ≤ 500000 (6 * 10^5)
How can i solve this problem within the given time-limit?
P.S: This is not a homework.