#P1856. Calculate the Perimeter

    ID: 15139 Type: Default 1000ms 256MiB

Calculate the Perimeter

Calculate the Perimeter

Given N axis-aligned rectangles with integer coordinates, compute the total perimeter of the union of these rectangles. The union is defined as the set of all points that lie in at least one rectangle.

For example, consider the 7 rectangles shown in Figure 1. Figure 2 illustrates the boundary of the union of all rectangles. All vertex coordinates are integers.

The perimeter is computed as the total length of the outer boundary of the union, i.e. all segments that separate the covered region from the uncovered region. Note that overlapping edges are counted only once.

In mathematical terms, if R is the union of all rectangles, the required perimeter is ( P = \text{length}(\partial R) ).

inputFormat

The first line contains an integer N, denoting the number of rectangles. Each of the next N lines contains four integers (x_1), (y_1), (x_2), (y_2) (with (x_1 < x_2) and (y_1 < y_2)), representing the bottom-left and top-right coordinates of a rectangle.

outputFormat

Output a single integer representing the total perimeter of the union of the given rectangles.

sample

1
0 0 1 1
4