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. iOS支付宝集成步骤;王刚韧的技术博客

  2. 享元模式(咖啡屋)【java与模式】

    package com.javapatterns.flyweight.coffeeshop; public class Flavor extends Order { private String fl ...

  3. C++ GUI Programming with Qt4 笔记 -- chap1

    1. Hello Qt #include <QApplication> #include <QLabel> int main(int argc, char *argv[]){ ...

  4. javaIo流实际应用

    /*查看目录下所有的文件*/ package cn.file; import java.io.File; public class Text2 { public static void main(St ...

  5. clr介绍

    CLR(公用语言运行时)和Java虚拟机一样也是一个运行时环境,它负责资源管理(内存分配和垃圾收集),并保证应用和底层操作系统之间必要的分离..NET提供了一个运行时环境,叫做公用语言运行时(Comm ...

  6. Android LayoutInflater.inflate使用上的问题解惑

    最近在在使用LayoutInflater.inflate方法时遇到了一些问题,以前没有仔细看过此类的使用方法,故将其记录下来,方便日后查阅. 相信大家都知道LayoutInflater.inflate ...

  7. JavaScript实现url地址自动检测并添加URL链接示例代码

    写一个简单的聊天系统,发出Htpp的Url实现跳转加上a标签,下面是具体的实现,感兴趣的朋友不要错过 背景:写一个简单的聊天系统,发出Htpp的Url实现跳转加上a标签.  实现代码: 复制代码代码如 ...

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

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

  9. XML3_XML元素和节点的具体解释

    就像一个树状的目录.可以把第一行当作它扎根的“土地”.XML文件是由节点构成的.它的第一个节点为“根节点”.一个XML文件必须有且只能有一 个根节点,其他节点都必须是它的子节点.我们在FLASH里使用 ...

  10. C++中弱符号(弱引用)的意义及实例

    今天读别人代码时看到一个“#pragma weak”,一时没明白,上网研究了一个下午终于稍微了解了一点C.C++中的“弱符号”,下面是我的理解,不正确的地方望大家指正. 本文主要从下面三个方面讲“弱符 ...