Area and Perimeter of a Union of Rectangles
Time limit1sMemory limit128 MB
Given up to 10000 axis-parallel rectangles on an integer grid, compute the area of their union (and its perimeter when r=2), counting overlaps once.
- Level
Hard8 of 10
- Topics
- Segment tree, Sorting, Prefix sum, Geometry
- Solved
- No attempts yet
Problem
Several rectangular sheets are placed on a plane. Write a program that computes the area, or the perimeter, of the region covered by these sheets.
Regarding the plane as a coordinate plane, the sheets are arranged so that the following two conditions hold.
- The and coordinates of the four vertices of every sheet (rectangle) are integers between and inclusive, and each side of every rectangle is parallel to the -axis or the -axis.
- The number of sheets is at most .
Sheets may overlap one another; overlapping area is counted only once. The perimeter is the total length of the boundary of the covered region, including the boundary of any hole that appears inside it.
Input
The first line contains the number of rectangles and an integer indicating the query type, separated by a space. Each of the next lines contains the lower-left vertex and the upper-right vertex of a sheet, given in the order , , , and separated by spaces.
Output
If , print the area of the covered region on the first line. If , print the area on the first line and the perimeter on the second line. In either case, end the output with a newline.