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. DP动态规划练习

    先来看一下经典的背包问题吧 http://www.cnblogs.com/Kalix/p/7617856.html  01背包问题 https://www.cnblogs.com/Kalix/p/76 ...

  2. spark提交任务的两种的方法

    在学习Spark过程中,资料中介绍的提交Spark Job的方式主要有两种(我所知道的): 第一种: 通过命令行的方式提交Job,使用spark 自带的spark-submit工具提交,官网和大多数参 ...

  3. win10下搭建私链

    首先要下载geth,下载地址:https://gethstore.blob.core.windows.net/builds/geth-windows-amd64-1.7.0-6c6c7b2a.exe ...

  4. Linux内核设计笔记7——中断

    中断与中断处理 何为中断? 一种由设备发向处理器的电信号 中断不与处理器时钟同步,随时可以发生,内核随时可能因为中断到来而被打断. 每一个中断都有唯一一个数字标志,称之为中断线(IRQ) 异常是由软件 ...

  5. oracle常用函数总结

    Oracle常用函数总结 ---oracle常用函数-----一.数值型常用函数----取整数--select floor(10.1) from dual;--将n四舍五入,保留小数点后m位(默认情况 ...

  6. 四:ResourceManger Restart

    概述: RM是yarn中最重要的组件.但是只有一个RM,因此存在单点失败的问题.RM的重启有两种方式: 1.(Non-work-preserving RM restart) 不保留工作状态的重启   ...

  7. [C++] OOP - Virtual Functions and Abstract Base Classes

    Ordinarily, if we do not use a function, we do not need to supply a definition of the function. Howe ...

  8. Thunder团队第三周 - Scrum会议4

    Scrum会议4 小组名称:Thunder 项目名称:i阅app Scrum Master:邹双黛 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传 ...

  9. 用纯css改变下拉列表select框的默认样式(转)

    用纯css改变下拉列表select框的默认样式 分享到 分类 JS学习   关键字 前端   发布 kris  2015-04-01 注意 转载须保留原文链接,译文链接,作者译者等信息.     在这 ...

  10. python实现post请求

    今天无论如何都要留下一些什么东西... 可以说今天学到一个新的一个东西,也需要分享出来,给更多的人去使用. 今天爬取的数据里面是客户端向服务器端发送加密过的token和一些页码之类的一个数据.(我主要 ...