Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16436    Accepted Submission(s): 6706

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
 
Source
 
Recommend
linle
 

Statistic | Submit | Discuss | Note

注意只要给一维离散化,且离散化的那一维必然是左闭右开的。

 #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(扫描线+线段树)的更多相关文章

  1. hdu1542 Atlantis (线段树+扫描线+离散化)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  2. [POJ1151][HDU1542]Atlantis(线段树,扫描线)

    英文题面,我就只放个传送门了. Solution  题意是算矩形面积并,这是扫描线算法能解决的经典问题. 算法的大致思想是,把每一个矩形拆成上边和下边(以下称作扫描线),每条扫描线有四个参数l,r,h ...

  3. hdu-1542 Atlantis(离散化+线段树+扫描线算法)

    题目链接: Atlantis Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/32768 K (Java/Others) ...

  4. poj1151 Atlantis——扫描线+线段树

    题目:http://poj.org/problem?id=1151 经典的扫描线问题: 可以用线段树的每个点代表横向被矩形上下边分割开的每一格,这样将一个矩形的出现或消失化为线段树上的单点修改: 每个 ...

  5. POJ 1151 Atlantis (扫描线+线段树)

    题目链接:http://poj.org/problem?id=1151 题意是平面上给你n个矩形,让你求矩形的面积并. 首先学一下什么是扫描线:http://www.cnblogs.com/scau2 ...

  6. hdu1542 Atlantis(扫描线+线段树+离散)矩形相交面积

    题目链接:点击打开链接 题目描写叙述:给定一些矩形,求这些矩形的总面积.假设有重叠.仅仅算一次 解题思路:扫描线+线段树+离散(代码从上往下扫描) 代码: #include<cstdio> ...

  7. 【POJ1151】Atlantis(线段树,扫描线)

    [POJ1151]Atlantis(线段树,扫描线) 题面 Vjudge 题解 学一学扫描线 其实很简单啦 这道题目要求的就是若干矩形的面积和 把扫描线平行于某个轴扫过去(我选的平行\(y\)轴扫) ...

  8. Atlantis poj1151 线段树扫描线

    Atlantis poj1151 线段树扫描线 题意 题目给了n个矩形,每个矩形给了左下角和右上角的坐标,矩形可能会重叠,求的是矩形最后的面积. 题解思路 这个是我线段树扫描线的第一题,听了学长的讲解 ...

  9. HDU 3642 - Get The Treasury - [加强版扫描线+线段树]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3642 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

随机推荐

  1. 【NOIP】提高组2015 子串

    [题意]求从字符串A中取出k个互不重叠的非空子串顺序拼接形成B的方案数.n<=1000,m<=100,k<=m. [算法]动态规划 [题解]这题主要是将从i-l转移变成从i-1转移, ...

  2. 【51NOD-0】1008 N的阶乘 mod P

    [算法]简单数学 [题解]多项式展开:(a*b)%p=(a%p*b%p)%p #include<cstdio> #include<algorithm> #define rep( ...

  3. FileReader 与canvas结合使用显示图片

    话不多少,直接上代码 function fileChange() { var file = this.files[0]; var imageType = /^image\//; //是否是图片 if ...

  4. Opencv模块功能介绍

    本文为原创作品,转载请注明出处 欢迎关注我的博客:http://blog.csdn.net/hit2015spring和http://www.cnblogs.com/xujianqing/   站在巨 ...

  5. python基础===新式类与经典类

    首先: Python 2.x中默认都是经典类,只有显式继承了object才是新式类 Python 3.x中默认都是新式类,不必显式的继承object 这两种类的区别: 新式类重定义的方法更多,当然这不 ...

  6. 2017多校第8场 HDU 6143 Killer Names 容斥,组合计数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6143 题意:m种颜色需要为两段长度为n的格子染色,且这两段之间不能出现相同的颜色,问总共有多少种情况. ...

  7. Xcode7 iOS9.0 的真机调试

    Xcode7的真机调试: 1.Xcode偏好 -> 账号 -> 增加 Apple ID ->显示 free 2.Target 运行 iOS 版本号 3.修正 Team 项   选择 ...

  8. Oracle简述

    Oracle是甲骨文公司推出的一款大型数据库管理系统.甲骨文公司成立于1977年,总部位于美国加利福尼亚州的红木滩.1989年,Oracle正式进入中国市场:2013年,甲骨文超越 IBM ,成为继 ...

  9. Leetcode 之Anagrams(35)

    回文构词法,将字母顺序打乱.可将字母重新排序,若它们相等,则属于同一组anagrams. 可通过hashmap来做,将排序后的字母作为key.注意后面取hashmap值时的做法. vector< ...

  10. HDU-4255

    A Famous Grid Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...