题目链接:

Atlantis

Time Limit: 2000/1000 MS (Java/Others)   

 Memory Limit: 65536/32768 K (Java/Others)

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
 
题意:
 
给了n个矩形的左下角和右上角的坐标,问这些矩形的面积和是多少;
 
思路:
 
离散化后用线段树结合扫描线算法可以做,是扫描线算法的模板题;
 
AC代码:
 
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+;
int n;
double rec[*N],xa,xb,ya,yb;
struct Tree
{
int l,r,cover;
double sum;
};
Tree tree[*N];
struct Line
{
double l,r,h;
int flag;
};
Line line[*N];
int cmp(Line x,Line y)
{
return x.h<y.h;
}
void build(int node,int L,int R)
{
tree[node].sum=;
tree[node].cover=;
tree[node].l=L;
tree[node].r=R;
if(L>=R)return ;
int mid=(L+R)>>;
build(*node,L,mid);
build(*node+,mid+,R);
}
void Pushup(int node)
{
if(tree[node].cover)
{
tree[node].sum=rec[tree[node].r+]-rec[tree[node].l];
}
else
{
if(tree[node].l!=tree[node].r)
tree[node].sum=tree[*node].sum+tree[*node+].sum;
else tree[node].sum=;
}
}
void update(int node,int L,int R,int x)
{
if(L<=tree[node].l&&R>=tree[node].r)
{
tree[node].cover+=x;
Pushup(node);
return ;
}
int mid=(tree[node].l+tree[node].r)>>;
if(R<=mid)update(*node,L,R,x);
else if(L>mid)update(*node+,L,R,x);
else
{
update(*node,L,mid,x);
update(*node+,mid+,R,x);
}
Pushup(node);
}
map<double,int>mp;
int main()
{
int Case=;
while()
{
scanf("%d",&n);
if(n==)break;
printf("Test case #%d\n",Case++);
int cnt=;
for(int i=;i<n;i++)
{
scanf("%lf%lf%lf%lf",&xa,&ya,&xb,&yb); rec[cnt]=line[cnt].l=xa;
line[cnt].r=xb;
line[cnt].flag=;
line[cnt++].h=ya; line[cnt].l=xa;
rec[cnt]=line[cnt].r=xb;
line[cnt].h=yb;
line[cnt++].flag=-;
}
sort(line+,line+cnt,cmp);
sort(rec+,rec+cnt);
int num=;
for(int i=;i<cnt;i++)
{
if(rec[i]!=rec[i-])rec[num++]=rec[i];
}
for(int i=;i<num;i++)
{
mp[rec[i]]=i;
}
build(,,num-);
double ans=; for(int i=;i<cnt-;i++)
{
int fx=mp[line[i].l];
int fy=mp[line[i].r];
update(,fx,fy-,line[i].flag);
ans+=tree[].sum*(line[i+].h-line[i].h);
}
printf("Total explored area: %.2lf\n",ans);
printf("\n");
}
return ;
}
 

hdu-1542 Atlantis(离散化+线段树+扫描线算法)的更多相关文章

  1. HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)

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

  2. HDU - 1542 Atlantis(线段树求面积并)

    https://cn.vjudge.net/problem/HDU-1542 题意 求矩形的面积并 分析 点为浮点数,需要离散化处理. 给定一个矩形的左下角坐标和右上角坐标分别为:(x1,y1).(x ...

  3. HDU 1542"Atlantis"(线段树+扫描线求矩形面积并)

    传送门 •题意 给你 n 矩形,每个矩形给出你 $(x_1,y_1),(x_2,y_2)$ 分别表示这个矩形的左下角和右上角坐标: 让你求这 n 个矩形并的面积: 其中 $x \leq 10^{5} ...

  4. HDU - 1542 扫描线入门+线段树离散化

    扫描线算法+线段树维护简介: 像这种求面积的并集的题目,就适合用扫描线算法解决,具体来说就是这样 类似这种给出点的矩形的对角的点的坐标,然后求出所有矩形面积的交集的问题,可以采用扫描线算法解决.图如下 ...

  5. POJ 1542 Atlantis(线段树 面积 并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 参考网址:http://blog.csdn.net/sunmenggmail/article/d ...

  6. codeforces 610D D. Vika and Segments(离散化+线段树+扫描线算法)

    题目链接: D. Vika and Segments time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  7. (HDU 1542) Atlantis 矩形面积并——扫描线

    n个矩形,可以重叠,求面积并. n<=100: 暴力模拟扫描线.模拟赛大水题.(n^2) 甚至网上一种“分块”:分成n^2块,每一块看是否属于一个矩形. 甚至这个题就可以这么做. n<=1 ...

  8. HDU 1542 Atlantis(矩形面积并)

    HDU 1542 Atlantis 题目链接 题意:给定一些矩形,求面积并 思路:利用扫描线,因为这题矩形个数不多,直接暴力扫就能够了.假设数据大.就要用线段树 代码: #include <cs ...

  9. Atlantis poj1151 线段树扫描线

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

随机推荐

  1. UTF-8 可变编码格式

    转自:http://blog.csdn.net/swedenfeng/article/details/53467720   UTF-8 是一种可变编码格式,长度从一个字节到四个字节,可根据UTF-8字 ...

  2. Android BitmapFactory图片压缩处理(大位图二次採样压缩处理)

    Android实际开发中.在载入大量图片的时候.比方ViewPager.GridView.ListView中,载入了大量的比較大图片就easy出现OOM(内存溢出)的异常,这是由于一个应用的最大内存使 ...

  3. Mongo-Hadoop

    下载 https://github.com/mongodb/mongo-hadoop/releases 解压到/home/kevin/hadoop/hadoop/share/mongo-hadoop- ...

  4. winform 下载文件显示进度和百分比

    /// <summary> /// 下载完成 /// </summary> private void DownloadFileCompleted() { IsComlate = ...

  5. linearLayout 和 relativeLayout的属性区别

    LinearLayout和RelativeLayout 共有属性: java代码中通过btn1关联次控件 android:id="@+id/btn1" 控件宽度 android:l ...

  6. python学习(七)字典学习

    #!/usr/bin/python # 字典 # 当时学java的时候, 语言基础就学了好久, 然后是各种API, 最后才是集合 # 键值对, 可变 # 1. 映射操作 D = {'food' : ' ...

  7. erlang中判断进程是否存活

    一个参数的方法是已知Pid判断进程是否存活.两个参数的方法是已知节点和Pid或进程名判断进程是否存活. is_process_alive(Pid) when is_pid(Pid)->rpc:c ...

  8. andeoid硬件解码

    Finally, I must say, finally, we get low-level media APIs in Android, the Android hardware decoding ...

  9. iOS项目 -- 模仿花椒直播做的第三层设计完整版

    由于是获取第三方的数据,开发的时候,把数据结构分为:闭环数据,和开环数据. 开环数据是网络的第三方数据,自己不能控制的了. 闭环数据是自己的数据,可以进行各式各样的设计. 这是闭环数据的数据库关键字设 ...

  10. 关于打开sdk下载不了的最优秀解决方式

    使用网站:  mirrors.neusoft.edu.cn  东北大学就可以