题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110064#problem/A

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.

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 题意: 给了n个矩形的左下角和右上角的坐标,求矩形面积的并(矩形可能覆盖在一起,求总面积); 思路:使用线段树记录所有矩形上下两条边的高度,用结构体数组记录矩形的左右两条边,并按照从左往右的顺序排序,从左到右加上每一个小矩形的面积。

代码如下:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#define N 210
using namespace std;
double y[N]; struct node
{
double x,y1,y2;
int f;
}Line[N]; struct node1
{
double lf,rf,cnt;
int l,r,c;
}tree[N*]; int cmp1(const node a,const node b)
{
return a.x < b.x;
} int cmp2(const void *a,const void *b)
{
return *(double *)a>*(double *)b?:-;
} void bulid(int t,int l,int r)
{
int mid;
tree[t].c=; tree[t].cnt=;
tree[t].l=l;
tree[t].r=r;///记录着所有矩形的两条竖边从左到右的顺序编号;
tree[t].lf=y[l];
tree[t].rf=y[r];
if(l+==r) return;
mid=(l+r)/;
bulid(*t,l,mid);
bulid(*t+,mid,r);
} void calen(int t)
{
if(tree[t].c>)
{
tree[t].cnt=tree[t].rf-tree[t].lf;
return ;
}
if(tree[t].l+==tree[t].r) tree[t].cnt=;///是矩形右面的边则将树节点的值清零;
else tree[t].cnt=tree[*t].cnt+tree[*t+].cnt;///计算父节点的值;
} void updata(int t,node e)
{
if(e.y1 == tree[t].lf && e.y2==tree[t].rf)
{
tree[t].c+=e.f;
calen(t);
return;
}
if(e.y2 <=tree[*t].rf ) updata(*t,e);
else if(e.y1 >=tree[*t+].lf) updata(*t+,e);
else
{
node tmp=e;
tmp.y2=tree[*t].rf;
updata(*t,tmp);
tmp=e;
tmp.y1=tree[*t+].lf;
updata(*t+,tmp);
}
calen(t);///计算各个父节点的cnt值;
} int main()
{
int Case=,n,t;
double x1,y1,x2,y2,ans;
while(scanf("%d",&n)!=EOF && n)
{
Case++;
t=;
for(int i=;i<=n;i++)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
Line[t].x=x1;
Line[t].y1=y1;
Line[t].y2=y2;
Line[t].f=;
y[t]=y1; Line[t+].x=x2;
Line[t+].y1=y1;
Line[t+].y2=y2;
Line[t+].f=-;
y[t+]=y2;
t+=;
}
sort(Line+,Line+t-,cmp1);///对结构体Line按x值从小到大进行排序;
qsort(y+,t-,sizeof(y[]),cmp2);///对y[]数组进行从小到大的排序;
bulid(,,t-);
ans=;
for(int i=;i<t;i++)
{
ans+=tree[].cnt*(Line[i].x - Line[i-].x);
updata(,Line[i]);
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n",Case,ans);
}
return ;
}

线段树---Atlantis的更多相关文章

  1. hdu 1542 Atlantis(线段树,扫描线)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  2. HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  3. POJ 1542 Atlantis(线段树 面积 并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 参考网址:http://blog.csdn.net/sunmenggmail/article/d ...

  4. 【HDU 1542】Atlantis 矩形面积并(线段树,扫描法)

    [题目] Atlantis Problem Description There are several ancient Greek texts that contain descriptions of ...

  5. 【POJ1151】Atlantis(线段树,扫描线)

    [POJ1151]Atlantis(线段树,扫描线) 题面 Vjudge 题解 学一学扫描线 其实很简单啦 这道题目要求的就是若干矩形的面积和 把扫描线平行于某个轴扫过去(我选的平行\(y\)轴扫) ...

  6. hdu1542 Atlantis 线段树--扫描线求面积并

    There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...

  7. HDU 1542 Atlantis(线段树面积并)

     描述 There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. S ...

  8. hdu1542 Atlantis (线段树+扫描线+离散化)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  9. HDU 1542 - Atlantis - [线段树+扫描线]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

随机推荐

  1. LoadRunner 12 发布,主推云

    LoadRunner 12 发布,主推云 http://blog.csdn.net/testing_is_believing/article/details/22572341

  2. [原创]Android插件化的一种实现

    Android的插件化已经是老生常谈的话题了,插件化的好处有很多:解除代码耦合,插件支持热插拔,静默升级,从根本上解决65K属性和方法的bug等等. 下面给大家介绍一下我们正在用的插件化框架.本片主要 ...

  3. Oracle数据库管理员面试题

    Oracle数据库管理员面试题 1.模拟使用oracle的flashback找回过去某个时间点的数据,实现误操作的恢复. http://www.txw100.com/soft/2013/08/547. ...

  4. saiku 元数据存储分析

    一.介绍 使用saiku的人一定对他的元数据存储都特别感兴趣,特别是有分布式管理需求的项目,更是迫切需要了解.其实它是使用Apache的开源项目Jackrabbit管理文件的! 二.代码跟踪 我也是使 ...

  5. sql:pivot unpivot

    pivot  行转列 unpivot  列转行 源码跑步起来 这是能跑起来的 源码转自 http://www.cnblogs.com/zhangzt/archive/2010/07/29/178782 ...

  6. Windows Store 开发总结——文件操作

    1.读取Isolated Storage 每个Metro程序都有三个文件夹:Local,Roaming,Temp.每个文件夹的访问方法都是相同的. Local用于将数据存储在本地,这是程序特定的文件夹 ...

  7. C primer plus 练习题 第六章

    16. #include <stdio.h> int main() { double remain = 100.0; ; ) { remain = remain * 0.08 + rema ...

  8. 微信、qq时间格式模板

    产品近来蛋疼,时间格式从做完到现在改了四遍了 ,最新的要求如下: * 2分钟内 无显示 * 2分钟-24小时 HH:mm * 昨天 昨天 HH:mm * 前天 前天 HH:mm * 今年 MM:DD ...

  9. springMVC对于controller处理方法返回值的可选类型

    http://www.360doc.com/content/14/0309/19/834950_359081989.shtml

  10. There is an internal error in the React performance measurement code.Did not expect componentDidMount timer to start while render timer is still in progress for another instance

    一.There is an internal error in the React performance measurement code.Did not expect componentDidMo ...