给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(扫描线)的更多相关文章

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

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

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

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

  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 —— 求矩形面积并 线段树 + 扫描线 + 离散化

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

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

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

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

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

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

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

  9. [POJ1151][HDU1542]Atlantis(线段树,扫描线)

    英文题面,我就只放个传送门了. Solution  题意是算矩形面积并,这是扫描线算法能解决的经典问题. 算法的大致思想是,把每一个矩形拆成上边和下边(以下称作扫描线),每条扫描线有四个参数l,r,h ...

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

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

随机推荐

  1. Java学习-集合(转)

    在编写java程序中,我们最常用的除了八种基本数据类型,String对象外还有一个集合类,在我们的的程序中到处充斥着集合类的身影!java中集合大家族的成员实在是太丰富了,有常用的ArrayList. ...

  2. C++学习笔记37:元编程

    元编程 什么是元编程(metaprogramming) 利用模板可以进行编译期计算(数值计算,型式计算和代码计算)的特点进行程序设计 为什么可以进行元编程? C++是两层语言:执行编译期计算的代码称为 ...

  3. round(x[, n]) : 四舍五入

    >>> round(12.3) 12.0 >>> round(12.5) 13.0 >>> round(12.36) 12.0 >>& ...

  4. 理解sparse coding

    理解sparse coding 稀疏编码系列: (一)----Spatial Pyramid 小结 (二)----图像的稀疏表示——ScSPM和LLC的总结 (三)----理解sparse codin ...

  5. Weex详解:灵活的移动端高性能动态化方案

    原文地址:http://www.infoq.com/cn/articles/introducing-weex 在2016年4月份的QCon上,阿里巴巴资深总监,淘宝移动平台及新业务事业部.阿里百川负责 ...

  6. memcached源代码包下载

    先下载libevent https://github.com/downloads/libevent/libevent/libevent-2.0.18-stable.tar.gz 再下载memcache ...

  7. ANDROID_MARS学习笔记_S02_007_Animation第一种使用方式:代码

    一.简介 二.代码1.xml(1)activity_main.xml <?xml version="1.0" encoding="utf-8"?> ...

  8. ANDROID_MARS学习笔记_S01原始版_022_MP3PLAYER002_本地及remote标签

    一.简介 1.在main.xml中用TabHost.TabWidget.FrameLayout标签作布局 2.在MainActivity中生成TabHost.TabSpec,调用setIndicato ...

  9. Nuget

    Install-Package Microsoft.AspNet.WebApi.Cors

  10. Django Model数据访问Making queries

    创建完Model之后, Django 自动为你提供一套数据库抽象层的API,利用它可以完成创建,提取,更新,删除对象的操作. 以下面的Model为例: class Blog(models.Model) ...