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

题目就是求所有矩形的并面积。

通过查阅知道了是扫描线,了解了扫描线的原理,用线段树手写了一下,结果PushUp函数写搓了。。看了AC的代码才知道了原因。

做法就是通过对纵坐标有序化,然后创建区间。

然后通过横向扫描过去,得到每段横向段的高度,乘以宽度就是面积了。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define LL long long using namespace std; //线段树
//扫描线
const int maxn = 205;
struct node
{
int lt, rt;
double height;
int num;
}tree[4*maxn]; struct Line
{
double x;
double y1, y2;
bool isLeft;
}line[maxn]; bool cmp(Line a, Line b)
{
return a.x < b.x;
} double y[maxn]; //向上更新
void PushUp(int id)
{
if(tree[id].num > 0)
{
tree[id].height = y[tree[id].rt] - y[tree[id].lt];
return;
}
if(tree[id].lt+1 == tree[id].rt)
tree[id].height = 0;
else
tree[id].height = tree[id<<1].height + tree[id<<1|1].height;
} //建立线段树
void Build(int lt, int rt, int id)
{
tree[id].lt = lt;
tree[id].rt = rt;
tree[id].height = 0;//每段的初值,根据题目要求
tree[id].num = 0;
if (lt+1 == rt)
{
//tree[id].val = 1;
return;
}
int mid = (lt + rt) >> 1;
Build(lt, mid, id<<1);
Build(mid, rt, id<<1|1);
//PushUp(id);
} //寻找符合修改的区间通过判断num进行修改
void Updata(int id,Line p)
{
if(p.y1 <= y[tree[id].lt] && p.y2 >= y[tree[id].rt])
{
if (p.isLeft > 0)
tree[id].num++;
else
tree[id].num--;
PushUp(id);
return;
}
int mid = (tree[id].lt+tree[id].rt) >> 1;
if (p.y1 < y[mid])
Updata(id<<1, p);
if (p.y2 > y[mid])
Updata(id<<1|1, p);
PushUp(id);
} int n; void Input()
{
double x1, y1, x2, y2;
int cnt = 1;
for (int i = 0; i < n; ++i)
{
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
y[cnt] = y1;
y[cnt+1] = y2; line[cnt].x = x1;
line[cnt].y1 = y1;
line[cnt].y2 = y2;
line[cnt].isLeft = true; line[cnt+1].x = x2;
line[cnt+1].y1 = y1;
line[cnt+1].y2 = y2;
line[cnt+1].isLeft = false;
cnt += 2;
}
sort(y+1, y+1+2*n);
sort(line+1, line+1+2*n, cmp);
Build(1, 2*n, 1);
} double Work()
{
double ans = 0;
Updata(1, line[1]);
int len = 2*n;
for (int i = 2; i <= len; ++i)
{
ans += (line[i].x-line[i-1].x) * tree[1].height;
Updata(1, line[i]);
}
return ans;
} int main()
{
//freopen("test.in", "r", stdin);
int times = 1;
while (scanf("%d", &n) != EOF && n)
{
Input();
double ans = Work();
printf("Test case #%d\n", times);
printf("Total explored area: %.2lf\n\n", ans);
times++;
}
return 0;
}

ACM学习历程—POJ1151 Atlantis(扫描线 && 线段树)的更多相关文章

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

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

  2. ACM学习历程—HDU 5289 Assignment(线段树 || RMQ || 单调队列)

    Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fro ...

  3. ACM学习历程—HDU 2795 Billboard(线段树)

    Description At the entrance to the university, there is a huge rectangular billboard of size h*w (h ...

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

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

  5. ACM学习笔记:可持久化线段树

    title : 可持久化线段树 date : 2021-8-18 tags : 数据结构,ACM 可持久化线段树 可以用来解决线段树存储历史状态的问题. 我们在进行单点修改后,线段树只有logn个(一 ...

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

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

  7. [HDU1542]Atlantis(扫描线+线段树)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  8. 【POJ1151】Atlantis(线段树,扫描线)

    [POJ1151]Atlantis(线段树,扫描线) 题面 Vjudge 题解 学一学扫描线 其实很简单啦 这道题目要求的就是若干矩形的面积和 把扫描线平行于某个轴扫过去(我选的平行\(y\)轴扫) ...

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

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

随机推荐

  1. iPhone换电池是原装电池好还是换第三方大容量电池好?

    转:https://www.xianjichina.com/news/details_60791.html 最近这段时间苹果降速门事件持续发酵,闹得满城风雨.尽管苹果公司两次致歉,很多果粉都去更换电池 ...

  2. 模拟IE各种版本的方法

    下载360极速浏览器.开启“兼容模式” 默认会是IE7.可以通过控制台(Ctrl + shift + I)调整各个版本

  3. iOS开发人员程序许可协议

    请细致阅读以下的许可协议条款和条件之前下载或使用苹果软件.   这些条款和条件构成你和苹果之间的法律协议.   iOS开发人员程序许可协议   目的 你想使用苹果软件(例如以下定义)来开发一个或多个应 ...

  4. Java源代码之LinkedHashMap

    Java源代码之LinkedHashMap 转载请注明出处:http://blog.csdn.net/itismelzp/article/details/50554412 一.LinkedHashMa ...

  5. Codeforces Round #316 (Div. 2) (ABC题)

    A - Elections 题意: 每一场城市选举的结果,第一关键字是票数(降序),第二关键字是序号(升序),第一位获得胜利. 最后的选举结果,第一关键字是获胜城市数(降序),第二关键字是序号(升序) ...

  6. 跳过权限检查,强制修改mysql密码

    windows: 1,停止MYSQL服务,CMD打开DOS窗口,输入 net stop mysql 2,在CMD命令行窗口,进入MYSQL安装目录 比如E:\Program Files\MySQL\M ...

  7. VMware 报错“Intel VT-x处于禁止状态”

    VMware Workstation 10虚拟机安装64位windows server 2008 R2系统时报错“Intel VT-x处于禁止状态”,如下图.   工具/原料   VMware Wor ...

  8. 【题解】 P5021赛道修建

    [题解]P5021 赛道修建 二分加贪心,轻松拿省一(我没有QAQ) 题干有提示: 输出格式: 输出共一行,包含一个整数,表示长度最小的赛道长度的最大值. 注意到没,最小的最大值,还要多明显? 那么我 ...

  9. Android之Handler使用方法总结

    方法一:(java习惯,在android平台开发时这样是不行的,由于它违背了单线程模型) 刚刚開始接触android线程编程的时候,习惯好像java一样,试图用以下的代码解决这个问题    new T ...

  10. Android Development Note-01

    Eclipse快捷键: 导包:ctrl+alt+o 格式化代码:ctrl+alt+f   MVC: M——Model V——View C——Control   android程序界面如何设计.调试 U ...