题目链接:

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. 云数据库 RDS 版怎么创建数据库和账号MySQL 5.7版

    若要使用云数据库RDS,您需要在实例中创建数据库和账号.对于MySQL 5.7版本的实例,您需要通过RDS控制台创建一个初始账号,然后可以通过数据管理(DMS)控制台创建和管理数据库.本文将主要介绍在 ...

  2. 用命令行执行ROBOT FRAMEWORK

    除了在ride中执行用例,我们也可以通过命令行的形式执行用例. 1.执行一整个项目 pybot+项目路径 2.执行某个测试集 pybot+测试集的路径 3.执行某个测试集里面的某个用例 pybot - ...

  3. C​P​U​_​C​S​t​a​t​e​_​P​S​t​a​t​e and then ACPI on Wiki

    http://wenku.baidu.com/link?url=eHbdT4EjdJx3dsQETGUIL8q1K3_EyuzGLWT0G103AEca0vs0gHR_v_3c0oaUL2gbkrr8 ...

  4. 命令行高效操作Git,看这篇就够了

    原文地址:http://blog.jboost.cn/2019/06/16/use-git.html 对于软件开发人员来说,git几乎是每天都需要接触的工具.但对于相处如此亲密的工作伙伴,你对它的了解 ...

  5. Android Calendar的学习与运用

    [java]mport java.text.DateFormat; import java.text.ParsePosition; import java.text.SimpleDateFormat; ...

  6. HDFS源码分析之数据块及副本状态BlockUCState、ReplicaState

    关于数据块.副本的介绍,请参考文章<HDFS源码分析之数据块Block.副本Replica>. 一.数据块状态BlockUCState 数据块状态用枚举类BlockUCState来表示,代 ...

  7. KVM+VNC 虚拟机远程管理

    1.安装kvm grep -E -o 'vmx|svm' /proc/cpuinfo #检查服务器是否支持虚拟化(vmx为interl平台.svm是AMD平台) #安装KVM所需软件包: yum gr ...

  8. OpenCV 中的三大数据类型:CvMat 类型

    前言 本文将介绍 OpenCV 中的矩阵结构 CvMat 并提供几个很常用的矩阵使用方法. 更多的矩阵处理函数还请参阅相关资料. CvMat 的类型定义 typedef struct CvMat { ...

  9. 多文档自己主动文摘:Multi-Document Summarization,MDS

  10. lombok插件安装

    eclipse安装lombok插件 lombok注解介绍 记得最后,加入的配置文件中的jar包,最好写成相对路径,这样.eclipse移动位置后,不会报错.