Overlapping Rectangles

来源:互联网 发布:优化软件点击 编辑:程序博客网 时间:2024/06/04 19:05

There are nnn rectangles on the plane. The problem is to find the area of the union of these rectangles. Note that these rectangles might overlap with each other, and the overlapped areas of these rectangles shall not be counted more than once. For example, given a rectangleAAA with the bottom left corner located at (0,0)(0, 0)(0,0) and the top right corner at (2,2)(2, 2)(2,2), and the other rectangle BBB with the bottom left corner located at (1,1)(1,1)(1,1) and the top right corner at (3,3)(3,3)(3,3), it follows that the area of the union of AAA and BBB should be 777, instead of 888.

Although the problem looks simple at the first glance, it might take a while to figure out how to do it correctly. Note that the shape of the union can be very complicated, and the intersected areas can be overlapped by more than two rectangles.

Note:

(1) The coordinates of these rectangles are given in integers. So you do not have to worry about the floating point round-off errors. However, these integers can be as large as1,000,0001,000,0001,000,000.

(2) To make the problem easier, you do not have to worry about the sum of the areas exceeding the long integer precision. That is, you can assume that the total area does not result in integer overflow.

Input Format

Several sets of rectangles configurations. The inputs are a list of integers. Within each set, the first integer (in a single line) represents the number of rectangles, n, which can be as large as100010001000. After n, there will be n lines representing the n rectangles; each line contains four integers<a,b,c,d><a, b, c, d><a,b,c,d> , which means that the bottom left corner of the rectangle is located at (a,b)(a, b)(a,b), and the top right corner of the rectangle is located at (c,d)(c, d)(c,d). Note that integers aaa,bbb,ccc,ddd can be as large as 1,000,0001,000,0001,000,000.

These configurations of rectangles occur repetitively in the input as the pattern described above. An integern=0n = 0n=0 (zero) signifies the end of input.

Output Format

For each set of the rectangles configurations appeared in the input, calculate the total area of the union of the rectangles. Again, these rectangles might overlap each other, and the intersecting areas of these rectangles can only be counted once. Output a single star '*' to signify the end of outputs.

样例输入

20 0 2 21 1 3 330 0 1 12 2 3 34 4 5 50

样例输出

73*

待解决

阅读全文
0 0
原创粉丝点击