All problems

Problem 689

Rectangle Area II

Hard · 30 pts · Geometry, Sweep Line, Coordinate Compression

Problem

You are given N axis-aligned rectangles, each by its bottom-left corner (x1, y1) and top-right corner (x2, y2). Find the total area covered by at least one rectangle, counting overlapping parts only once, and print it modulo 1000000007. Compress the x and y coordinates: the distinct values cut the plane into at most (2N)² cells, and each cell is either fully covered or not. Sum the areas of the covered cells (use 64-bit or big integers before taking the remainder).

Input

The first line contains N. Each of the next N lines contains x1, y1, x2 and y2.

Output

Print the covered area modulo 1000000007.

Constraints

1 <= N <= 200; 0 <= x1 < x2 <= 10^9; 0 <= y1 < y2 <= 10^9

Example input

3
0 0 2 2
1 0 2 3
1 0 3 1

Example output

6

Explanation

The rectangles (0,0)-(2,2), (1,0)-(2,3) and (1,0)-(3,1) cover 4, 3 and 2 units, but they overlap. Counting every covered unit square once gives a total area of 6.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: The Skyline Problem Next: Range Frequency Queries