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

【分析】
扫描线的基本应用,离散化一下按横坐标从左到右扫就行了。
 /*
元稹
《离思五首·其四》 曾经沧海难为水,除却巫山不是云。
取次花丛懒回顾,半缘修道半缘君。
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map>
#include <ctime>
#include <cstdlib>
#include <stack>
#define LOCAL
const int MAXN = + ;
const int MAXM = + ;
const int INF = ;
const int SIZE = ;
const int maxnode = 0x7fffffff + ;
using namespace std;
struct Line{//扫描线
int flag;//表示是出线还是入线
double x, y1, y2; Line (double a, double b, double c, double d){
flag = (int)d;
x = a;
y1 = b;
y2 = c;
}
bool operator < (const Line &b)const{
return x < b.x;
};
};
struct Seg_Tree{
struct Node{
int l, r, flag;
double ll, rr, len;
}tree[];
vector<double>y, data;
vector<Line>line;
map<double, int>Map;//离散化 void update(int t, int s, int e, int val){
int l = tree[t].l, r = tree[t].r;
if ((l + ) == r) {
tree[t].flag += val;
if (tree[t].flag == ) tree[t].len = ;
else tree[t].len = tree[t].rr - tree[t].ll;
}
else{
int mid = (l + r)>>;
if (s < mid) update(t << , s, e, val);
if (e > mid) update((t << ) | , s, e, val);
tree[t].len = tree[t << ].len + tree[(t << ) | ].len; }
}
void build(int t, int l, int r){
tree[t].l = l;
tree[t].r = r;
tree[t].flag = ;
tree[t].len = ;
//这两个是代表真实的值
tree[t].ll = y[l];
tree[t].rr = y[r];
if ((l + ) == r) return;
//左闭右开
int mid = (l + r)>>;
build((t<<), l, mid);
build((t<<) | , mid, r);
}
void init(){
Map.clear();;
line.clear();
y.clear();
data.clear();
}
}A;
int n; void init(){
A.init();
for (int i = ; i <= n; i++){
double x1, x2, y1, y2;
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
A.line.push_back(Line(x1, y1, y2, ));//1代表入线
A.line.push_back(Line(x2, y1, y2, -));
A.data.push_back(y1);
A.data.push_back(y2);
}
int cnt = ;//离散化
sort(A.data.begin(), A.data.end());
for (int i = ; i < A.data.size(); i++){
if (i == || A.data[i] != A.data[i - ]){
A.Map[A.data[i]] = cnt++;
A.y.push_back(A.data[i]);
}
}
}
void work(){
sort(A.line.begin(), A.line.end());
A.build(, , A.y.size() - );
double Ans = ;
for (int i = ; i < A.line.size(); i++){
if (i != ) Ans += (A.line[i].x - A.line[i - ].x) * A.tree[].len;
A.update(, A.Map[A.line[i].y1], A.Map[A.line[i].y2], A.line[i].flag);
}
printf("Total explored area: %.2lf\n\n" , Ans);
} int main(){ int t = ;
while (scanf("%d", &n) && n){
printf("Test case #%d\n", ++t);
init();
work();
}
return ;
}

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

  1. hdu1542 Atlantis(扫描线+线段树+离散)矩形相交面积

    题目链接:点击打开链接 题目描写叙述:给定一些矩形,求这些矩形的总面积.假设有重叠.仅仅算一次 解题思路:扫描线+线段树+离散(代码从上往下扫描) 代码: #include<cstdio> ...

  2. HDU 3642 - Get The Treasury - [加强版扫描线+线段树]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3642 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  3. 【BZOJ3958】[WF2011]Mummy Madness 二分+扫描线+线段树

    [BZOJ3958][WF2011]Mummy Madness Description 在2011年ACM-ICPC World Finals上的一次游览中,你碰到了一个埃及古墓. 不幸的是,你打开了 ...

  4. HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)

    Description Ted has a new house with a huge window. In this big summer, Ted decides to decorate the ...

  5. 【bzoj4491】我也不知道题目名字是什么 离线扫描线+线段树

    题目描述 给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串 输入 第一行n,表示A数组有多少元素接下来一行为n个整数A[i]接下来一个整数Q,表示询问数 ...

  6. P3722 [AH2017/HNOI2017]影魔(单调栈+扫描线+线段树)

    题面传送门 首先我们把这两个贡献翻译成人话: 区间 \([l,r]\) 产生 \(p_1\) 的贡献当且仅当 \(a_l,a_r\) 分别为区间 \([l,r]\) 的最大值和次大值. 区间 \([l ...

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

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

  8. Poj1151&HDU1542 Atlantis(扫描线+线段树)

    题意 给定\(n​\)个矩形\((x_1,y_1,x_2,y_2)​\),求这\(n​\)个矩形的面积并 题解 扫描线裸题,可以不用线段树维护,\(O(n^2)\)是允许的. #include < ...

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

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

  10. 矩形面积并-扫描线 线段树 离散化 模板-poj1151 hdu1542

    今天刚看到这个模板我是懵逼的,这个线段树既没有建树,也没有查询,只有一个update,而且区间成段更新也没有lazy标记....研究了一下午,我突然我发现我以前根本不懂扫描线,之所以没有lazy标记, ...

随机推荐

  1. 【转】Android ProgressDialog的使用

    原文网址:http://blog.csdn.net/sjf0115/article/details/7255280 版权声明:本文为博主原创文章,未经博主允许不得转载. <1>简介 Pro ...

  2. org.springframework.web.context.ContextLoaderListener 转

    ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在web ...

  3. Delphi 编写的Web Service

      一编写服务程序 第一步:File----->New----->Other------>WebServices----->Soap Server Application选择I ...

  4. Hibernate(五)一对一单向关联映射

    上次的博文中 Hibernate从入门到精通(四)基本映射我们已经讲解了一下基本映射和相关概念,接下来 我们会讲稍微复杂点的映射——关系映射. 关系映射分类 关系映射即在基本映射的基础上处理 多个相关 ...

  5. EasyWebServer编写CGI程序的环境变量

    示例: SERVER_SOFTWARE=EasyWebServer/1.9 SERVER_PROTOCOL=HTTP/1.1 SERVER_PORT= SERVER_NAME=aozima-noteb ...

  6. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  7. 简单的LR核心项集和Goto表填充演示程序

    /* * 该程序用于计算语言的核心项集 * RexfieldVon * 2013年8月24日21:19:25 */ #include <stdio.h> #include <stdl ...

  8. Python:urllib和urllib2的区别(转)

    原文链接:http://www.cnblogs.com/yuxc/ 作为一个Python菜鸟,之前一直懵懂于urllib和urllib2,以为2是1的升级版.今天看到老外写的一篇<Python: ...

  9. SQL Server 2008 常见异常收集(持续更新)

    写在前面: 最近,在使用SQL Server 2008时,出现了不少问题.发现,很多问题都是以前碰见过的,并且当时也寻找到了解决方法(绝大部分来源于“百度”与“Google”),只是时间一长,又忘记了 ...

  10. [MODx] Solve cannot upload large file

    If you also run into this problem, dont' worry, here is the solution for you. First: In Modx, go &qu ...