Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15236    Accepted Submission(s): 6265

Problem 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 file 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
 
Recommend
linle
题目大意:求矩形并的面积.
分析:比较固定的解法:线段树+扫描线.矩形的面积公式是底乘高,每次把相同y坐标的底的长度给加起来,同时向上跳,直到遇到另外一条线位置,那么底的和*高就是扩展得到的面积,这样不断地扩展就能得到答案.具体算法可以参看:传送门
          直接套线段树会有一个问题,[x,x]在这道题中表示的是一个点,是不合法的,而线段树中确是有意义的,如果处理到[x,x+1]继续分治成[x,x],[x+1,x+1]就会出现问题,正确的做法是每次将右端点往左移一位,特判如果此时的l=r,那么就是之前的[x,x+1]这种情况,直接判掉,否则就可以像线段树那样正常地递归下去.
          这道题因为坐标比较大,需要离散化一下,因为高度是需要计算的,所以只需要离散横坐标就可以了.那么线段树记录的就是下标,每次修改一个值都必须要二分查找这个值对应的下标是哪一位.
          比较坑的一个点是数据可能会有0,我在处理的时候没考虑到这种情况,一直爆栈,主要还是离散化的部分鲁棒性不强.
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = ; struct node
{
double l, r, h;
int id;
}e[maxn]; double X[maxn], c[maxn << ], XX[maxn], ans;
int cnt, n, tot, tag[maxn << ], cas = ; bool cmp(node a, node b)
{
return a.h < b.h;
} int find(double x)
{ int l = , r = cnt, res = cnt - ;
while (l <= r)
{
int mid = (l + r) >> ;
if (X[mid] >= x)
{
res = mid;
r = mid - ;
}
else
l = mid + ;
}
return res;
} void pushup(int o, int l, int r)
{
if (tag[o])
c[o] = X[r + ] - X[l];
else
if (l == r) //没有被完全覆盖就是两个点
c[o] = ;
else
c[o] = c[o * ] + c[o * + ];
} void update(int o, int l, int r, int x, int y, int v)
{
if (x <= l && r <= y)
{
tag[o] += v;
pushup(o, l, r);
return;
}
int mid = (l + r) >> ;
if (x <= mid)
update(o * , l, mid, x, y, v);
if (y > mid)
update(o * + , mid + , r, x, y, v);
pushup(o, l, r);
} int main()
{
while (~scanf("%d", &n) , n)
{
memset(c, , sizeof(c));
memset(tag, , sizeof(tag));
ans = 0.0;
tot = cnt = ;
for (int i = ; i <= n; i++)
{
double a, b, c, d;
scanf("%lf%lf%lf%lf", &a, &b, &c, &d);
e[++tot].l = a;
e[tot].r = c;
e[tot].h = b;
e[tot].id = ;
X[tot] = a;
e[++tot].l = a;
e[tot].r = c;
e[tot].h = d;
e[tot].id = -;
X[tot] = c;
}
sort(X + , X + tot + );
sort(e + , e + + tot, cmp);
XX[] = X[];
cnt = ;
for (int i = ; i <= tot; i++)
if (X[i] != X[i - ])
XX[++cnt] = X[i];
memcpy(X, XX, sizeof(XX));
for (int i = ; i < tot; i++)
{
int l = find(e[i].l), r = find(e[i].r) - ;
update(, , cnt, l, r, e[i].id);
ans += c[] * (e[i + ].h - e[i].h);
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n", cas++, ans);
} return ;
}

Hdu1542 Atlantis的更多相关文章

  1. hdu1542 Atlantis(矩阵面积的并)

    这个题算是我的第一个扫描线的题,扫描线算是一种思想吧,用到线段树+离散化.感觉高大上. 主要参考了这位大神的博客. http://www.cnblogs.com/kuangbin/archive/20 ...

  2. HDU1542 Atlantis —— 求矩形面积并 线段树 + 扫描线 + 离散化

    题目链接:https://vjudge.net/problem/HDU-1542 There are several ancient Greek texts that contain descript ...

  3. hdu1542 Atlantis 线段树--扫描线求面积并

    There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...

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

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

  5. hdu1542 Atlantis (线段树+矩阵面积并+离散化)

    There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...

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

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

  7. hdu-1542 Atlantis(离散化+线段树+扫描线算法)

    题目链接: Atlantis Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/32768 K (Java/Others) ...

  8. HDU-1542 Atlantis(离散化+扫描线)

    题目大意:给n个矩形,可能重叠,求面积. 题目分析:线段树维护扫描线. 代码如下: # include<bits/stdc++.h> using namespace std; # defi ...

  9. HDU1542 Atlantis(矩形面积并)

    #pragma warning (disable:4996) #include<iostream> #include<cstring> #include<string&g ...

随机推荐

  1. MySql面试题(持续更新)

    1. 左连接,右连接,内连接的概念. 左连接:以左表为主,保留左表的所有数据,并且依次拿每行数据去匹配右表所有行,如果没匹配的,右边表的数据为null. 右连接:以右表为主,保留右表的所有数据,并且依 ...

  2. Python3实现机器学习经典算法(三)ID3决策树

    一.ID3决策树概述 ID3决策树是另一种非常重要的用来处理分类问题的结构,它形似一个嵌套N层的IF…ELSE结构,但是它的判断标准不再是一个关系表达式,而是对应的模块的信息增益.它通过信息增益的大小 ...

  3. 用vsstudio 设计Winform 高分屏上布局错乱的问题

    在使用win10高分辨率150%,200%系统进行winform开发时, 会有布局错乱的现象,比如之前定义的300px的宽度,往往被设置成600px (200%分辨率下). 这个问题vs2015的解决 ...

  4. Python学习小目录汇总

    python其他知识目录 python基础知识-1 1.typora软件使用 2.python解释器安装 3.Python解释器环境变量添加 4.计算机编码知识: 5.输出print(): 6.变量 ...

  5. 大前端全栈CSS3移动端开发

    作者声明:本博客中所写的文章,都是博主自学过程的笔记,参考了很多的学习资料,学习资料和笔记会注明出处,所有的内容都以交流学习为主.有不正确的地方,欢迎批评指正 本节课学习视频来源:https://ww ...

  6. Visiting a Friend(思维)

    Description Pig is visiting a friend. Pig's house is located at point 0, and his friend's house is l ...

  7. web压力测试_(收集)

    作者:ZeldaZzz链接:http://www.zhihu.com/question/19867883/answer/89775858来源:知乎著作权归作者所有,转载请联系作者获得授权. 一个完整的 ...

  8. Windows Forms编程实战学习:第三章 菜单

    第三章 菜单 1,控件和容器 所有的Windows Forms控件都是从System.Windows.Forms.Control类继承的,相关类的层次结构如下图所示: MarshalByRefObje ...

  9. prototype.js中Function.prototype.bind方法浅解

    prototype.js中的Function.prototype.bind方法: Function.prototype.bind = function() { var __method = this; ...

  10. Spring Boot(八)集成Spring Cache 和 Redis

    在Spring Boot中添加spring-boot-starter-data-redis依赖: <dependency> <groupId>org.springframewo ...