A 2-D matrix ‘MAT’ of size ‘N’ * ‘N’ is present, where each cell consists of a bulb. Initially, all bulbs are turned OFF. You are given a 2-D array ‘BULB’ of size ‘M’, where ‘BULB[i]’ = [‘ROW’, ‘COL’] indicates that the bulb is turned ON at position [‘ROW’, ‘COL’]. When a bulb is turned ON, it illuminates its cell and illuminates the cells present in the same row, column, or diagonal.
Note: There can be repeated values present in the ‘BULB’ array. If the same value is repeated in the ‘BULB’ array, the ‘BULB’ remains on.
For example:Initially, let ‘MAT’ be :
0 0 0
0 0 0
0 0 0
Let the ‘BULB’ array be: [[0, 0]]
‘MAT’ will be changed to :
‘1’ 1 1
1 1 0
1 0 1 where (1 represents the cell is illuminated and ‘1’ represents the cell is illuminated and the bulb is turned ON).
Let ‘QUERY’ array be : [[0, 1]]
As the cell [0, 1] is illuminated, we will print 1 and then turn OFF the bulb at that position and adjacent to it.
‘MAT’ will be changed to :
0 0 0
0 0 0
0 0 0
The first line of input contains an integer ‘T’, denoting the number of test cases.
The first line of each test case contains two space-separated integers, ‘N’, ‘M’, and ‘Q’, representing the size of the matrix, the size of the ‘BULB’ array, and the size of the ‘QUERY’ array.
The next ‘M’ lines of each test case contain two space-separated integers representing the ‘BULB’ array elements.
The next ‘Q’ lines of each test case contain two space-separated integers representing the ‘QUERY’ array elements.
Output Format :
For each test case, print the result of queries in 0 and 1, where 0 represents the cell is not illuminated, and 1 represents the cell is illuminated.
Print output of each test case in a separate line.
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= M, N, Q <= 2*10^4
0 <= BULB[i][0], BULB[i][1], QUERY[i][0], QUERY[i][1] < N
Time Limit: 1 sec
2
3 1 1
0 0
0 1
4 2 2
0 0
3 3
1 1
1 0
1
1 0
For test case 1:
Initially, the ‘MAT’ will be :
0 0 0
0 0 0
0 0 0
After turning ON, bulbs present in the ‘BULB’ array, ‘MAT’ will be changed to :
‘1’ 1 1
1 1 0
1 0 1 where (1 represents the cell is illuminated and ‘1’ represents the cell is illuminated and the bulb is turned ON).
Performing ‘QUERY’ 1:
As the cell [0, 1] is illuminated, we will print 1 and then turn OFF the bulb at that position and adjacent to it.
‘MAT’ will be changed to :
0 0 0
0 0 0
0 0 0
So the result is [1].
For test case 2:
Initially, the ‘MAT’ will be :
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
After turning ON, bulbs present in the ‘BULB’ array, ‘MAT’ will be changed to :
‘1’ 1 1 1
1 1 0 1
1 0 1 1
1 1 1 ‘1’ where (1 represents the cell is illuminated and ‘1’ represents the cell is illuminated as well as the bulb is turned ON).
Performing ‘QUERY’ 1:
As the cell [1, 1] is illuminated, we will print 1 and then turn OFF the bulb at that position and adjacent to it.
‘MAT’ will be changed to :
1 0 0 1
0 1 0 1
0 0 1 1
1 1 1 ‘1’
Performing ‘QUERY’ 2:
As the cell [1, 0] is not illuminated, we will print 0.
So the result is [1, 0].
2
5 2 2
0 0
4 4
1 1
1 1
5 2 3
0 0
0 4
0 4
0 1
1 4
1 1
1 1 0
Try to check in all directions from which a cell can be illuminated for each query.
The basic idea is to simply check every row, column, and diagonal from which a cell can be illuminated for each query. For each cell, there can be 1 row, 1 column, and four diagonals from which it can be illuminated. So we simply check whether a bulb is ON or not in these. We use a set to store the positions of the bulbs that are already ON.
Here is the algorithm :
CHECK(‘N’, ‘S’, ‘R’, ‘C’) (where ‘N’ is the size of the matrix, ‘S’ is the set that consists of positions where the bulb is ON, ‘R’ and ‘C’ is the position of the current query).
O(M + (N*Q*logM)), where ‘M’ is the size of ‘BULB’, ‘N’ is the number of rows or columns, and ‘Q’ is the number of queries.
We traverse all the elements of the ‘BULB’ array to turn ON the bulbs which take O(M) time. We then run a loop to find results for each query that takes O(Q) time, and for each query, we check in the same row and columns and in all diagonals whether a turned ON bulb is present that takes O(N) time. We also search for elements in the set that takes O(logM) time. Therefore, the overall time complexity will be O(M + (N*Q)).
O(M), where ‘M’ is the size of ‘BULB’.
We use a set to store the bulbs’ positions that are turned ON, which can take O(M) space. Therefore, the overall space complexity will be O(M).