Colored Paper
InterviewTime limit1sMemory limit64 MB
Compute the area of each of N overlapping axis-aligned rectangles that stays visible after later rectangles cover it.
- Level
Easy3 of 10
- Topics
- Simulation, Matrix, Implementation
- Solved
- No attempts yet
Problem
Rectangular sheets of colored paper, each a different color, are placed on a plane one at a time until sheets are down. No sheet is placed at an angle, so any two sides of any two sheets are either parallel or perpendicular. Figure 1 shows sheets 1, 2, and 3 placed in that order.

Figure 1
Placing a fourth sheet as in figure 2 hides sheet 3 completely. Sheets 1 and 2 are partly hidden, and sheet 4 is fully visible.

Figure 2
Write a program that computes the visible area of each sheet after the sheets are placed at the given positions, in the given order.
Input
The first line has the number of sheets (). Each of the next lines describes one sheet, in the order the sheets are placed.
The plane is a grid at most 1001 cells wide and at most 1001 cells tall. Each cell is a square of width 1, height 1, and area 1.
Take a grid 6 cells wide and 6 cells tall as an example. The pair written inside a cell is the number of that cell. The bottom left cell is and the top right cell is .

A sheet is given by four integers , , , : the number of its bottom left cell, its width, and its height. The gray sheet in the picture above has its bottom left cell at , width 3, and height 2, so it is written as 1 4 3 2. The width and the height are integers of at least 1, and no sheet reaches outside the grid.
Output
Print the visible area of each sheet as one integer on its own line, in the order the sheets are given in the input. If a sheet is not visible at all, print 0.