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. xcode编译运行报错纪录(持续更新)

    ---恢复内容开始--- 1. Undefined symbols for architecture i386: "_deflate", referenced from: -[NS ...

  2. js生成动态日历

    效果图:   看代码: <html> <head> <title>动态日历</title> <style type="text/css& ...

  3. 通过 OpenNI 建立 Kinect 3D Point Cloud

    這篇還是算延續前一篇的<透過 OpneNI 合併 Kinect 深度以及彩色影像資料>.在可以透過 OpenNI 讀取到 Kinect 的深度.色彩資訊之後,其實就可以試著用這些資訊,來重 ...

  4. 获取元素样式 currentStyle 和 getcomputedStyle

    场景 你要获取某一元素的样式,可是没有获取到,返回的值为undefined,可是有时候又能成功? 为什么? 因为,xx.stly.xxx 可以获取的样式信息,是dom元素style属性里的样式,对于通 ...

  5. WEB开发原则

    1.最小权限原则,只允许用户做****,而不是"不允许用户做****"2.浏览器查看的是服务端代码的执行输出的文本,除非服务器有漏洞,否则浏览者无法查看 服务端的ASPX,CS代码 ...

  6. 深入理解Python中的生成器

    生成器(generator)概念 生成器不会把结果保存在一个系列中,而是保存生成器的状态,在每次进行迭代时返回一个值,直到遇到StopIteration异常结束. 生成器语法 生成器表达式: 通列表解 ...

  7. 【原创】Android开发使用华为手机调试logcat没有应用输出信息

    输入 *#*#2846579#*#* 点击project Menu点击后台 1.设置logcat 2. Dump & Log",打开开关"打开Dump & Log& ...

  8. 《APUE》第三章笔记(1)

    以下内容是我看<APUE>第二版第三章的笔记,有错还希望指出来,谢谢. unbuffered I/O,跟buffered I/O相对,buffered I/O就是 ISO C标准下的标准输 ...

  9. js获取json数据

    var json = {  contry:{ area:{ man:"12万",  women:"10万" } } };//方式一:使用eval解析  var  ...

  10. js获取url的get传值函数

    function getvl(name) { var reg = new RegExp("(^|\\?|&)"+ name +"=([^&]*)(\\s| ...