hdu 1542 Atlantis(段树&扫描线&面积和)
Atlantis
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6386 Accepted Submission(s): 2814
friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.
(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.
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.
2
10 10 20 20
15 15 25 25.5
0
Test case #1
Total explored area: 180.00
pid=1543" target="_blank">1543
3333面积=底边长*高。这底边长不一定要求连续。所以假设我们知道某一时刻底边长和高就能够算出面积了。而线段树就能够出色的完毕求出某个时间底边长(x轴被覆盖的长度)。
所以我们仅仅须要把全部平行与x轴的边按高度排序。然后依次插入到线段树中。遇到矩形的下边就增加到线段树中。遇到上边就把相应的下边从线段树中删除。文字可能不是非常好理解。绘图看看就知道了。
#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=250;
#define lson L,mid,ls
#define rson mid+1,R,rs
int n,m;
int cov[maxn<<2];
double len[maxn<<2],H[maxn];
struct node
{
double x1,x2,h;
int v;
node(double a=0,double b=0,double c=0,int d=0):x1(a),x2(b),h(c),v(d){}
} seg[maxn];
bool cmp(node a,node b)
{
return a.h<b.h;
}
void init()
{
sort(H,H+m);
m=unique(H,H+m)-H;
}
int Hash(double x)
{
return lower_bound(H,H+m,x)-H;
}
void PushUp(int L,int R,int rt)
{
if(cov[rt])//有标记肯定整块覆盖了
len[rt]=H[R+1]-H[L];
else if(L==R)//没有左右儿子了。 len[rt]=0;
else//没有整块覆盖可是被部分覆盖了
len[rt]=len[rt<<1]+len[rt<<1|1];
}
void update(int L,int R,int rt,int l,int r,int d)
{
if(l<=L&&R<=r)
{
cov[rt]+=d;
PushUp(L,R,rt);
return;
}//这样的标记是不用下传的。由于没删除一个上边。一定有一个下边与之相应。 int mid=(L+R)>>1,ls=rt<<1,rs=ls|1;
if(l<=mid)
update(lson,l,r,d);
if(r>mid)
update(rson,l,r,d);
PushUp(L,R,rt);
}
int main()
{
int cas=1,i,ptr;
double x1,x2,y1,y2,ans;
while(scanf("%d",&n),n)
{
printf("Test case #%d\n",cas++);
for(i=ptr=0;i<n;i++)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
H[ptr]=x1;
seg[ptr++]=node(x1,x2,y1,1);
H[ptr]=x2;
seg[ptr++]=node(x1,x2,y2,-1);
}
m=ptr,ans=0;
init();
sort(seg,seg+ptr,cmp);
memset(len,0,sizeof len);
memset(cov,0,sizeof cov);
for(i=0,ptr--;i<ptr;i++)
{
update(0,m-1,1,Hash(seg[i].x1),Hash(seg[i].x2)-1,seg[i].v);//m个结点m-1条线段
ans+=(seg[i+1].h-seg[i].h)*len[1];
}
printf("Total explored area: %.2lf\n\n",ans);
}
return 0;
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
hdu 1542 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 ...
- 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/Ot ...
- HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdu 1542 Atlantis (线段树扫描线)
大意: 求矩形面积并. 枚举$x$坐标, 线段树维护$[y_1,y_2]$内的边是否被覆盖, 线段树维护边时需要将每条边挂在左端点上. #include <iostream> #inclu ...
- HDU 1542 Atlantis(线段树面积并)
描述 There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. S ...
- POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并
题意:给出矩形两对角点坐标,求矩形面积并. 解法:线段树+离散化. 每加入一个矩形,将两个y值加入yy数组以待离散化,将左边界cover值置为1,右边界置为2,离散后建立的线段树其实是以y值建的树,线 ...
- (HDU 1542) Atlantis 矩形面积并——扫描线
n个矩形,可以重叠,求面积并. n<=100: 暴力模拟扫描线.模拟赛大水题.(n^2) 甚至网上一种“分块”:分成n^2块,每一块看是否属于一个矩形. 甚至这个题就可以这么做. n<=1 ...
随机推荐
- URAL 1180. Stone Game (博弈 + 规律)
1180. Stone Game Time limit: 1.0 second Memory limit: 64 MB Two Nikifors play a funny game. There is ...
- linux中怎样设置dhcpd
linux中怎样设置DHCP 在 linux 以下设置 DHCP一点也不复杂﹐您全部要做的仅仅有一个文件﹕/etc/dhcpd.conf . 以下﹐我用我自己的设置文件来说说怎么改动这个文件﹕ d ...
- ASP.NET - TreeView控件,只操作最后一级节点
效果: 使用母板页进行,左右页面进行跳转. 绑定TreeView控件:http://www.cnblogs.com/KTblog/p/4792302.html 主要功能: 点击节点的时候,只操作最后一 ...
- ASP.NET、WinForm - 判断整个页面文本框是否为空
foreach(Control ctrl in Page.Controls) { foreach(Control childc in ctrl.Controls) { switch(childc.Ge ...
- 中间件(Middleware)
中间件(Middleware) ASP.NET Core开发,开发并使用中间件(Middleware). 中间件是被组装成一个应用程序管道来处理请求和响应的软件组件. 每个组件选择是否传递给管道中的下 ...
- MFC界面相关(彩色工具栏)
MFC工具栏控件 创建工具栏步骤: (1)在Resource View中插入新toolbar (2)在toolbar上双击增加按钮,更改ID为ID_BUTTON,编译后在resource.h中即可看到 ...
- jni 入门 android的C编程之旅 --->环境搭建&&helloworld
需要进行jni的开发有一下几个条件: 1:能初步使用C/C++如果不会,请参读 谭浩强的 C编程语言 2:android应用开发已经基本入门,如果没有,请先行学习 这两个条件基本满足后,我们开始了: ...
- Oracle实用-01:绑定变量
数据库虽然在学校系统学习过,但是在工作中真正使用起来收获又是不一样的,今天起打算将项目中使用到的技术再分享出来,不以书本的顺序,只从碰到的问题为顺序. 虽然不是纯粹的数据库工程师,但是每个程序员总免不 ...
- poj1066 Jugs
poj1066 Jugs http://poj.org/problem?id=1606 解题思路:本题可以用数学方法解得,最易理解,常规的解法是搜索.直接用接近模拟的广度优先搜索即可过. 给两个容器, ...
- 使用CUNIT测试
使用CUNIT测试 一:概述 CUnit是一个c语言的单元测试框架,它是以静态链接库的形式,连接到用户代码中的,主要的功能就是提供了语义丰富的断言和多种测试结果输出接口,可以方便地生成测试报告. 但是 ...