题目链接:

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. sql join相关

    JOIN: 如果表中有至少一个匹配,则返回行 LEFT JOIN: 即使右表中没有匹配,也从左表返回所有的行,返回左表所有行 RIGHT JOIN: 即使左表中没有匹配,也从右表返回所有的行,返回右表 ...

  2. VC进程间通信之消息传递PostMessge()或SendMessage()

    1.  进程内消息: (1). 仅仅传消息码 (2). 传送消息串 发送端: void CTestDlg::OnBnClickedButtonSend() { CString* msg = new C ...

  3. 解决php网页运行超时问题:Maximum execution time of 30 seconds exceeded

    Fatal error: Maximum execution time of 30 seconds exceeded in C:\Inetpub\wwwroot\ry.php on line 11 意 ...

  4. Linux下实现RAID

    一.实验目的 1.掌握Linux系统下软RAID的实现方法: 2.掌握RAID5的配置过程: 3. 通过实验熟悉RAID.5的特点. 二.实验内容及步骤 1.在VMware中创建一台Linux. 2. ...

  5. Linux 文件系统IO性能优化

    对于LINUX SA来说,服务器性能是需要我们特别关注的,包括CPU.IO.内存等等系统的优化变得至关重要,这里转载一篇非常不错的关于IO优化的文章,供大家参考和学习: 一.关于页面缓存的信息,可以用 ...

  6. 研究下JavaScript中的Rest參数和參数默认值

    研究下JavaScript中的Rest參数和參数默认值 本文将讨论使 JavaScript 函数更有表现力的两个特性:Rest 參数和參数默认值. Rest 參数 通常,我们须要创建一个可变參数的函数 ...

  7. python opener代理

    链接:http://www.jb51.net/article/46495.htm https://www.cnblogs.com/cunyusup/p/7341829.html

  8. win10 64位 安装TensorFlow

    .由于之前安装的是python2.7 ,tensorflow在windows下必须要python3 网上查了一下有三种方法2版本共存 1.不用Anaconda windows 安装python2 与p ...

  9. NVR硬件录像机web无插件播放方案功能实现之相关接口注意事项说明

    该篇博文主要用来说明EasyNVR硬件录像回放版本的相关接口说明和调用的demo: 方便用户的二次开发和集成. 软件根目录会包含接口文档的,因此,本文主要是对一些特定接口的说明和接口实现功能的讲解以及 ...

  10. 更新pip10后 ImportError: cannot import name ‘main'(转)

    解决:找到报错文件,也就是那个pip,然后cd进目录 vi 编辑pip,将里面的内容改为如下所示: # -*- coding: utf-8 -*- import re import sys from ...