Letters on the Wall
Time limit2sMemory limit128 MB
Given up to 100 rectangles each filled with one of four parity-based patterns, compute the total number of unit cells that end up marked on the wall.
- Level
Medium7 of 10
- Topics
- Intervals, Math, Matrix, Simulation
- Solved
- No attempts yet
Problem
Minsik wants to put apology letters on a wall for Youngsik. Each letter is a 1 x 1 square. The lower-left corner of the wall is the origin (0, 0), the positive x-axis points right, and the positive y-axis points up.
A unit square whose lower-left corner is (x, y) is treated as one cell. At first, no cell has a letter. For each operation, Minsik chooses the rectangle from (x1, y1) to (x2, y2), meaning all cells with x1 <= x < x2 and y1 <= y < y2, and puts letters on some of those cells. If a cell already has a letter, putting another one there does not increase the count.
Each rectangle uses one of the following four methods.
- Put letters on every cell in the rectangle.
- Put letters on cells in odd-numbered rows of the whole wall. In coordinates, these are exactly the cells with even
y. - Put letters on cells in odd-numbered columns of the whole wall. In coordinates, these are exactly the cells with even
x. - Count rows and columns from the rectangle's lower-left corner, and put letters on cells whose row and column numbers have the same parity. In coordinates, these are exactly the cells where
(x + y)has the same parity as(x1 + y1).
After all rectangles are processed, compute the total number of cells that have a letter.
Input
The first line contains the number of rectangles N. N is a positive integer not greater than 100.
Each of the next N lines contains one rectangle in the form x1 y1 x2 y2 p. The point (x1, y1) is the lower-left corner of the rectangle, and (x2, y2) is the upper-right corner. The value p is the method number described above and is one of 1, 2, 3, or 4.
It is always true that x1 < x2 and y1 < y2. All of x1, y1, x2, and y2 are positive integers not greater than 40,000.
Output
Print the total number of cells on the wall that have a letter.
Hint
In the first public test, the 10 x 10 rectangle affected by methods 1 and 2 fills 100 cells. The other 10 x 10 rectangle affected by methods 3 and 4 fills 75 cells. The two filled regions share 3 cells, so the answer is 100 + 75 - 3 = 172.