——扫描线
原题传送门
Description
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis.
Some of these texts even include maps of parts of the island.
But unfortunately, these maps describe different regions of Atlantis.
Your friend Bill has to know the total area for which maps exist.
You (unwisely) volunteered to write a program that calculates this quantity.
Data
Input
The input consists of several test cases.
Each test case starts with a line containing a single integer n of available maps.
The n following lines describe one map each.
Each of these lines contains four numbers x1;y1;x2;y2, not necessarily integers.
The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.
The input file is terminated by a line containing a single 0. Don’t process it.
Output
For each test case, your program should output one section.
The first line of each section must be “Test case #k”, where k is the number of the test case (starting with 1).
The second one must be “Total explored area: a”, where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.
Output a blank line after each test case.
Sample Input
2
10 10 20 20
15 15 25 25.5
0
Sample Output
Test case #1
Total explored area: 180.00
//This is a blank line
1 <= n <= 100 0 <= x1 < x2 <= 100000 0 <= y1 < y2 <= 100000
前言
POJ是一个神奇的OJ.
按道理说, d o u b l e double double类型的输出应该是 % l f \%lf %lf.
但是这样提交会WA!
后来参考奆老博客发现这个输出竟然改成 % f \%f %f就可以了!
在此声明.
思路
一道计算矩形面积并的裸题.
何为面积并?
举个例子,这样分布的矩形:
面积并就是这个部分:
如果没有扫描线,这个图形的面积还是很难计算的.
那么,什么是扫描线?
扫描线
扫描线,其实就是扫描的线,就像条形码.
形象一点,这个图形被扫描线分割成这些部分:
这个图形被切割的每一个部分都是一个矩形.
这样子,这个图形的总面积就可以拆分成每个图形的面积.
那具体如何求每一个矩形的面积?
由于读入的矩形比较容易操作,所以我们回到原题解释.
矩形面积并
这道题如果不是矩形面积并,我们如何解决?
当然是直接加了
我们可以考虑这样的思路:
- 遇到矩形左边界,我们加上它
- 遇到矩形右边界,我们计算边界长乘距离左边界的长度
其实在本题思路就是如此,只不过需要修改:
-
对于每一个矩形 ( x 1 , y 1 , x 2 , y 2 ) (x_1,y_1,x_2,y_2) (x1,y1,x2,y2)(与题意相符),我们设立两个四元组: ( x 1 , y 1 , y 2 , 1 ) (x_1,y_1,y_2,1) (x1,y1,y2,1)和 ( x 2 , y 1 , y 2 , − 1 ) (x_2,y_1,y_2,-1) (x2,y1,y2,−1).
-
我们将这些四元组按照 x x x递增排序.
-
设立一个层数数组 c [ ] c[ ] c[],表示这个区间被覆盖的次数.
-
遍历每一个四元组,对于每一个四元组:
如果第四个关键字为 1 1 1,就将 c [ y 1 , y 2 ] c[y_1,y_2] c[y1,y2