This page is still under construction.

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

Rectangles

Time limit1sMemory limit128 MB

Summary
Compute the total area covered by up to 1000 axis-aligned rectangles, counting overlaps once.
Level

Medium6 of 10

Topics
Sorting, Intervals, Geometry
Solved
No attempts yet

Problem

A rectangle whose sides are parallel to the axes is always determined by the two endpoints of one of its diagonals. For example, a rectangle can be drawn once the top left corner (x1,y1)(x_1, y_1) and the bottom right corner (x2,y2)(x_2, y_2) are given.

Given a set of rectangles, we want the total area they cover. In the picture below, the total area is the region inside the solid lines. An overlapping region is counted once, not twice.

Write a program that reads a set of rectangles and computes the total area covered by all of them.

Input

The input describes a set of NN rectangles, where NN is between 0 and 1000. The first line contains the integer NN. Each of the remaining lines holds the coordinates of one rectangle as four integers x1x_1, y1y_1, x2x_2, y2y_2, which give the two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2). One or more blanks separate the numbers. These two points are not necessarily the top left and bottom right corners. Every xix_i and yiy_i is between 0 and 3276732767.

Output

On the first line print the total area covered by the rectangles. You may assume that the area is at most 3276732767.

Examples1

  1. Example 1

    Input
    4
    20 5 0 15
    37 26 14 9
    20040 2 20050 18
    17 22 33 15
    
    Expected output
    715