#P1884. Grass Planting Area
Grass Planting Area
Grass Planting Area
Farmer John bought a new machine that can plant grass in any axis‐aligned rectangular region of his farm. Unfortunately, the machine malfunctioned and planted grass in N
rectangular regions, some of which may overlap. Given these rectangles, compute the total area covered by grass, counting overlapping areas only once.
In the Cartesian coordinate system (with the positive X axis pointing right and the positive Y axis pointing up), each rectangle is defined by its top‐left corner (x1, y1) and bottom‐right corner (x2, y2). Formally, if the ith rectangle is specified by (x1, y1) and (x2, y2), its area is given by:
$$\text{Area}_i = \max(0, x_2-x_1) \times \max(0, y_1-y_2) $$Note that if some portions of the rectangles overlap, the overlapping area should only be counted once in the final answer.
inputFormat
The first line contains an integer N
(1 ≤ N ≤ 1000
), the number of rectangles.
Each of the next N
lines contains four integers: x1 y1 x2 y2
, where (x1, y1)
is the top‐left coordinate and (x2, y2)
is the bottom‐right coordinate of a rectangle.
It is guaranteed that x1 < x2
and y1 > y2
.
outputFormat
Output a single integer representing the total area covered by the N
rectangles.
sample
1
0 10 10 0
100