This page is still under construction.

Parts of this page are still being built. What you see may change.

Rectangles

Time limit3sMemory limit128 MB

Summary
Given N axis-aligned rectangles, compute the area of their union.
Level

Hard8 of 10

Topics
Segment tree, Sorting, Prefix sum
Solved
No attempts yet

Problem

There are NN axis-aligned rectangles placed on the plane. Write a program that computes the total area of the region covered by these rectangles. A region covered by several rectangles at once is counted only once.

Input

The first line contains a positive integer NN. (1≤N≤200,0001 \le N \le 200{,}000)

Each of the next NN lines contains four integers x1x_1, x2x_2, y1y_1, y2y_2 separated by spaces, describing the rectangle [x1,x2]×[y1,y2][x_1, x_2] \times [y_1, y_2].

All coordinates are integers with 0≤x1<x2≤1090 \le x_1 < x_2 \le 10^9 and 0≤y1<y2≤1090 \le y_1 < y_2 \le 10^9.

Output

Print the total area of the region covered by the NN rectangles.

Examples2

  1. Example 1

    Input
    2
    0 3 1 2
    1 2 0 3
    
    Expected output
    5
    
  2. Example 2

    Input
    1
    0 5 0 4
    
    Expected output
    20