So the first option is to just bash it out. We can just have an area and increment all the points which have one more layer of paint added onto it. For the sample input,
3 2
1 1 5 5
4 4 7 6
3 3 8 7
We start with the following 2D array.
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
The 2nd line of the input turns it into the following.
1 1 1 1 0 0 0 0
1 1 1 1 0 0 0 0
1 1 1 1 0 0 0 0
1 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Continuing on,
1 1 1 1 0 0 0 0
1 1 1 1 0 0 0 0
1 1 1 1 0 0 0 0
1 1 1 2 1 0 0 0
0 0 0 1 1 0 0 0
0 0 0 1 1 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
And finally,
1 1 1 1 0 0 0 0
1 1 1 1 0 0 0 0
1 1 2 2 1 1 0 0
1 1 2 3 2 1 0 0
0 0 1 2 2 1 0 0
0 0 1 2 2 1 0 0
0 0 1 1 1 1 0 0
0 0 0 0 0 0 0 0
And after we make the array, we can iterate the entire thing. However, this is \mathcal{O}(n(\text{Max Coordinate})^2). This is way over the time complexity. However, there is a way to get rid of the \text{(Max Coordinate)}^2 using prefix sums. You are given input in the following form,
x1 y1 x2 y2
Now, what if we add 1 to (x_1,y_1) and and (x_2, y_2) while adding -1 to (x_1,y_2) and (x_2,y_1). Then, using the formula for prefix sums on the array, we will get the array we need. I will show this with the sample input. We start off with
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
After the first set of coordinates, we get
1 0 0 0 -1 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
-1 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Continuing on,
1 0 0 0 -1 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 1 0 -1 0 0
-1 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0
0 0 0 -1 0 1 0 0
0 0 0 0 0 0 0 0
And finally,
1 0 0 0 -1 0 0 0
0 0 0 0 0 0 0 0
0 0 1 0 0 0 -1 0
0 0 0 1 0 -1 0 0
-1 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0
0 0 0 -1 0 1 0 0
0 0 -1 0 0 0 1 0
Applying the formula to build the prefix sum array, we get this array,
1 1 1 1 0 0 0 0
1 1 1 1 0 0 0 0
1 1 2 2 1 1 0 0
1 1 2 3 2 1 0 0
0 0 1 2 2 1 0 0
0 0 1 2 2 1 0 0
0 0 1 1 1 1 0 0
0 0 0 0 0 0 0 0
And now you can iterate through and find which values are equivalent to K.