Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12266    Accepted Submission(s): 5151

Problem 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 file 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
 
Source
 

题意:

x轴向右,y轴向下的坐标平面内,求矩形并的面积。坐标值非整数(0~100000).

代码:

//坐标值是double,把用到的每个坐标离散化。然后再扫描线。
//注意:int rig=Bsearch(nodes[i].r,m,mp)-1;和sum[rt]=mp[r+1]-mp[l];
//离散化之后a~a+1是有长度的。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
int cnt[maxn*];
double sum[maxn*],mp[maxn*];
struct node
{
double l,r,h;
int d;
node(){}
node(double a,double b,double c,int d):l(a),r(b),h(c),d(d){}
bool operator < (node &p){
if(h==p.h) return d>p.d;
return h<p.h;
}
}nodes[maxn*];
int Bsearch(double a,int b,double *c)
{
int l=,r=b-,mid;
while(l<=r){
mid=(l+r)>>;
if(c[mid]==a) return mid;
else if(c[mid]>a) r=mid-;
else l=mid+;
}
return -;
}
void Pushup(int l,int r,int rt)
{
if(cnt[rt])
sum[rt]=mp[r+]-mp[l];
else if(l==r) sum[rt]=;
else sum[rt]=sum[rt<<]+sum[rt<<|];
}
void Update(int ql,int qr,int c,int l,int r,int rt)
{
if(ql<=l&&qr>=r){
cnt[rt]+=c;
Pushup(l,r,rt);
return;
}
int m=(l+r)>>;
if(ql<=m) Update(ql,qr,c,l,m,rt<<);
if(qr>m) Update(ql,qr,c,m+,r,rt<<|);
Pushup(l,r,rt);
}
int main()
{
int n,cas=;
while(scanf("%d",&n)&&n){
double x1,x2,y1,y2;
int m=,nu=;
for(int i=;i<n;i++){
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
nodes[nu++]=node(x1,x2,y1,);
nodes[nu++]=node(x1,x2,y2,-);
mp[m++]=x1;mp[m++]=x2;
}
sort(mp,mp+m);
m=unique(mp,mp+m)-mp;
sort(nodes,nodes+nu);
memset(sum,,sizeof(sum));
memset(cnt,,sizeof(cnt));
double ans=;
for(int i=;i<nu-;i++){
int lef=Bsearch(nodes[i].l,m,mp);
int rig=Bsearch(nodes[i].r,m,mp)-;
if(lef<=rig)
Update(lef,rig,nodes[i].d,,m-,);
ans+=sum[]*(nodes[i+].h-nodes[i].h);
}
printf("Test case #%d\n",++cas);
printf("Total explored area: %.2lf\n\n",ans);
}
return ;
}

HDU1542 扫描线+离散化的更多相关文章

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

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

  2. HDU - 1255 覆盖的面积(线段树求矩形面积交 扫描线+离散化)

    链接:线段树求矩形面积并 扫描线+离散化 1.给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. 2.看完线段树求矩形面积并 的方法后,再看这题,求的是矩形面积交,类同. 求面积时,用被覆 ...

  3. HDU1542 Atlantis —— 求矩形面积并 线段树 + 扫描线 + 离散化

    题目链接:https://vjudge.net/problem/HDU-1542 There are several ancient Greek texts that contain descript ...

  4. hdu1542 线段树+扫描线+离散化

    仅仅想说题目给的欲实际不服     还是这类型的水题吧   建议看之前我写的那个 #include<stdio.h> #include<string.h> #include&l ...

  5. POJ-1151-Atlantis(线段树+扫描线+离散化)[矩形面积并]

    题意:求矩形面积并 分析:使用线段树+扫描线...因为坐标是浮点数的,因此还需要离散化! 把矩形分成两条边,上边和下边,对横轴建树,然后从下到上扫描上去,用col表示该区间有多少个下边,sum代表该区 ...

  6. poj1151 Atlantis (线段树+扫描线+离散化)

    有点难,扫描线易懂,离散化然后线段树处理有点不太好理解. 因为这里是一个区间,所有在线段树中更新时,必须是一个长度大于1的区间才是有效的,比如[l,l]这是一根线段,而不是区间了. AC代码 #inc ...

  7. HDU - 1255 扫描线+离散化进阶

    这道题最开始我以为和HDU - 1542 那道题一样,只需要把cover次数改成2次即可,但是后面仔细一想,我们需要求的是覆盖次数大于等于2次的,这样的话,我们需要维护两个长度,HDU-1542 由于 ...

  8. HDU1542 扫描线(矩形面积并)

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

  9. Helter Skelter (扫描线 + 离散化 + 树状数组)

    扫描线:按照其中一个区间的标记为pos,然后左区间标记d为正影响,有区间标记d为负影响,然后根据所有的pos排序.pos从小扫到大,那么对于某一个区间一定会被扫过2次,那么经过2次之后就只剩下中间那一 ...

随机推荐

  1. spring 整合hibernate注解时候,出现“Unknown entity: com.ssh.entry.Admin; nested exception is org.hibernate.MappingException: Unknown entity: com.ssh.entry.Admin”异常的问题

    今天学习使用ssh框架的时候,出现一个异常,弄了好久才找到,在这记录一下,我的sb错误1.spring整合hibernate,取代*.hbm.xml配置文件   在applicationContext ...

  2. 20届的阿里 头条 网易 滴滴 百度 小米等Java面经

    转载连接 个人博客:junxuelian.cn 总结:个人感觉回答面试官问题不必太官方和书面化,腾讯sng招实习被发现照着百度百科念.结果可想而知.用自己的话和理解去回答就好.可能应届生会抱怨设计题, ...

  3. 自测之Lesson4:gdb

    题目:列出gdb过程中常用的命令. 常用命令: 命令 作用 使用示例1 使用示例2 list 列出代码 list 行号 list 函数名 break 设置断点 break 行号 b 行号 run 运行 ...

  4. 软工2017第五周——个人PSP

    10.13 --10.19本周例行报告 1.PSP(personal software process )个人软件过程. 类型 任务 预计时间 开始时间                结束时间 中断时 ...

  5. lintcode-32-最小子串覆盖

    最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 注意事项 如果在source中没有这样的子串,返回"" ...

  6. 【Linux】- apt-get命令

    apt-get,是一条linux命令,适用于deb包管理式的操作系统,主要用于自动从互联网的软件仓库中搜索.安装.升级.卸载软件或操作系统. Advanced Package Tool,又名apt-g ...

  7. FineCMS介绍

      产品简介 FineCMS(简称免费版.企业版.公益版)是一款基于PHP+MySql+CI框架开发的高效简洁的中小型内容管理系统,面向多终端包括Pc端网页和移动端网页,支持自定义内容模型和会员模型, ...

  8. 含html转义字符编码(&#22235;)转换--python

    在抓取下来的网页源码显示的是如下的内容,而不是可读性的汉字 (当然,如果是在Web页面上展示,则实体会自动被浏览器转为原字符,正常显示) 经查资料后得知, 在网页中以四开头的是HTML实体,具体什么是 ...

  9. Mysql查询优化从入门到跑路(三)查询的基本操作

    查询的基本操作 1.选择操作 对应的是限制条件,操作对象是二维表的行.     优化方式:选择操作下推     目的:尽量减少连接操作前的元租数,使得中间临时关系尽量少(元祖数少,连接得到的元组数就少 ...

  10. 学习 SQL 语句 - Select(9): 其他

    //只要前五条记录 procedure TForm1.Button1Click(Sender: TObject); begin   with ADODataSet1 do begin     Clos ...