(中等) HDU 1542 Atlantis,扫描线。
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.
题目就是求矩形并的面积,具体请看 线段树 (扫描线) 这篇文章。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath> #define lson L,M,lc
#define rson M+1,R,rc
#define lc po*2
#define rc po*2+1 using namespace std; struct BIAN
{
double x,y1,y2;
int state;
}; BIAN bian[];
double hash[];
double BIT[*];
int COL[*];
int COU; bool cmp(BIAN a,BIAN b)
{
return a.x<b.x;
} int find(double x)
{
int L=,R=COU;
int M; while(R>L)
{
M=(L+R)/; if(fabs(hash[M]-x)<0.000001)
return M; if(hash[M]<x)
L=M+;
else
R=M-;
} return L;
} void pushUP(int L,int R,int po)
{
if(COL[po])
BIT[po]=hash[R+]-hash[L];
else if(L==R)
BIT[po]=;
else
BIT[po]=BIT[lc]+BIT[rc];
} void update(int ul,int ur,int ut,int L,int R,int po)
{
if(ul<=L&&ur>=R)
{
COL[po]+=ut; if(COL[po]>)
BIT[po]=hash[R+]-hash[L];
else if(L==R)
BIT[po]=;
else
pushUP(L,R,po); return;
} int M=(L+R)/; if(ul<=M)
update(ul,ur,ut,lson);
if(ur>M)
update(ul,ur,ut,rson); pushUP(L,R,po);
} int main()
{
int cas=;
int N;
double x1,x2,y1,y2;
double ans; for(cin>>N;N;cin>>N)
{
ans=;
COU=;
memset(hash,,sizeof(hash));
memset(COL,,sizeof(COL));
memset(BIT,,sizeof(BIT)); for(int i=;i<=N;++i)
{
scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2); bian[i*-].x=x1;
bian[i*-].y1=y1;
bian[i*-].y2=y2;
bian[i*-].state=; bian[i*].x=x2;
bian[i*].y1=y1;
bian[i*].y2=y2;
bian[i*].state=-; hash[COU++]=y1;
hash[COU++]=y2;
} sort(hash+,hash+COU);
sort(bian+,bian+*N+,cmp); int k=;
for(int i=;i<COU;++i)
if(hash[i]!=hash[i-])
hash[k++]=hash[i];
COU=k-; for(int i=;i<=*N;++i)
{
ans+=BIT[]*(bian[i].x-bian[i-].x); update(find(bian[i].y1),find(bian[i].y2)-,bian[i].state,,COU-,);
} ans+=BIT[];
printf("Test case #%d\nTotal explored area: %.2f\n\n",++cas,ans);
} return ;
}
代码改了一下的:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm> #define lc po*2
#define rc po*2+1
#define lson L,M,lc
#define rson M+1,R,rc
#define ji i*2-1
#define ou i*2 using namespace std; struct BIAN
{
double x,y1,y2;
short state;
}; const int maxn=; BIAN bian[maxn];
double BIT[maxn*];
int COL[maxn*];
double hash[maxn];
int COU; int find(double x)
{
int L=,R=COU,M; while(R>L)
{
M=(L+R)/; if(fabs(x-hash[M])<0.0000001)
return M; if(hash[M]<x)
L=M+;
else
R=M-;
} return L;
} void callUP(int L,int R,int po)
{
if(COL[po])
BIT[po]=hash[R+]-hash[L];
else if(L==R)
BIT[po]=;
else
BIT[po]=BIT[lc]+BIT[rc];
} void pushUP(int L,int R,int po)
{
int temp=min(COL[lc],COL[rc]); COL[po]+=temp;
COL[lc]-=temp;
COL[rc]-=temp; callUP(L,(L+R)/,lc);
callUP((L+R)/+,R,rc); callUP(L,R,po);
} void pushDown(int L,int R,int po)
{
if(COL[po])
{
COL[lc]+=COL[po];
COL[rc]+=COL[po]; callUP(L,(L+R)/,lc);
callUP((L+R)/+,R,rc); callUP(L,R,po); COL[po]=;
}
} void update(int ul,int ur,int ut,int L,int R,int po)
{
if(ul<=L&&ur>=R)
{
COL[po]+=ut;
pushUP(L,R,po); return;
} pushDown(L,R,po); int M=(L+R)/; if(ul<=M)
update(ul,ur,ut,lson);
if(ur>M)
update(ul,ur,ut,rson); pushUP(L,R,po);
} bool cmp(BIAN a,BIAN b)
{
return a.x<b.x;
} int main()
{
int N;
int cas=;
double ans;
double x1,x2,y1,y2; ios::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(); for(cin>>N;N;cin>>N)
{
memset(BIT,,sizeof(BIT));
memset(COL,,sizeof(COL)); for(int i=;i<=N;++i)
{
cin>>x1>>y1>>x2>>y2; hash[ji]=y1;
hash[ou]=y2; bian[ji].state=;
bian[ou].state=-; bian[ji].x=x1;
bian[ou].x=x2; bian[ji].y1=bian[ou].y1=y1;
bian[ji].y2=bian[ou].y2=y2;
} sort(bian+,bian+*N+,cmp);
sort(hash+,hash+*N+); COU=;
for(int i=;i<=*N;++i)
if(hash[i]!=hash[i-])
hash[COU++]=hash[i];
--COU; ans=; for(int i=;i<=*N;++i)
{
ans+=BIT[]*(bian[i].x-bian[i-].x); update(find(bian[i].y1),find(bian[i].y2)-,bian[i].state,,COU,);
} cout<<"Test case #"<<cas++<<endl;
cout<<"Total explored area: "<<ans<<endl<<endl;
} return ;
}
(中等) HDU 1542 Atlantis,扫描线。的更多相关文章
- POJ 1151 HDU 1542 Atlantis(扫描线)
题目大意就是:去一个地方探险,然后给你一些地图描写叙述这个地方,每一个描写叙述是一个矩形的右下角和左上角.地图有些地方是重叠的.所以让你求出被描写叙述的地方的总面积. 扫描线的第一道题,想了又想,啸爷 ...
- (HDU 1542) Atlantis 矩形面积并——扫描线
n个矩形,可以重叠,求面积并. n<=100: 暴力模拟扫描线.模拟赛大水题.(n^2) 甚至网上一种“分块”:分成n^2块,每一块看是否属于一个矩形. 甚至这个题就可以这么做. n<=1 ...
- HDU 1542 Atlantis(矩形面积并)
HDU 1542 Atlantis 题目链接 题意:给定一些矩形,求面积并 思路:利用扫描线,因为这题矩形个数不多,直接暴力扫就能够了.假设数据大.就要用线段树 代码: #include <cs ...
- HDU 1542 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 ...
- 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 ...
- HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 1542 Atlantis(扫描线)题解
题意:给n个可能相交的矩形,问你不重复的总面积 思路:扫描线,一边扫一边加. 扫描线:图片来源:理解扫描线 假设我们要算以下四个矩形面积,显然中间深色的是重复的.我们按照x的大小,从左往右扫,然后用线 ...
随机推荐
- LA 4329 BIT 分治
#include <cstdio> #include <queue> #include <cstring> #include <iostream> #i ...
- Android手机图片适配问题
需求:今天在做ListView的时候遇到一个问题,就是ListView中加载图片的时候.有些图片的大小比较大,所以会出现图片显示不充分的问题. 首先,再不做任何处理的情况下,大小是这样的.宽度是Wra ...
- Ubuntu下安装使用MongoDB
安装 官网下载: https://www.mongodb.org/ 解压解包 重命名为mongodb 移动到/usr/local/目录下 创建连个软连接 ln -s /usr/local/mongo ...
- Problem H: 小火山的围棋梦想 多校训练2(小火山专场)
题目链接:http://acm.zzuli.edu.cn/zzuliacm/problem.php?id=1908 题意:如果'.'被'*'围起来,就把'.'变为'*'. 分析:如果是'*'直接输出, ...
- 转:如何让LoadRunner实现多个场景运行?
场景分析: 有3个不同的场景,分别为搜索,下载,上传,其中3个场景执行顺序为按照搜索->下载->上传流程操作:哪么如何让Loadrunner中如何实现多个场景运行: 方法1:利用Loadr ...
- AngularJs: Reload page
<a ng-click="reloadRoute()" class="navbar-brand" title="home" data- ...
- JSP内置对象--session对象(getId(),getCreationTime(),getLastAccessedTime(),isNew(),invalidate(),setAttribute(),getAttribute())
session对象是javax.servlet.http.HttpSession接口的实例,但是不像HttpServletRequest或HttpServletResponse一样,有父接口,他没有父 ...
- redis 持久化与备份策略 【转载】
本文转载自 http://blog.csdn.net/is_zhoufeng/article/details/10210353 持久化(persistence) 本文是 Redis 持久化文档 的中文 ...
- Android命令行工具logcat详细用法!
logcat是Android中一个命令行工具,可以用于得到程序的log信息. 见板凳详细说明! 本贴内容来自网络,引用网址为:http://hi.baidu.com/%C9%C1%D2%AB% ...
- docker私有仓库搭建(ubuntu 14.04和centos7)
最近是在做一个关于docker云化的项目,马上就要开始实战.下午先做了一个私有仓库搭建的实验,先大概做个笔记,有兴趣的蛮看一下吧. 先在所有机子上都安装上docker,我的是两台ubuntu,分别是1 ...