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 ...
随机推荐
- Netty IO线程模型学习总结
Netty框架的 主要线程是IO线程.线程模型的好坏直接决定了系统的吞吐量.并发性和安全性. Netty的线程模型遵循了Reactor的基础线程模型.以下我们先一起看下该模型 Reactor线程模型 ...
- BMP文件结构
1. 位图文件头 位图文件头包含有关于文件类型.文件大小.存放位置等信息,在Windows 3.0以上版本的位图文件中用BITMAPFILEHEADER结构来定义: typedef struct ta ...
- Head First PHP &MySQL学习笔记
近期一段时间在学习PHP,买了<Head First PHP&MySQL>中文版这本书,之前买过<Head First设计模式>,感觉这系列的书籍整体来说非常不错. ...
- Swift - 多线程实现方式(1) - NSThread
1,Swift继续使用Object-C原有的一套线程,包括三种多线程编程技术: (1)NSThread (2)Cocoa NSOperation(NSOperation和NSOperationQueu ...
- 用log(N)的解法实现数值的整数次方
// // main.m // c++test // // Created by andyyang on 6/3/13. // Copyright (c) 2013 andyyang. All rig ...
- 利用ScktSrvr打造多功能Socket服务器
Socket服务端编程中最重要的也是最难处理的工作便是客户请求的处理和数据的接收和发送,如果每一个Socket服务器应用程序的开发都要从头到尾处理这些事情的话,人将会很累,也会浪费大量时间.试想,如果 ...
- eval 捕获dbi错误
[root@dr-mysql01 ~]# cat t2.pl use DBI; my $dbUser='zabbix'; my $user="root"; my $passwd=& ...
- linux c编程 多线程(初级)《转载》---赠人玫瑰,手有余香!
原文地址:http://blog.csdn.net/liang890319/article/details/8393120 进程简单的说就是把一段代码复制成多份,并让他们同时执行.进程间通信是为了 ...
- winform实现listview中combox
一.概要 因为要在项目中要在ListView中实现下拉框选择,用DataGrid的话,一个不美观,二个绑定数据麻烦,参考网上一种做法,就是单击ListView时,判断单击的区域,然后将Combox控件 ...
- Ext图表的精彩
最近做了几个Ext的图表,觉得效果还不错,分享一下 1.Chart[饼图.折线图.柱状图.堆状图]: 2.Grid: 3.最大化: