#K45492. Digital Drawing Application
Digital Drawing Application
Digital Drawing Application
You are given a digital drawing application where you simulate the operations on a 2D canvas. The canvas is represented as a grid of height \(h\) and width \(w\). Initially, all cells in the canvas are off (value 0). There are three types of commands:
draw x1 y1 x2 y2
: Turn on all cells in the rectangular region from the top-left cell \((x_1, y_1)\) to the bottom-right cell \((x_2, y_2)\). Here, the coordinates are 1-indexed.erase x1 y1 x2 y2
: Turn off all cells in the specified rectangular region.count
: Count and output the total number of cells that are turned on in the canvas.
For each count
command, your program should print the resulting count on a new line. This simulation is a direct implementation of the rectangle update and query process. Note that the operations are performed in the order they are read from the input.
For example, if the canvas is of size \(5 \times 5\) and the commands are:
draw 1 1 2 2 count draw 3 3 4 4 count erase 1 1 2 2 count
Then the output should be:
4 8 4
This is because the first draw
command lights up 4 cells, the second command adds 4 more cells resulting in 8 cells turned on (after overlapping does not double count), and after erasing the first drawn block, only 4 cells remain lit.
inputFormat
The input is given from standard input (stdin) with the following format:
h w n command_1 command_2 ... command_n
Here, \(h\) and \(w\) are the height and width of the canvas. \(n\) is the number of commands that follow. Each command is either a drawing command (draw x1 y1 x2 y2
), an erasing command (erase x1 y1 x2 y2
), or the counting command (count
).
outputFormat
For each occurrence of the count
command, output the count of cells that are turned on. Each result must be printed on a new line in the order in which the commands appear.
5 5 6
draw 1 1 2 2
count
draw 3 3 4 4
count
erase 1 1 2 2
count
4
8
4
</p>