poj1151==codevs 3044 矩形面积求并
Atlantis
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 21511 | Accepted: 8110 |
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.
Input
The input consists of several test cases. Each test case starts with a line containing a single integer n (1 <= n <= 100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0 <= x1 < x2 <= 100000;0 <= y1 < y2 <= 100000), 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.
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.
Output a blank line after each test case.
Sample Input
Sample Output
Source
题解:
题目的意思是给定n个矩形的2n个坐标,求矩形的覆盖面积。如果开一个大的bool数组,将覆盖过的部分更新为true,再从头到尾扫描一遍,在坐标范围比较小的情况下,可以求解。但是如果坐标x,y的取值范围很大,比如[-10^8,10^8],用上面这个方法就不能求解了;而且坐标还有可能是实数,上面的方法就更加不可行了,需要寻找一种新的解法,就是下面要说到的“离散化”。
注意到要表示一个矩形,只需要知道其2个顶点的坐标就可以了(最左下,最右上)。可以用2个数组x[0...2n-1],y[0...2n-1]记录下矩形Ri的2个坐标(x1,y1),(x2,y2),然后将数组x[0...xn-1],y[0...2n-1]排序,为下一步的扫描线作准备,这就是离散化的思想。这题还可以用线段树做进一步优化,但是这里只介绍离散化的思想。
看下面这个例子:有2个矩形(1,1),(3,3)和(2,2),(4,4)。如图:
图中虚线表示扫描线,下一步工作只需要将这2个矩形覆盖过的部分的bool数组的对应位置更新为true,接下去用扫描线从左到右,从上到下扫描一遍,就可以求出矩形覆盖的总面积。
这个图对应的bool数组的值如下:
1 1 0 1 2 3
1 1 1 <----> 4 5 6
0 1 1 7 8 9
注意到要表示一个矩形,只需要知道其2个顶点的坐标就可以了(最左下,最右上)。可以用2个数组x[0...2n-1],y[0...2n-1]记录下矩形Ri的2个坐标(x1,y1),(x2,y2),然后将数组x[0...xn-1],y[0...2n-1]排序,为下一步的扫描线作准备,这就是离散化的思想。这题还可以用线段树做进一步优化,但是这里只介绍离散化的思想。
看下面这个例子:有2个矩形(1,1),(3,3)和(2,2),(4,4)。如图:
图中虚线表示扫描线,下一步工作只需要将这2个矩形覆盖过的部分的bool数组的对应位置更新为true,接下去用扫描线从左到右,从上到下扫描一遍,就可以求出矩形覆盖的总面积。
这个图对应的bool数组的值如下:
1 1 0 1 2 3
1 1 1 <----> 4 5 6
0 1 1 7 8 9
AC代码:
注意::::POJ1151上G++要用%.2f才能过!%.2lf WA!
__EOF__

本文作者:shenben
本文链接:https://www.cnblogs.com/shenben/p/5837614.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
本文链接:https://www.cnblogs.com/shenben/p/5837614.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术