Numbers in a matrix

4.5

14 votes
Advanced Data Structures, Data Structures, Red-black Trees, Easy
Problem

You are given a matrix of size n×m. Initally, the matrix contains 0s. There are q queries and you must process them sequentially by performing the following operations:

  1. toggle(x,y): If the value of (x,y) is 0, then change it to 1 and vice versa.
  2. For each pair (x,y), perform the first query.

Your task is to count the remaining number of 1s that are available after the q queries.

Input format

  • First line: Three integers n, m, and q (1n,m109,1q105)
  • Each q line contains the following:
    • 1 x y that denotes the first operation (1xin,1yim)
    • 2 denotes the second operation
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In the sample:
after the first query, the number of '1' is 1
after the second query, the number is 5
after the third query, the number is 4.
So we print 4.

Editor Image

?