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. appendHTML方法ajax加载更多评论实例页面

    //在后添加 <script>var appendHTML = function(el, html) { var divTemp = document.createElement(&quo ...

  2. vue 中slot 的具体用法

    子组件 <template> <div class="slotcontent"> <ul> <!--<slot></sl ...

  3. python链家网高并发异步爬虫and异步存入数据

    python链家网二手房异步IO爬虫,使用asyncio.aiohttp和aiomysql 很多小伙伴初学python时都会学习到爬虫,刚入门时会使用requests.urllib这些同步的库进行单线 ...

  4. UVA - 10976 分数拆分

    题意: 给定正整数k(1<=k <= 10000),找出所有正整数 x>= y, 使得1/k = 1/x + 1/y 分析: 因为 x >= y 所以 1/x <= 1/ ...

  5. admin——django自带数据库管理工具

    admin是Django自带的数据库管理工具,是一个app 在admin.py中可以自己定制类来使admin数据库管理页面展现自己想要查看的信息 models.py from django.db im ...

  6. dva使用及项目搭建

    一.简介 本文将简单分析dva脚手架的使用及项目搭建过程. 首先,dva是一个基于redux和redux-saga的数据流方案,然后为了简化开发体验,dva还额外内置了react-router和fet ...

  7. LA3263 一笔画

    题目大意:依次给定多个点(要求第一个点和最后一个点重叠),把前后两个点相连求最后得到的图形的面的个数 根据欧拉定理: 设平面图的顶点数为V,边数为E,面数为F,则V+F-E = 2 这里的E是指如果一 ...

  8. noip模拟赛 收集果子

    分析:显然的,树形dp,状态也很好想到:f[i][j]表示以i为根的子树收集到j个果子的方案数.转移的话就相当于是背包问题,每个子节点可以选或不选.如果不选子节点k的话,那么以k为根的子树的边无论断不 ...

  9. 592. Fraction Addition and Subtraction

    Problem statement: Given a string representing an expression of fraction addition and subtraction, y ...

  10. JPA中映射关系详细说明(一对多,多对一,一对一、多对多)、@JoinColumn、mappedBy说明

    JPA中的映射关系 jpa中维护one to one ,one to many, many to one ,many to many 四种映射关系. 在每个关系中,双方中的一方在其表中拥有连接列.那么 ...