POJ 1151: 扫描线


——扫描线
原题传送门

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就可以了!
在此声明.

思路

一道计算矩形面积并的裸题.
何为面积并?
举个例子,这样分布的矩形:
在这里插入图片描述
面积并就是这个部分:
在这里插入图片描述
如果没有扫描线,这个图形的面积还是很难计算的.
那么,什么是扫描线?

扫描线

扫描线,其实就是扫描的线,就像条形码.
形象一点,这个图形被扫描线分割成这些部分:
在这里插入图片描述
这个图形被切割的每一个部分都是一个矩形.
这样子,这个图形的总面积就可以拆分成每个图形的面积.
那具体如何求每一个矩形的面积?
由于读入的矩形比较容易操作,所以我们回到原题解释.

矩形面积并

这道题如果不是矩形面积并,我们如何解决?
当然是直接加了
我们可以考虑这样的思路:

  • 遇到矩形左边界,我们加上它
  • 遇到矩形右边界,我们计算边界长乘距离左边界的长度

其实在本题思路就是如此,只不过需要修改:

  1. 对于每一个矩形 ( 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).

  2. 我们将这些四元组按照 x x x递增排序.

  3. 设立一个层数数组 c [ ] c[ ] c[],表示这个区间被覆盖的次数.

  4. 遍历每一个四元组,对于每一个四元组:
    如果第四个关键字为 1 1 1,就将 c [ y 1 , y 2 ] c[y_1,y_2] c[y1,y2

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值