HDU1542--Atlantis(扫描线)
给N个矩形的端点坐标,求矩形覆盖面积和。
原理很简单,从左到右扫描,线段树记录的是纵向覆盖的长度。区间更新。因为坐标是实数而且很大,所以需要离散化。
WA+RE+CE+MLE+。。。一共错了二十多次。用了最蠢的办法,最后发现错在初始化的时候,构造函数参数我写成了int。。蠢哭。。。
和普通的线段树是不同的,因为此类题型中矩形左右两竖边的长度时相同的,而且每次都是先加一在减一,不会出现负数。而且最后用到的只有总长度。
AC代码:
#include <bits/stdc++.h>
#define clr(x,c) memset(x,c,sizeof(x)) using namespace std;
const int N = 20005; struct ScanLine {
double x;
double upY, downY;
int flag; // 入边1 出边-1
bool operator<(const ScanLine a) const {
return x < a.x;
}
ScanLine() {}
ScanLine(double x, double y1, double y2, int f) : x(x), upY(y1), downY(y2), flag(f) {}
} line[N]; double tr[N];
int cover[N];
double yy[N]; #define lson (o<<1)
#define rson (o<<1|1)
#define mid (l+r>>1)
int yl, yr, v;
void pushup(int o, int l, int r)
{
if (cover[o]) tr[o] = yy[r] - yy[l];
else if (l + 1 == r) tr[o] = 0; // 叶子
else tr[o] = tr[lson] + tr[rson];
} void update(int o, int l, int r)
{
if (yl > r || yr < l) return ;
if (yl <= l && yr >= r) {
cover[o] += v;
pushup(o, l, r);
return ;
}
if (l + 1 == r) return ; // 不包含的叶子节点要退出,否则死循环T^T
if (yl <= mid) update(lson, l, mid);
if (yr > mid) update(rson, mid, r); // 注意这里不是mid+1 因为mid~mid+1一段的距离也要算
pushup(o, l ,r);
} int main()
{
int n;
int cas = 0;
while (~scanf("%d", &n) && n) {
int cnt = 0;
double x1, y1, x2, y2;
for (int i = 0; i < n; ++i) {
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
line[++cnt] = ScanLine(x1, y2, y1, 1);
yy[cnt] = y1;
line[++cnt] = ScanLine(x2, y2, y1, -1);
yy[cnt] = y2;
}
sort(yy + 1, yy + cnt + 1);
sort(line + 1, line + cnt + 1);
int len = unique(yy + 1, yy + cnt + 1) - yy - 1;
clr(cover, 0);
clr(tr, 0);
double ans = 0;
for (int i = 1; i <= cnt; ++i) {
ans += tr[1] * (line[i].x - line[i - 1].x);
yl = lower_bound(yy+1, yy + len + 1, line[i].downY) - yy;
yr = lower_bound(yy+1, yy + len + 1, line[i].upY) - yy;
v = line[i].flag;
update(1, 1, len);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n", ++cas, ans);
}
return 0;
}
还有一种方法,比较好理解,和平时的线段树比较像,就是对于每个区间求[l, r-1],算的时候右边加一,这样递归的时候就不用考虑叶子节点了。
#include <bits/stdc++.h>
#define clr(x,c) memset(x,c,sizeof(x))
using namespace std;
const int N = 20005; struct ScanLine {
double x;
double upY, downY;
int flag; // 入边1 出边-1
bool operator<(const ScanLine a) const {
return x < a.x;
}
ScanLine() {}
ScanLine(double x, double y1, double y2, int f) : x(x), upY(y1), downY(y2), flag(f) {}
} line[N]; double tr[N];
int cover[N];
double yy[N]; #define lson (o<<1)
#define rson (o<<1|1)
#define mid ((l+r)>>1)
int yl, yr, v; void pushup(int o, int l, int r)
{
if (cover[o] > 0) tr[o] = yy[r+1] - yy[l];
else if (l == r) tr[o] = 0;
else tr[o] = tr[lson] + tr[rson];
} void update(int o, int l, int r)
{
if (yl > r || yr < l) return ;
if (yl <= l && yr >= r) {
cover[o] += v;
pushup(o, l, r);
return ;
}
if (yl <= mid) update(lson, l, mid);
if (yr > mid) update(rson, mid + 1, r);
pushup(o, l ,r);
} int main()
{
int n;
int cas = 0;
while (~scanf("%d", &n) && n) {
int cnt = 0;
double x1, y1, x2, y2;
for (int i = 0; i < n; ++i) {
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
line[++cnt] = ScanLine(x1, y2, y1, 1);
yy[cnt] = y1;
line[++cnt] = ScanLine(x2, y2, y1, -1);
yy[cnt] = y2;
}
sort(yy + 1, yy + cnt + 1);
sort(line + 1, line + cnt + 1);
int len = unique(yy + 1, yy + cnt + 1) - yy - 1;
clr(cover, 0);
clr(tr, 0);
double ans = 0;
for (int i = 1; i <= cnt; ++i) {
ans += tr[1] * (line[i].x - line[i - 1].x);
yl = lower_bound(yy+1, yy+len+1, line[i].downY) - yy;
yr = lower_bound(yy+1, yy+len+1, line[i].upY) - yy - 1;
v = line[i].flag;
update(1, 1, len-1);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n", ++cas, ans);
}
return 0;
}
HDU1542--Atlantis(扫描线)的更多相关文章
- [HDU1542]Atlantis(扫描线+线段树)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- hdu1542 Atlantis(矩阵面积的并)
这个题算是我的第一个扫描线的题,扫描线算是一种思想吧,用到线段树+离散化.感觉高大上. 主要参考了这位大神的博客. http://www.cnblogs.com/kuangbin/archive/20 ...
- hdu1542 Atlantis 线段树--扫描线求面积并
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...
- hdu1542 Atlantis (线段树+扫描线+离散化)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU1542 Atlantis —— 求矩形面积并 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/HDU-1542 There are several ancient Greek texts that contain descript ...
- HDU-1542 Atlantis(离散化+扫描线)
题目大意:给n个矩形,可能重叠,求面积. 题目分析:线段树维护扫描线. 代码如下: # include<bits/stdc++.h> using namespace std; # defi ...
- Poj1151&HDU1542 Atlantis(扫描线+线段树)
题意 给定\(n\)个矩形\((x_1,y_1,x_2,y_2)\),求这\(n\)个矩形的面积并 题解 扫描线裸题,可以不用线段树维护,\(O(n^2)\)是允许的. #include < ...
- hdu1542 Atlantis(扫描线+线段树+离散)矩形相交面积
题目链接:点击打开链接 题目描写叙述:给定一些矩形,求这些矩形的总面积.假设有重叠.仅仅算一次 解题思路:扫描线+线段树+离散(代码从上往下扫描) 代码: #include<cstdio> ...
- [POJ1151][HDU1542]Atlantis(线段树,扫描线)
英文题面,我就只放个传送门了. Solution 题意是算矩形面积并,这是扫描线算法能解决的经典问题. 算法的大致思想是,把每一个矩形拆成上边和下边(以下称作扫描线),每条扫描线有四个参数l,r,h ...
- hdu1542 Atlantis (线段树+矩阵面积并+离散化)
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...
随机推荐
- css helper class
应该习惯的css helper class .text-centered text-align: center; .text-right text-align: right; .small small ...
- 【@Transactional】Spring 之注解事务 @Transactional
spring 事务注解 默认遇到throw new RuntimeException("...");会回滚 需要捕获的throw new Exception("...&q ...
- python xlrd,xlwt 读写excel文件
python 读excel文件,需要xlrd库.下载地址:https://pypi.python.org/pypi/xlrd python 写excel文件,需要xlwt库.下载地址:https:// ...
- 在安装MySQL Workbentch的时候出现如下问题,已经解决。
mysql workbench cannot be executed from a path that contains non-ASCII characters. this problem is i ...
- Google考虑抛弃Cookies机制
根据华尔街日报的报道,Google 正在考虑抛弃古老的浏览器 cookies 来追踪用户信息的机制.作为替代,Google 将开发一种「个人匿名标识机制」.Google 早前已经计划在 IE 和 iP ...
- 1920-Jangbi的Rush
描述 最后一届的OSL决赛由神族的Jangbi对阵人族Fantasy.Jangbi5BG爆叉叉准备一波rush,但是范特西早有防备,在地图上埋下了许多地雷.但是Jangbi显然不是毕姥爷那样的无脑平A ...
- [Gauss]POJ2947 Widget Factory
题意: 有n种小工具要加工,每种工具的加工时间为3到9天,给了m条加工记录. 每条记录 X $s_1$ $s_2$ 分别代表 这个工人在$s_1$到$s_2$(前闭后闭)的时间里加工了X件小工具 ...
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-004- 处理上传文件
一.用 MultipartFile 1.在html中设置<form enctype="multipart/form-data">及<input type=&quo ...
- Type-C设计上的防护
Type C设计上各家芯片公司都提供了很多方案,但在防护方面很多留给了客户自己选择,这方面我可以重点聊聊,说起防护,无非就是过压过流防护. 过压防护,Type C的信号线有很多,都需要做静电防护,US ...
- VC2008下CRichEditView加载RichEdit4.1版本(还有一些类似的文章)
在之前的文章<RichEdit 各个版本介绍>中,写到RichEdit已经到达6.0版本了,而我们经常编程使用的却还是2.0,在vc6.0中甚至还使用1.0版本,更高的版本修复了 ...