[HDU1542]Atlantis(扫描线+线段树)
Atlantis
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16436 Accepted Submission(s): 6706Problem DescriptionThere 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.InputThe 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.
OutputFor 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 Input2
10 10 20 20
15 15 25 25.5
0Sample OutputTest case #1
Total explored area: 180.00SourceRecommendlinle
注意只要给一维离散化,且离散化的那一维必然是左闭右开的。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ls (x<<1)
#define rs (ls|1)
#define lson ls,L,mid
#define rson rs,mid+1,R
#define rep(i,l,r) for (int i=l; i<=r; i++)
using namespace std; const int N=;
int n,cnt,T,tot,cov[N<<];
double x1,y1,x2,y2,ans,b[N],len[N<<];
struct P{ double x,y1,y2; int k; }p[N]; bool cmp(P &a,P &b){ return a.x<b.x; } void upd(int x,int L,int R){
if (cov[x]) len[x]=b[R+]-b[L];
else if (L==R) len[x]=; else len[x]=len[ls]+len[rs];
} void mdf(int x,int L,int R,int l,int r,int k){
if (L==l && r==R){ cov[x]+=k; upd(x,L,R); return; }
int mid=(L+R)>>;
if (r<=mid) mdf(lson,l,r,k);
else if (l>mid) mdf(rson,l,r,k);
else mdf(lson,l,mid,k),mdf(rson,mid+,r,k);
upd(x,L,R);
} int main(){
while (scanf("%d",&n),n){
T++; cnt=tot=ans=;
memset(len,,sizeof(len)); memset(cov,,sizeof(cov));
rep(i,,n){
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
p[++cnt]=(P){x1,y1,y2,}; p[++cnt]=(P){x2,y1,y2,-};
b[++tot]=y1; b[++tot]=y2;
}
sort(b+,b+tot+); tot=unique(b+,b+tot+)-b-;
sort(p+,p+cnt+,cmp);
rep(i,,cnt-)
mdf(,,tot,lower_bound(b+,b+tot+,p[i].y1)-b,lower_bound(b+,b+tot+,p[i].y2)-b-,p[i].k),ans+=len[]*(p[i+].x-p[i].x);
printf("Test case #%d\nTotal explored area: %.2lf\n\n",T,ans);
}
return ;
}
[HDU1542]Atlantis(扫描线+线段树)的更多相关文章
- hdu1542 Atlantis (线段树+扫描线+离散化)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- [POJ1151][HDU1542]Atlantis(线段树,扫描线)
英文题面,我就只放个传送门了. Solution 题意是算矩形面积并,这是扫描线算法能解决的经典问题. 算法的大致思想是,把每一个矩形拆成上边和下边(以下称作扫描线),每条扫描线有四个参数l,r,h ...
- hdu-1542 Atlantis(离散化+线段树+扫描线算法)
题目链接: Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- poj1151 Atlantis——扫描线+线段树
题目:http://poj.org/problem?id=1151 经典的扫描线问题: 可以用线段树的每个点代表横向被矩形上下边分割开的每一格,这样将一个矩形的出现或消失化为线段树上的单点修改: 每个 ...
- POJ 1151 Atlantis (扫描线+线段树)
题目链接:http://poj.org/problem?id=1151 题意是平面上给你n个矩形,让你求矩形的面积并. 首先学一下什么是扫描线:http://www.cnblogs.com/scau2 ...
- hdu1542 Atlantis(扫描线+线段树+离散)矩形相交面积
题目链接:点击打开链接 题目描写叙述:给定一些矩形,求这些矩形的总面积.假设有重叠.仅仅算一次 解题思路:扫描线+线段树+离散(代码从上往下扫描) 代码: #include<cstdio> ...
- 【POJ1151】Atlantis(线段树,扫描线)
[POJ1151]Atlantis(线段树,扫描线) 题面 Vjudge 题解 学一学扫描线 其实很简单啦 这道题目要求的就是若干矩形的面积和 把扫描线平行于某个轴扫过去(我选的平行\(y\)轴扫) ...
- Atlantis poj1151 线段树扫描线
Atlantis poj1151 线段树扫描线 题意 题目给了n个矩形,每个矩形给了左下角和右上角的坐标,矩形可能会重叠,求的是矩形最后的面积. 题解思路 这个是我线段树扫描线的第一题,听了学长的讲解 ...
- HDU 3642 - Get The Treasury - [加强版扫描线+线段树]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3642 Time Limit: 10000/5000 MS (Java/Others) Memory L ...
随机推荐
- 教你 Shiro 整合 SpringBoot,避开各种坑(山东数漫江湖)
依赖包 <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-sprin ...
- DTW 算法(转)
DTW为(Dynamic Time Warping,动态时间归准)的简称.应用很广,主要是在模板匹配中,比如说用在孤立词语音识别,计算机视觉中的行为识别,信息检索等中.可能大家学过这些类似的课程都看到 ...
- charger related source code position
Platform Qualcomm MSM8917 or MSM8937 Source kernel/msm-3.18/drivers/power/qpnp-smbcharger.c kernel/m ...
- 64_m2
mimetic-devel-0.9.8-7.fc26.i686.rpm 12-Feb-2017 05:40 288474 mimetic-devel-0.9.8-7.fc26.x86_64.rpm 1 ...
- python之operator操作符函数
operator函数主要分为以下几类:对象比较.逻辑比较.算术运算和序列操作. 举例: #python 3.4 >>> operator.eq(1,2)False >>& ...
- Two Sum ——经典的哈希表的题
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- redis 安装及安装遇到的问题解决
https://blog.csdn.net/jy0902/article/details/19248299 http://q.fireflyclub.org/?/article/24 https:// ...
- hdu5728
详细题解: http://blog.csdn.net/wust_zzwh/article/details/51966450 ……化简公式的能力还不够啊…… #include<bits/stdc+ ...
- 五十九 数据库访问 使用MySQL
MySQL是Web世界中使用最广泛的数据库服务器.SQLite的特点是轻量级.可嵌入,但不能承受高并发访问,适合桌面和移动应用.而MySQL是为服务器端设计的数据库,能承受高并发访问,同时占用的内存也 ...
- magento后台语言
Magento后台自身携带了一个语言切换的功能,见后台左下角 你会发现长长的一串,其中绝大多数语言你可能根本没有机会用到,而你想要从中文切换到英文时,每次都要瞪大眼睛去找英文在下拉框的哪个位置,所以精 ...