10551 - 矩形周长

N 张矩形的海报,照片和其他同样形状的图片贴在墙上。它们的边都是垂直的或水平的。每个矩形可以部分或者全部覆盖其他矩形。所有的矩形组成的集合的轮廓称为周长。写一个程序计算周长。

图 1 是一个有 7 个矩形的例子:

图 1.一个 7 个矩形的集合

对应的轮廓为图 2 所示的所有线段的集合:

图 2. 矩形集合的轮廓

所有矩形的顶点坐标均为整数。所有的坐标都在 [-10000,10000] 的范围内,并且任何一个矩形面积都为正数。结果的值可能需要 32 位有符号整数表示。

输入

Line 1: N,张贴在墙上的矩形的数目。
Lines 2..N+1 接下来的N行中,每行都有两个点的坐标,分别是矩形的左下角坐标和右上角坐标。每一个坐标由 X 坐标和 Y 坐标组成。

输出

只有一行,为一个非负整数,表示输入数据中所有矩形集合的轮廓长度。

样例

输入

7 -15 0 5 10 -5 8 20 25 15 -4 24 14 0 -6 16 4 2 15 10 22 30 10 36 20 34 0 40 16 

输出

228

提示

Picture IOI 1998 A number, N (1 <= N < 5000), of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter. Write a program to calculate the perimeter.

Figure 1 shows an example with seven rectangles:

Figure 1. A set of seven rectangles

The corresponding boundary is the whole set of line segments drawn in Figure 2:

Figure 2. The boundary of the set of rectangles

The vertices of all rectangles have integer coordinates. All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area. The numeric value of the result fits in a 32-bit signed representation.

PROGRAM NAME: picture INPUT FORMAT Line 1: N, the number of rectangles pasted on the wall.
Lines 2..N+1 In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate.

SAMPLE INPUT (file picture.in) 7 -15 0 5 10 -5 8 20 25 15 -4 24 14 0 -6 16 4 2 15 10 22 30 10 36 20 34 0 40 16

OUTPUT FORMAT A single line with a non-negative integer which corresponds to the perimeter for the input rectangles.

SAMPLE OUTPUT (file picture.out) 228

时间限制 1 秒
内存限制 128 MB
讨论 统计
上一题 下一题