Atlantis
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 21511   Accepted: 8110

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 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

Source

题解:
 题目的意思是给定n个矩形的2n个坐标,求矩形的覆盖面积。如果开一个大的bool数组,将覆盖过的部分更新为true,再从头到尾扫描一遍,在坐标范围比较小的情况下,可以求解。但是如果坐标x,y的取值范围很大,比如[-10^8,10^8],用上面这个方法就不能求解了;而且坐标还有可能是实数,上面的方法就更加不可行了,需要寻找一种新的解法,就是下面要说到的“离散化”。
    注意到要表示一个矩形,只需要知道其2个顶点的坐标就可以了(最左下,最右上)。可以用2个数组x[0...2n-1],y[0...2n-1]记录下矩形Ri的2个坐标(x1,y1),(x2,y2),然后将数组x[0...xn-1],y[0...2n-1]排序,为下一步的扫描线作准备,这就是离散化的思想。这题还可以用线段树做进一步优化,但是这里只介绍离散化的思想。
    看下面这个例子:有2个矩形(1,1),(3,3)和(2,2),(4,4)。如图:

图中虚线表示扫描线,下一步工作只需要将这2个矩形覆盖过的部分的bool数组的对应位置更新为true,接下去用扫描线从左到右,从上到下扫描一遍,就可以求出矩形覆盖的总面积。
    这个图对应的bool数组的值如下:
    1 1 0                       1 2 3
    1 1 1       <---->       4 5 6
    0 1 1                       7 8 9

AC代码:
注意::::POJ1151上G++要用%.2f才能过!%.2lf WA!
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=;
const double eps=1e-;
double ans=,x[N<<],y[N<<],pos[N][];
bool hash[N<<][N<<];
int cmp(const void *a,const void *b){
double *aa=(double *)a;
double *bb=(double *)b;
if(fabs(*aa-*bb)<=eps) return ;
else if(*aa-*bb>) return ;
return -;
}
int main(){int i,j,k,n,x1,y1,x2,y2,cas=;
while(scanf("%d",&n)==){
if(!n) break;
for(ans=k=i=;i<n;i++,k+=){
scanf("%lf%lf%lf%lf",&pos[i][],&pos[i][],&pos[i][],&pos[i][]);
x[k]=pos[i][];y[k]=pos[i][];x[k+]=pos[i][];y[k+]=pos[i][];
}
memset(hash,,sizeof hash);
qsort(x,n<<,sizeof x[],cmp);
qsort(y,n<<,sizeof y[],cmp);
for(i=;i<n;i++){
for(k=;fabs(x[k]-pos[i][])>eps;k++); x1=k;
for(k=;fabs(y[k]-pos[i][])>eps;k++); y1=k;
for(k=;fabs(x[k]-pos[i][])>eps;k++); x2=k;
for(k=;fabs(y[k]-pos[i][])>eps;k++); y2=k;
for(j=x1;j<x2;j++){
for(k=y1;k<y2;k++){
hash[j][k]=;
}
}
}
for(i=;i<*n-;i++){
for(j=;j<*n-;j++){
ans+=hash[i][j]*(x[i+]-x[i])*(y[j+]-y[j]);
}
}
printf("Test case #%d\n",++cas);
printf("Total explored area: %.2lf\n\n",ans);
}
return ;
}

poj1151==codevs 3044 矩形面积求并的更多相关文章

  1. codevs 3044 矩形面积求并

    3044 矩形面积求并   题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Description 可能有多组数据,读到n=0为止(不 ...

  2. codevs 3044 矩形面积求并 (扫描线)

    /* 之前一直偷懒离散化+暴力做着题 今天搞一下扫描线 自己按照线段树的一般写法写的有些问题 因为不用于以前的区间sum so 题解搬运者23333 Orz~ 去掉了打标记的过程 同时更新区间的时候先 ...

  3. codevs 3044 矩形面积求并 || hdu 1542

    这个线段树的作用其实是维护一组(1维 平面(?) 上的)线段覆盖的区域的总长度,支持加入/删除一条线段. 线段树只能维护整数下标,因此要离散化. 也可以理解为将每一条处理的线段分解为一些小线段,要求每 ...

  4. codves 3044 矩形面积求并

    codves  3044 矩形面积求并  题目等级 : 钻石 Diamond 题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Desc ...

  5. 【题解】codevs 3044 矩形面积合并

    传送门 3044 矩形面积求并 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下 ...

  6. 3044 矩形面积求并 - Wikioi

    题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Description 可能有多组数据,读到n=0为止(不超过15组) 每组数据第一行 ...

  7. [Codevs] 矩形面积求并

    http://codevs.cn/problem/3044/ 线段树扫描线矩形面积求并 基本思路就是将每个矩形的长(平行于x轴的边)投影到线段树上 下边+1,上边-1: 然后根据线段树的权值和与相邻两 ...

  8. [codevs3044][POJ1151]矩形面积求并

    [codevs3044][POJ1151]矩形面积求并 试题描述 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入 可能有多组数据,读到n=0为止(不超过15组) 每组数据第一行一个数n ...

  9. 矩形面积求并(codevs 3044)

    题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Description 可能有多组数据,读到n=0为止(不超过15组) 每组数据第一行 ...

随机推荐

  1. iis如何在dos中注册

    iis如何在dos中注册   2009-09-23 08:13 提问者采纳   cd \cd c:\windows\microsoft.net\framework\v2.0.50727aspnet_r ...

  2. scikit-learn - 分类模型的评估 (classification_report)

    使用说明 参数 sklearn.metrics.classification_report(y_true, y_pred, labels=None, target_names=None, sample ...

  3. jquery attr的属性

    在JS中设置节点的属性与属性值用到setAttribute(),获得节点的属性与属性值用到getAttribute(),而在jquery中,用一个attr()就可以全部搞定了,赞一个先 ^^ jque ...

  4. Python入门之类(class)

    面向对象三大特性 面向对象的三大特性是指:封装.继承和多态. 一.封装 封装,顾名思义就是将内容封装到某个地方,以后再去调用被封装在某处的内容. 所以,在使用面向对象的封装特性时,需要: 将内容封装到 ...

  5. [kuangbin带你飞]专题四 最短路练习

    对于最短路,我主要使用的就是dijkstra,Floyd,SPFA这三个算法.先来介绍一下这三个算法. 1. dijkstra算法.它适用于边权为正的情况,它是单源最短路,就是从单个源点出发到所有的结 ...

  6. mysql主从同步,主库宕机解决方案

    链接:https://blog.csdn.net/zfl589778/article/details/51441719

  7. cookie和session的区别及session的生命周期

    这些都是基础知识,不过有必要做深入了解.先简单介绍一下. 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择,都纪录 ...

  8. PHP面向对象设计五大原则(SOLID)梳理总结

    PHP设计原则梳理,参考<PHP核心技术与最佳实践>.<敏捷开发原则.模式与实践>,文章PHP面向对象设计的五大原则.设计模式原则SOLID 单一职责原则(Single Res ...

  9. GitHub & puppeteer & Chinese character & bug

    GitHub & puppeteer & Chinese character & bug https://github.com/GoogleChrome/puppeteer/b ...

  10. hdu 5029树链剖分

    /* 解:标记区间端点,按深度标记上+下-. 然后用线段树维护求出最小的,再将它映射回来 */ #pragma comment(linker, "/STACK:102400000,10240 ...