Problems > Area of Overlapping Rectangles

Area of Overlapping Rectangles

Difficulty: 2300 6000 ms 128 MB

geometry sweep-line

Print the total area covered by the union of N axis-aligned rectangles (overlaps counted once).

Input: the first line has N; each of the next N lines has x1 y1 x2 y2 (lower-left and upper-right corners).

Output: the area of the union.

Constraints:

  • 1 ≤ N ≤ 103
  • |coordinates| ≤ 104

Sample Tests

Input 1
2
0 0 2 2
1 1 3 3
Output 1
7
Input 2
1
0 0 5 5
Output 2
25

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.