Experiment in Berland

4.8

6 votes
Data Structures, Depth First Search, Algorithms, Disjoint Set Union, Graphs
Problem

Alice, a well-known scientist in Berland, has a floor of size NM in his research laboratory, with each tile measuring 1X1. If a drop of chemical falls on a tile, it will combine to form a larger drop if there is a drop of chemical solution present in the adjacent tile that shares the common sides or edges.
Note: If a chemical solution falls on the same tile, the size does not change and all indexing are 1 based


You will be given Q queries, each of which can be of two types .
Type 1: Represents coordinates of point on which chemical drop falls
Type 0: Print total number of large drops formed till now.

query of type 1 representing the coordinates of a cell on which chemical drop will fall and for each query of type 0find the number of connected components in the grid.

Input Format

First line contain 3 integers N,M and Q representing the number of rows, columns and the number of queries.

Next Q lines contain 1 integer which represents the type of query that has been asked.


For query of type 1, there will be 2 more integer x and y which represent the coordinate of cells on which chemical solution is falling on.

for query of type 0you have to output the number of large drops till now

Output Format

For each query of type 0 output the total number of large drops till now

Constraints

1N,M103

1xN

1yM

1Q101000

Sample Input
3 3 5
1 2 1
1 2 2
0
1 3 2
0
Sample Output
1
1
Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

We first marked the cell (2,1) and then cell (2,2) as cell (2,1) and (2,2) share common edges both will connect together to form a single connected component then the cell (3,2) is marked which shares a common edge with cell (2,2) as cell (2,2) & cell (3,2) are connected together they will form a single connected component

 

Editor Image

?