You are given two integers, βNβ and βK,β and initially, you have an 'N' * 'N' grid. You are asked βKβ queries wherein each query you are given a coordinate, and you have to colour it and print the number of uncoloured cells in the grid.
The first line of the input contains βT,β denoting the number of test cases.
The first line of each test case contains two space-separated integers, βNβ and βK,β denoting dimensions of the grid and number of queries.
The following βKβ lines contain two space-separated integers, 'X[i]' and 'Y[i]', denoting the coordinates of the grid.
Output Format:
For each test case, return the answer of each query.
Print the answer in a separate line for each query.
1 <= T <= 3
1 <= N <= 5000
1 <= K <= min(3000,N * N)
1 <= X[i], Y[i] <= N
Time limit: 1 sec
1
4 4
1 2
3 4
2 3
1 2
15
14
13
13

2
9 4
1 2
4 1
3 3
2 4
6 5
2 2
2 3
2 3
2 1
3 3
80
79
78
77
35
34
34
33
32
In test case 1,
After Query 1, the cell is newly covered and hence 1 less cell will be the output from 81. Thus 80 is the answer.
Similarly Queries 2, 3, 4 are unique, the cell is newly covered and thus answers will be 79, 78, 77 respectively.
In test case 2,
After Query 1, the cell is newly covered and hence 1 less cell will be the output from 36. Thus 35 is the answer.
After Query 2, the cell is newly covered and hence 1 less cell will be the output from 35. Thus 34 is the answer.
After Query 3, the cell query is repeating. Thus 34 is the answer.
After Query 4, the cell is newly covered and hence 1 less cell will be the output from 34. Thus 33 is the answer.
After Query 5, the cell is newly covered and hence 1 less cell will be the output from 33. Thus 32 is the answer.
Find the number of coloured cells after each query.
Explanation:
The steps are as follows:
Initialize a set βSTβ which will be used to store coordinates of the colored cells.
In each query, we have coordinates ('X[i]', βY[i]β) and we do the following each time:
A cell number of a coordinate is the position this coordinate would be if we were to flatten the matrix i.e. if there was only 1 row and βNβ * βNβ column. A cell number of coordinate ('i', βjβ) is = βNβ * βiβ + βjβ .
O(K * log(K)), Where βKβ is the number of queries.
Since there are K queries and in each query we are doing O(log(K)) operations insertion or updation in the set. Thus the time complexity will be O(K * log(K)).
O(K), Where βKβ is the number of queries.
Since we will store colored cells coordinated in the map, which will take O(K) space. Thus the space complexity will be O(K).