Atlantis
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 16222   Accepted: 6172

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 思路:首先将横坐标离散化,再对纵坐标排序,然后根据y轴从下往上扫描,每次的高度就是seg[i].y-seg[i-1].y,这就相当于分矩形的宽,然后要做的事就是查询x轴(矩形长)的有效长度,这就要交给线段树了。 AC代码:
 /*************************************************************************
> File Name: area.cpp
> Author: wangzhili
> Mail: wangstdio.h@gmail.com
> Created Time: 2014/3/1 星期六 16:06:57
************************************************************************/ #include<iostream>
#include<algorithm>
#include<cstdio>
#define MAX 1000
using namespace std;
class TreeNode
{
public:
int left;
int right;
int mid;
int cover;
int flag;
double length;
};
typedef struct
{
double xl, xr, y;
int flag;
}Line;
TreeNode node[*MAX];
Line seg[MAX];
double x[MAX];
double length;
bool cmp(Line a, Line b)
{
return a.y < b.y;
} void BuildTree(int k, int l, int r)
{
node[k].left = l;
node[k].right = r;
node[k].mid = (l + r) >> ;
node[k].cover = ;
node[k].flag = ;
if(l + == r)
{
node[k].flag = ;
return ;
}
int mid = (l + r) >> ;
BuildTree(k << , l, mid);
BuildTree(k << |, mid, r);
} void UpdateTree(int k, int l, int r, int flag)
{
if(node[k].left == l && node[k].right == r)
{
node[k].cover += flag;
node[k].length = x[r-] - x[l-];
return ;
}
if(node[k].flag)
return ;
if(node[k].mid <= l)
UpdateTree(k << |, l, r, flag);
else if(node[k].mid >= r)
UpdateTree(k << , l, r, flag);
else
{
UpdateTree(k << , l, node[k].mid, flag);
UpdateTree(k << |, node[k].mid, r, flag);
}
} void GetLength(int k)
{
if(node[k].cover > )
{
length += node[k].length;
return ;
}
if(node[k].flag)
return;
GetLength(k << );
GetLength(k << |);
} int GetIndex(double num, int length)
{
int l, r, mid;
l = , r = length-;
while(l <= r)
{
mid = (l + r) >> ;
if(x[mid] == num)
return mid;
else if(x[mid] > num)
r = mid - ;
else
l = mid + ;
}
} int main(int argc, char const *argv[])
{
int n, i, j, k, cnt;
int xl, xr;
double ans;
double x1, y1, x2, y2;
cnt = ;
BuildTree(, , );
// freopen("in.c", "r", stdin);
while(~scanf("%d", &n) && n)
{
j = ;
ans = .;
for(i = ; i < ; i ++)
{
node[i].cover = ;
}
for(i = ; i < n; i ++)
{
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
seg[j].xl = x1;
seg[j].xr = x2;
seg[j].y = y1;
x[j] = x1;
seg[j ++].flag = ;
seg[j].xl = x1;
seg[j].xr = x2;
seg[j].y = y2;
x[j] = x2;
seg[j ++].flag = -;
}
sort(x, x+j);
sort(seg, seg+j, cmp);
k = ;
for(i = ; i < j; i ++)
{
if(x[i] != x[i-])
x[k ++] = x[i];
}
xl = GetIndex(seg[].xl, k) + ;
xr = GetIndex(seg[].xr, k) + ;
UpdateTree(, xl, xr, seg[].flag);
length = ;
GetLength();
for(i = ; i < j; i ++)
{
ans += (seg[i].y-seg[i-].y)*length;
xl = GetIndex(seg[i].xl, k)+;
xr = GetIndex(seg[i].xr, k)+;
UpdateTree(, xl, xr, seg[i].flag);
length = .;
GetLength();
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n", ++cnt, ans);
}
return ;
}
												

POJ -- 1151的更多相关文章

  1. 扫描线三巨头 hdu1928&&hdu 1255 && hdu 1542 [POJ 1151]

    学习链接:http://blog.csdn.net/lwt36/article/details/48908031 学习扫描线主要学习的是一种扫描的思想,后期可以求解很多问题. 扫描线求矩形周长并 hd ...

  2. POJ 1151 Atlantis(扫描线)

    题目原链接:http://poj.org/problem?id=1151 题目中文翻译: POJ 1151 Atlantis Time Limit: 1000MS   Memory Limit: 10 ...

  3. POJ 1151 Atlantis(线段树-扫描线,矩形面积并)

    题目链接:http://poj.org/problem?id=1151 题目大意:坐标轴上给你n个矩形, 问这n个矩形覆盖的面积 题目思路:矩形面积并. 代码如下: #include<stdio ...

  4. POJ 1151 Atlantis (扫描线+线段树)

    题目链接:http://poj.org/problem?id=1151 题意是平面上给你n个矩形,让你求矩形的面积并. 首先学一下什么是扫描线:http://www.cnblogs.com/scau2 ...

  5. POJ 1151 Atlantis 矩形面积求交/线段树扫描线

    Atlantis 题目连接 http://poj.org/problem?id=1151 Description here are several ancient Greek texts that c ...

  6. poj 1151(离散化+矩形面积并)

    题目链接:http://poj.org/problem?id=1151 关于离散化,这篇博客讲的很好:http://www.cppblog.com/MiYu/archive/2010/10/15/12 ...

  7. 【POJ 1151】Atlantis

    离散化后扫描线扫一遍. 夏令营时gty学长就讲过扫描线,可惜当时too naive,知道现在才写出模板题. 当时也不会线段树啊233 #include<cstdio> #include&l ...

  8. POJ 1151 Atlantis 线段树+离散化+扫描线

    这次是求矩形面积并 /* Problem: 1151 User: 96655 Memory: 716K Time: 0MS Language: G++ Result: Accepted */ #inc ...

  9. [POJ 1151] Atlantis

    一样的题:HDU 1542 Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18148   Accepted ...

  10. hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

随机推荐

  1. JS数值输入控制

    在html文本框录入数值时,可用如下方法进行控制判断. 整数:<input type="text" name="aaa" onkeypress=" ...

  2. 01_根据Id查询User的数据

    [工程目录] [数据库表中内容 user表] [sqlMapConfig.xml配置文件主要内容] 简述:sqlMapConfig.xml配置文件主要有两个作用: 1.配置和数据连接的相关信息,例如事 ...

  3. 【转】JSON简介以及用法代码汇总

    什么是JSON? JavaScript 对象表示法(JavaScript Object Notation). JSON是一种轻量级的数据交换格式,某个JSON格式的文件内部譬如可以长成这样: { &q ...

  4. 使用Map辅助拼装树状结构,消除递归调用

    目前菜单或其他树状结构在数据库中的存储,多数是以一个parentid作为关联字段,以一维形式存储.使用时全部查询出来,然后在内存中拼装成树状结构.现在主要涉及的是拼装方法的问题. 一般可以进行 递归调 ...

  5. yii2单独给input或者其他标签定义class

    <?= $form->field($model, 'price5', ['options' => ['class' => 'col-sm-6']])->textInput ...

  6. CentOS6.4 安装aria2多线程下载工具

    aria2是一个Linux下的多线程下载工具,支持HTTP/HTTPS.FTP.BitTorrent.Metalink协议. 平时在linux上下载http上的东西常用如wget.curl命令,但是他 ...

  7. DISC免费性格测试题

    现在给大家推荐一款世界500强和猎头公司招聘人才时用的DISC性格测评,用在找对象方面也比较合适.大家不妨看下自己的性格,就知道该找什么样的意中人啦--- 在每一个大标题中的四个选择题中只选择一个最符 ...

  8. poj 1681 Painter's Problem

    Painter's Problem 题意:给一个n*n(1 <= n <= 15)具有初始颜色(颜色只有yellow&white两种,即01矩阵)的square染色,每次对一个方格 ...

  9. WF工作流与管理类应用系统工作流需求实现的一些误区

             如今实现各种应用系统大家都知道工作流是一个非常重要的环节,不同的业务系统的工作流需求是需要找相应的工作流产品去实现的,因为不同工作流产品的架构细节也许会成为某类需求实现的瓶颈. WF ...

  10. C++ 类族的设计

     - 类族的设计]    按以下的提示,由基类的设计和测试开始,逐渐地完成各个类的设计,求出圆格柱体的表面积.体积并输出并且完成要求的计算任务:    (1)先建立一个Point(点)类,包含数据成员 ...