Atlantis
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 16882   Accepted: 6435

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

我累个去~~这题从早上8:30一直做到如今22:50,总算是A掉了,上午一直没看懂扫描线,然后看到傍晚总算是有些头目了,结果写的时候非常生疏,等到摸摸索索完全然全地写完已经大半夜了。满心欢喜地提交又各种RE,開始以为是数组开小了,然后翻了倍依然RE,就这样RE了6、7次后猛地发现是freopen忘了凝视掉,o(╯□╰)o...尼玛啊!!总归是在睡觉前找到问题了。

题意:给定n个矩形的对角坐标,各自是左下和右上,浮点型。求矩形覆盖的面积。

题解:扫描线解法,将每一个矩形分别以长宽为直线切割,终于垂直于x轴的割线有2n条,垂直于y轴的割线也有2n条,将这些割线存到两个数组里并排序,建立一个线段树用以维护扫描线当前在y轴方向覆盖的长度。最后,在扫描线从左往右扫描的过程中,一旦经过一条垂直于x轴的割线就立刻将这条割线更新到线段树里,覆盖面积即为两条相邻扫描线的距离乘以左边一条扫描线的长度,累加下去即得终于覆盖面积。x轴割线数组须要记录边是入边还是出边。若是出边则右边的的面积对此出边是不计的,可是存在入边重合的情况,须要详细考虑。

