hdu 1542 Atlantis(线段树,扫描线)
Atlantis
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6116 Accepted Submission(s): 2677
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 ::这道题我是参考hh博客上的代码。用线段树来保存当前被覆盖的线段长度总和。 建议:如果刚学线段树+扫描线,最好把那sample input 作为测试数据输出中间变量的值, 这样可以有更深的理解 代码:#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int N=500;
double sum[N<<2];
int col[N<<2];
double x[N]; struct node
{
double l,r,h;
int s;
bool operator < (const node & t) const {
return h<t.h;
}
}e[N]; void add(node &e,double l,double r,double h,int s)
{
e.l=l; e.r=r; e.h=h; e.s=s;
} int Bina(double x[],int n,double a)
{
int l=0,r=n-1;
while(l<=r)
{
int m=(l+r)>>1;
if(x[m]==a) return m;
if(x[m]<a) l=m+1;
else r=m-1;
}
return -1;
} void PushUp(int rt,int l,int r)
{
if(col[rt]) sum[rt]=x[r+1]-x[l];
else sum[rt]=sum[rt<<1]+sum[rt<<1|1];
} void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
col[rt]+=c;
PushUp(rt,l,r);
return ;
}
int m=(l+r)>>1;
if(L<=m) update(L,R,c,lson);
if(R>m) update(L,R,c,rson);
PushUp(rt,l,r);
} int main()
{
// freopen("in.txt","r",stdin);
int n,cas=1;
while(scanf("%d",&n)>0&&n)
{
int k=0,i;
for(i=0; i<n; i++)
{
double a,b,c,d;
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
x[k]=a;
add(e[k++],a,c,b,1);
x[k]=c;
add(e[k++],a,c,d,-1);
}
sort(e,e+k);
///////离散化////////////////////////////////////
sort(x,x+k);
int m=1;
for(i=1; i<k; i++)
{
if(x[i]!=x[i-1])
x[m++]=x[i];
}
////////////////////////////////////////////////
// for(i=0; i<m ;i++) printf("x[%d]=%lf\n",i,x[i]);
// for(i=0; i<k; i++) printf("%d-> l=%lf, r=%lf, h=%lf ..s=%d\n",i,e[i].l,e[i].r,e[i].h,e[i].s); memset(sum,0,sizeof(sum));
memset(col,0,sizeof(col));
double ans=0;
for(i=0; i<k-1; i++)
{
int L=Bina(x,m,e[i].l);
int R=Bina(x,m,e[i].r)-1;
if(L<=R) update(L,R,e[i].s,0,k-1,1);
// printf("L=%d, x[L]=%lf ; R=%d, x[R]=%lf\n",L,x[L],R,x[R]);
// printf("覆盖区间长度=%lf\n",sum[1]);
ans+=(e[i+1].h-e[i].h)*sum[1];//e[i+1].h-e[i].h是固定的长度H,sum[1]是在覆盖在H区域内的线段有多长
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n",cas++,ans);
}
return 0;
}
hdu 1542 Atlantis(线段树,扫描线)的更多相关文章
- 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 (线段树扫描线)
大意: 求矩形面积并. 枚举$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值建的树,线 ...
- Atlantis HDU - 1542 (线段树扫描线)
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...
- hdu 1542(线段树+扫描线 求矩形相交面积)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- hdu 1542 Atlantis(段树&扫描线&面积和)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdu1542 Atlantis 线段树--扫描线求面积并
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...
随机推荐
- 环信SDK与Apple Watch的结合(3)
第3章主要介绍怎样在Watch App的页面上显示iPhone程序里的数据.主要操作的是“EMWatchOCDemo WatchKit Extension”这个文件夹,附源码EMWatchOCDemo ...
- MEF核心笔记(6)让 MEF 拥抱 AOP
场景: 最近推荐同事在项目中使用起了 MEF,用其构建一个插件式的多人开发框架,因为该框架不是让我去设计了,所以对于 MEF 和 IOC 等概念不是很了解的同事,便会出现各种问题.接入 AOP 便是其 ...
- Configuring a Windows Azure Project
A Windows Azure project includes two configuration files: ServiceDefinition.csdef and ServiceConfigu ...
- asp.net控件的Hyperlink控件
Asp.net控件: Hyperlink控件:Hyperlink控件又称为超链接控件,该控件在功能上跟Html的<a herf=””>控件相似,其显示的模式为超链接的形式. 注意: Hyp ...
- sql server聚合函数sum计算出来为空,怎样返回0
通常我们计算数据库中表的数据有几个常用的聚合函数 1.count : 计数 2.sum: 计算总和 3.avg: 取平均值 4.max: 取最大值 5.min: 取最小值 6.isnull: 当返回数 ...
- 重新想象 Windows 8.1 Store Apps (73) - 新增控件: DatePicker, TimePicker
[源码下载] 重新想象 Windows 8.1 Store Apps (73) - 新增控件: DatePicker, TimePicker 作者:webabcd 介绍重新想象 Windows 8.1 ...
- 使用SignalR+Asp.net创建实时聊天应用程序
一.概述: 使用 ASP.NET 那么 SignalR 2 创建一个实时聊天应用程序.将 SignalR 添加 MVC 5 应用程序中,并创建聊天视图发送并显示消息. 在Demo中,将学习Signal ...
- git 使用笔记(二)
续 2.15 删除文件 $ rm testDel.txt删除掉工作区的testDel.txt文件, 1)这时可以通过git checkout -- testDel.txt从版本库恢复该文件到工作区 2 ...
- 【OpenCV】OpenCV中GPU模块使用
CUDA基本使用方法 在介绍OpenCV中GPU模块使用之前,先回顾下CUDA的一般使用方法,其基本步骤如下: 1.主机代码执行:2.传输数据到GPU:3.确定grid,block大小: 4.调用内核 ...
- webservice 的wsdl文件生成客户端java类
提供两个方法: 第一个: 发布webservice项目后, 地址栏地址 http://localhost:8888/lxitedu.webservice.cxf-ch2/services/userS ...