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. uC/OS-II学习历程(持续更新)

    开始接触嵌入式操作系统的知识了,作为入门,选择了一个小巧的系统——uC/OS-II.当然,难度也并不小,至少对我来说是这样.刚刚开始看,使用的参考书是任哲的<嵌入式实时操作系统uC/OS-II原 ...

  2. mac下的改装人生——mbp拆卸的各种资源整理

    这几天弄了好多的mac上硬件的问题,我自己的mac也被我拆了3次,感觉自己终于跟硬件扯上关系了哈. 在这里放一些我找到比较好的资源教程,供大家学习,如果我以后还需要继续拆我的macbook,我也会回来 ...

  3. mac上的键盘生活——打字训练

    我的打字真的很有问题,错误率实在是太高了,于是乎下定决心改掉打字习惯,起码为了对得起我的机械键盘   所谓双击键(shift)就是当一个键钮上有两个字符时的辅助选择键,像标注在数字1上的 !,就是在你 ...

  4. Apache-POI操作Excel的一些小技巧

    Apache-POI操作Excel将合并后的单元格全部填充为相同数据的一个实例. private static void fillMergedRegion(final Sheet sheet) { f ...

  5. CENTOS 7 开放端口设置

    CentOS 7 默认没有使用iptables,所以通过编辑iptables的配置文件来开启80端口是不可以的 CentOS 7 采用了 firewalld 防火墙 如要查询是否开启80端口则: [r ...

  6. jQuery选择器总结 转

    jQuery选择器总结 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3 ...

  7. 【基础】多线程更新窗体UI的若干方法

    一.前言 在单线程中设置窗体某个控件的值很简单的事,只需要设置控件文本的值就可以了,但是有的业务场景很是复杂,界面上的控件也很多,这种情况下当数据量比较多的时候,在单线程中更新UI不可避免地会发生假死 ...

  8. 原生JavaScript的省市县三级联动

    三级联动是我们写表单时必不可少的,比如在写收货地址时,就用到他了,最近在看原生JavaScript,从基础写起,待完善,以后再写个jquery版的. <!DOCTYPE html> < ...

  9. JAVA wait(), notify(),sleep详解

    转自: http://blog.csdn.net/zyplus 在JAVA中,是没有类似于PV操作.进程互斥等相关的方法的.JAVA的进程同步是通过synchronized()来实现的,需要说明的是, ...

  10. tl;drLegal ——开源软件license的搜索引擎

    TLDRLegal - Open Source Licenses Explained in Plain English可以很方便查询各个开源license的总结(能做什么,不能做什么),还能比较不同的 ...