2014-9-23 23:32:51更新

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define maxn 202
#define lson l, mid, rt << 1
#define rson mid, r, rt << 1 | 1
using namespace std; struct Node {
double y1, y2, len;
int covers;
} T[maxn << 2];
struct Node2 {
double x, y1, y2;
int isLeft;
} xNode[maxn];
double yNode[maxn]; bool cmp(Node2 a, Node2 b) {
return a.x < b.x;
} void pushUp(int l, int r, int rt) {
if(T[rt].covers > 0)
T[rt].len = T[rt].y2 - T[rt].y1;
else if(r - l == 1) T[rt].len = 0.0;
else T[rt].len = T[rt << 1].len + T[rt << 1 | 1].len;
} void build(int l, int r, int rt) {
T[rt].covers = 0;
T[rt].y1 = yNode[l];
T[rt].y2 = yNode[r];
T[rt].len = 0.0;
if(r - l == 1) return;
int mid = (l + r) >> 1;
build(lson);
build(rson);
} void update(Node2 x, int l, int r, int rt) {
if(x.y1 == T[rt].y1 && x.y2 == T[rt].y2) {
T[rt].covers += x.isLeft;
pushUp(l, r, rt);
return;
}
int mid = (l + r) >> 1;
if(x.y2 <= yNode[mid]) update(x, lson);
else if(x.y1 >= yNode[mid]) update(x, rson);
else {
Node2 x1 = x, x2 = x;
x1.y2 = x2.y1 = yNode[mid];
update(x1, lson);
update(x2, rson);
}
pushUp(l, r, rt);
} int main() {
//freopen("stdin.txt", "r", stdin);
int n, i, id, cas = 1;
double x1, y1, x2, y2, sum;
while(scanf("%d", &n), n) {
for(i = id = 0; i < n; ++i) {
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
yNode[id] = y1;
xNode[id].x = x1;
xNode[id].y1 = y1;
xNode[id].y2 = y2;
xNode[id++].isLeft = 1; yNode[id] = y2;
xNode[id].x = x2;
xNode[id].y1 = y1;
xNode[id].y2 = y2;
xNode[id++].isLeft = -1;
}
sort(yNode, yNode + id);
build(0, id - 1, 1);
sort(xNode, xNode + id, cmp);
update(xNode[0], 0, id - 1, 1);
for(i = 1, sum = 0.0; i < id; ++i) {
sum += T[1].len * (xNode[i].x - xNode[i-1].x);
update(xNode[i], 0, id - 1, 1);
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n", cas++, sum);
}
return 0;
}

#include <stdio.h>
#include <algorithm>
#define maxn 202
#define lson l, mid, rt << 1
#define rson mid, r, rt << 1 | 1
using std::sort; struct Node{
double y1, y2, height; //y1, y2记录y坐标离散前的值
int coverTimes;
} tree[maxn << 2]; //区间树
double yArr[maxn]; //垂直于Y轴的割线
struct node{
double x, y1, y2;
int isLeftEdge;
} xArr[maxn]; //垂直于X轴的割线 bool cmp(node a, node b){ return a.x < b.x; } void build(int l, int r, int rt)
{
tree[rt].coverTimes = 0;
tree[rt].height = 0;
tree[rt].y1 = yArr[l];
tree[rt].y2 = yArr[r];
if(r - l == 1) return; int mid = (l + r) >> 1;
build(lson);
build(rson);
} void getSweepLinesHeight(int l, int r, int rt)
{//因为存在线段覆盖的情况,所以长线段结束并不能代表扫描线长度为0
if(tree[rt].coverTimes > 0){
tree[rt].height = tree[rt].y2 - tree[rt].y1;
}else if(r - l == 1){
tree[rt].height = 0;
}else tree[rt].height = tree[rt << 1].height + tree[rt << 1 | 1].height;
} void update(node xNode, int l, int r, int rt)
{
if(xNode.y1 == tree[rt].y1 && xNode.y2 == tree[rt].y2){
tree[rt].coverTimes += xNode.isLeftEdge;
getSweepLinesHeight(l, r, rt);
return;
} //include r - l == 1 int mid = (l + r) >> 1;
if(xNode.y2 <= yArr[mid]) update(xNode, lson);
else if(xNode.y1 >= yArr[mid]) update(xNode, rson);
else{
node temp = xNode;
temp.y2 = yArr[mid];
update(temp, lson);
temp = xNode; temp.y1 =yArr[mid];
update(temp, rson);
} getSweepLinesHeight(l, r, rt); //Attention!
} int main()
{
//freopen("stdin.txt", "r", stdin);
int n, i, cas = 1, id;
double x1, y1, x2, y2, sum;
while(scanf("%d", &n), n){
for(i = id = 0; i < n; ++i){
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
yArr[id] = y1; xArr[id].x = x1;
xArr[id].isLeftEdge = 1; //1表示左。-1表示右
xArr[id].y1 = y1; xArr[id++].y2 = y2; yArr[id] = y2; xArr[id].x = x2;
xArr[id].isLeftEdge = -1;
xArr[id].y1 = y1; xArr[id++].y2 = y2;
} sort(yArr, yArr + id);
sort(xArr, xArr + id, cmp);
build(0, id - 1, 1); update(xArr[0], 0, id - 1, 1);
for(i = 1, sum = 0; i < id; ++i){
sum += tree[1].height * (xArr[i].x - xArr[i - 1].x);
update(xArr[i], 0, id - 1, 1);
} printf("Test case #%d\nTotal explored area: %.2lf\n\n", cas++, sum);
}
return 0;
}

POJ1151 Atlantis 【扫描线】的更多相关文章

  1. poj1151 Atlantis——扫描线+线段树

    题目:http://poj.org/problem?id=1151 经典的扫描线问题: 可以用线段树的每个点代表横向被矩形上下边分割开的每一格,这样将一个矩形的出现或消失化为线段树上的单点修改: 每个 ...

  2. [POJ1151]Atlantis

    [POJ1151]Atlantis 试题描述 There are several ancient Greek texts that contain descriptions of the fabled ...

  3. ACM学习历程—POJ1151 Atlantis(扫描线 && 线段树)

    Description There are several ancient Greek texts that contain descriptions of the fabled island Atl ...

  4. poj1151 Atlantis (线段树+扫描线+离散化)

    有点难,扫描线易懂,离散化然后线段树处理有点不太好理解. 因为这里是一个区间,所有在线段树中更新时,必须是一个长度大于1的区间才是有效的,比如[l,l]这是一根线段,而不是区间了. AC代码 #inc ...

  5. POJ1151 Atlantis 线段树扫描线

    扫描线终于看懂了...咕咕了快三个月$qwq$ 对于所有的横线按纵坐标排序,矩阵靠下的线权值设为$1$,靠上的线权值设为$-1$,然后执行线段树区间加减,每次的贡献就是有效宽度乘上两次计算时的纵坐标之 ...

  6. POJ 1151 HDU 1542 Atlantis(扫描线)

    题目大意就是:去一个地方探险,然后给你一些地图描写叙述这个地方,每一个描写叙述是一个矩形的右下角和左上角.地图有些地方是重叠的.所以让你求出被描写叙述的地方的总面积. 扫描线的第一道题,想了又想,啸爷 ...

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

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

  8. POJ-1151 Atlantis 矩形面积并

    题目链接:http://poj.org/problem?id=1151 扫描线+离散+线段树,线段树每个节点保存的是离散后节点右边的线段. //STATUS:C++_AC_16MS_208KB #in ...

  9. POJ1151 Atlantis 水题 计算几何

    http://poj.org/problem?id=1151 想学一下扫描线线段树,结果写了道水题. #include<iostream> #include<cstdio> # ...

随机推荐

  1. 2-bitmap

    在2.5亿个整数中找出不重复的整数,注,内存不足以容纳这2.5亿个整数. 思路: bitmap用一个bit来代表存在还是不存在,现在我们要判断重不重复,则需要三个状态:不存在,存在一个,存在多个.2b ...

  2. [SGU 199] Beautiful People

    [SGU 199] Beautiful People The most prestigious sports club in one city has exactly N members. Each ...

  3. django-2的路由层(URLconf)

    URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于客户端发来的某个URL调用哪一段逻辑代码 ...

  4. Java 基本的递归写法

    1.首先我们得有一个树状结构的表,类似这种结构.必须得有 id,pid  其他的根据需要来. 我们叫它treeTbl表吧.这里pid为0的表示是顶级节点. 2.接着select * from tree ...

  5. C#调用Java的WebService出现500 服务器错误

    最近在用C#调用Java写的WebService时,发现老是返回500 服务器错误,到底什么原因一直找不出来, 后来google了以后,找到国外的http://stackoverflow.com站点已 ...

  6. JavaOO知识点小结一

    Java语言的特点是什么?简单 面向对象 跨平台 多线程 健壮性安全性 垃圾回收机制如何编译和执行java文件?产生帮助文档用什么命令?编译: javac 文件名执行: java 类名产生帮助文档 j ...

  7. 在 Laravel 应用中使用 pjax 进行页面加速

    说明# PHPHub 使用 pjax 来加速网页的加载, 这篇文章是在开发完此功能后做的笔记. 什么是 Pjax# .--. / \ ## a a ( '._) |'-- | _.\___/_ ___ ...

  8. Appium Android 获取包名和 Activity 的几种方法 (转)

    本文档主要记录“获取包名和 Activity 的方法”,用于自动化测试时启动APP.以下方法主要来源于网络和社区同学的贡献,特此感谢! 1. 方法一: pm list package查看包名 adb ...

  9. appium分层自动化的封装

    1.创建一个case包,start_app的python文件 #coding=utf-8from appium import webdriverfrom util.read_init import R ...

  10. 通过分析反汇编还原 C 语言 if…else 结构

    让我们从反汇编的角度去分析并还原 C 语言的 if - else 结构,首先我们不看源代码,我们用 OllyDBG 载入 PE 文件,定位到 main 函数领空,如下图所示. 在图示中,我已经做好了关 ...