题目链接:

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. Python 2.7 升 3.4

    Ubuntu 14.04 已经安装有python3.4.0 命令行使用python3 或者创建链接即可 ln -s /usr/bin/python3 /usr/bin/python [推荐此方法,然后 ...

  2. Mode Standby

    Modern Standby 1.Connected Standby和 Connected Standby是Windows 8全新的电源管理系统,即当系统进入休眠状态时,应用程式虽处於暂停(suspe ...

  3. EntityFramework 6.0 修改一个已经存在的对象

    public void UpdateObj(someobject obj) { db.Entry(obj).State = EntityState.Modified; db.SaveChanges() ...

  4. ReentrentLock重入锁

    ReentrentLock lock=new ReentrentLock(); lock.lock(); //锁的代码 finally{ lock.unlock(); } ReentrentLock ...

  5. 集群 安装 配置FastDFS

    FastDFS 集群 安装 配置 这篇文章介绍如何搭建FastDFS 集群 FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载 ...

  6. ios -- 极光推送《3》 pod 方法

    iOS SDK 集成指南 SDK说明 适用版本 本文匹配的 SDK版本:r2.1.5 以后.查看最近更新了解最新的SDK更新情况.使用Xcode 6及以上版本可以使用新版Push SDK,Xcode ...

  7. Android Thread.UncaughtExceptionHandler异常消息捕获

    public void uncaughtException(Thread thread, Throwable ex) { //处理异常 Log.e("崩溃",thread.getN ...

  8. EasyDarwin支持GB28181协议开发

    本文转自:http://blog.csdn.net/gavin1010/article/details/77926853 EasyGB28181服务器开发 背景 当前的安防行业,除了私有协议,普遍使用 ...

  9. live555二次开发经验总结:RTSPClient客户端与RTSPServer服务器

    live555介绍 安防领域的流媒体开发者估计没有谁不知道live555的,可能并不是因为其架构有多牛,代码有多好看,而是因为这玩意存在的年限实在是太长了,从changelog来看,live555从2 ...

  10. Spring Boot:Thymeleaf篇

    Spring Boot干货系列:(四)Thymeleaf篇http://www.cnblogs.com/zheting/p/6707037.html 前言 Web开发是我们平时开发中至关重要的,这里就 ...