线段树---Atlantis
题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110064#problem/A
Description
Input
The input file is terminated by a line containing a single 0. Don't process it.
Output
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的更多相关文章
- hdu 1542 Atlantis(线段树,扫描线)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- POJ 1542 Atlantis(线段树 面积 并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 参考网址:http://blog.csdn.net/sunmenggmail/article/d ...
- 【HDU 1542】Atlantis 矩形面积并(线段树,扫描法)
[题目] Atlantis Problem Description There are several ancient Greek texts that contain descriptions of ...
- 【POJ1151】Atlantis(线段树,扫描线)
[POJ1151]Atlantis(线段树,扫描线) 题面 Vjudge 题解 学一学扫描线 其实很简单啦 这道题目要求的就是若干矩形的面积和 把扫描线平行于某个轴扫过去(我选的平行\(y\)轴扫) ...
- hdu1542 Atlantis 线段树--扫描线求面积并
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...
- HDU 1542 Atlantis(线段树面积并)
描述 There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. S ...
- hdu1542 Atlantis (线段树+扫描线+离散化)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 1542 - Atlantis - [线段树+扫描线]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
随机推荐
- chm格式文件能打开,但看不到内容问题
是chm格式的能打开文件,也能看到左边的目录信息,但是无法显示右面的具体内容.报错:无法显示网页.错误页面的url是:res://C:WINDOWSsystem32shdoclc.dll/dnserr ...
- Exists 比Contains 慢非常多。
void Main() { List<string> s = new List<string>(){}; for(int i=0;i<10000;i++) { s.Add ...
- Mac安装win7
为了给老板的mac air装上win7,研究了大致4-5小时,终于搞定! 新版的air安装实在是太不容易了,现在记录如下: 制作硬件:8G优盘一个(至少大于6G空白优盘一个),Macbook ai ...
- alt属性和title属性差异---终于分清楚了!
凡是接触过前端的开发者,相信都会接触到<img>标签,自然alt title更是不会陌生,但对他们真正的含义和使用方法,你确定了解吗? 参考: http://www.junchenwu.c ...
- UNIX环境高级编程笔记之标准I/O库
一.总结 文件I/O一章讲了不带缓冲的I/O,本章讲的是带缓冲的I/O.不带缓冲针对的是内核的系统调用,而带缓冲针对的是用户空间的标准库函数,是基于带缓冲的I/O实现的.不带缓冲的I/O通过文件描述符 ...
- 【学】常用hash算法的介绍
基本知识 Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩映 ...
- 使用 AFNetworking 进行 XML 和 JSON 数据请求
(1)XML 数据请求 使用 AFNetworking 中的 AFHTTPRequestOperation 和 AFXMLParserResponseSerializer,另外结合第三方框架 XMLD ...
- POJ 1804 Brainman
Brainman Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7787 Accepted: 4247 Descript ...
- SQL1159 Initialization error with DB2 .NET Data Provider, reason code 7(问题补充)
SQL1159 Initialization error with DB2 .NET Data Provider, reason code 7 需要注册GAC,修改注册表 IBM官方方案: http: ...
- C#多线程管理代码
/// <summary> /// 多线程执行 /// </summary> public class MultiThreadingWorker { /// <summa ...