题目链接:

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. 目标主体名称不正确,无法生成 SSPI 上下文。

    参考地址:http://blog.csdn.net/burgess_liu/article/details/18300959 两个命令:setspn -L Server03 和 setspn -D S ...

  2. 解析iscroll-小demo

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  3. .NET实现爬虫

    前几天看到一个.NET Core写成的爬虫,有些莫名的小兴奋,之前一直用集搜客去爬拉勾网的招聘信息,这个傻瓜化工具相当于用HTML模板页去标记DOM节点,然后在浏览器窗口上模拟人的浏览行为同时跟踪节点 ...

  4. html中图片上传预览的实现

    本地图片预览 第一种方法 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type& ...

  5. Centos内核版本升级

  6. java 泛型小小的测试题

    判断以下哪种书写时正确的? 1.ArrayList<String> lists = new ArrayList<String>();2.ArrayList<Object& ...

  7. 将众多小文件输入Hadoop的解决方案 可挂载的HDFS

    配置HDFS为可挂载后: 1-可挂载后才支持非完整POSIX语义: 2-仍然不支持随机写入,仍然为“一次写入,多次读取”: 3-可能误用,导致众多小文件: : 1-使用Solr存储和检索小文件: 2- ...

  8. 我的Android进阶之旅------>Android检测wifi连接状态

    今天要实现监听系统Wifi连接状态,下面代码简化后提取出来的,以备后用. step1. 编写BroadcastReceiver import android.content.BroadcastRece ...

  9. PHP Framework

    PHP Framework is built for PHP developers who need elegant toolkit to create full-featured web appli ...

  10. 【Java线程】锁机制:synchronized、Lock、Condition(转)

    原文地址 1.synchronized 把代码块声明为 synchronized,有两个重要后果,通常是指该代码具有 原子性(atomicity)和 可见性(visibility). 1.1 原子性 ...