这个题算是我的第一个扫描线的题,扫描线算是一种思想吧,用到线段树+离散化。感觉高大上。

主要参考了这位大神的博客。

http://www.cnblogs.com/kuangbin/archive/2012/08/15/2640870.html

HDU1542 Atlantis(线段树:扫描线)

http://acm.hdu.edu.cn/showproblem.php?pid=1542

分析:

首先假设有下图两个矩阵,我们如果用扫描线的方法如何计算它们的总面积呢?

首先我们将矩形的上下边分为上位边(即y坐标大的那条平行于x轴的边),和下位边(y坐标小的平行于x轴的边).然后我们把所有矩形的上下位边按照他们y坐标从小到大排序,可以得到4条扫描线:

又因为上面2个矩形有4个不同的浮点数x坐标,所以我们需要把x坐标离散化,这样才能用线段树来维护信息.所以我们这样离散化:

由上图可知,4个不同的x坐标把x轴分成了3段有效的区间.这里要注意我们线段树中每个叶节点(控制区间[L,L])不是指X[L]坐标,而是指区间[X[L],X[L+1]].线段树中其他节点控制的区间[L,R],也是指的x坐标轴的第L个区间到第R个区间的范围,也就是X[L]到X[R+1]坐标的范围.

然后我们Y坐标从小到大的顺序读入每条扫描线,并维护当前我们所读入的所有扫描线能有效覆盖X轴的最大长度sum[1].这里特别要注意如果我们读入的扫描线是矩形的下位边,那么我们就使得该范围的标记cnt位+1,如果是上位边,那么该范围的cnt就-1.所以如果cnt=0时,表示该节点控制的范围没有被覆盖,只要cnt!=0 就表示该节点控制的几块区间仍然被覆盖.

下面依次读入每条矩阵边,来一一分析,首先是读入第一条矩阵边:

我们读入了矩形1的下位边,那么该区域的cnt就+1=1了,所以该区域[10,20]就被覆盖了,然后可以推出整个区域被覆盖的长度是10.再根据第二条扫描线离第一条扫描线的高度差为5.所以不管你第二条扫描线是哪个矩形的什么边,或者能覆盖到X轴的什么范围,我上图中蓝色的矩形面积肯定是要算到总面积里面去的.即总面积ret+=sum[1]*(扫描线2的高度-扫描线1的高度). (想想看是不是这样).

下面读第二条扫描线:

由于第二条扫描线也是下位边,所以[15,20]和[20,25]的cnt+1.使得我们覆盖的范围变成了[10,25]了,并且第3条扫描线在20高度,所以这次我们必然增加的面积是上面深蓝色的长条=sum[1]*(扫描线3的高度-扫描线2的高度).

下面我们要读第三条扫描线了:

由于第三条扫描线是区间[10,20]的上位边,所以对应区间的cnt要-1,所以使得区间[10,15]的cnt=0了,而[15,20]区间的cnt-1之后变成了1.[20,25]的cnt仍然为1,不变.所以当前覆盖的有效x轴长度为10,即区间[15,25].所以增加的面积是图中褐色的部分.

到此,矩形的面积和就算出来了.由于对于任一矩形都是先读下位边(cnt+1),再读上位边(cnt-1)的,所以在更新线段树的过程中,任意节点的cnt都是>=0的.

贴一下xj学长的代码吧。(主要有注释)

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define LL __int64
#define lson l, mid, 2*rt
#define rson mid+1, r, 2*rt+1
const int maxn = +;
using namespace std;
int n;
double y[maxn];
struct node
{
int l, r, c; //l, r记录左右节点,c记录覆盖情况
double cnt, lf, rf; //cnt记录这一段当前的长度,lf,rf记录这一段实际的边界
}tr[*maxn];
struct Line
{
double x, y1, y2; //x记录该段x坐标,y1,y2记录该段投影的y边界
int f;
}line[maxn]; //记录在y轴上的投影,f=1表示线段的开始,f=-1表示线段的结束
bool cmp(Line a, Line b)
{
return a.x < b.x;
}
void build(int l, int r, int rt)
{
tr[rt].l = l; tr[rt].r = r;
tr[rt].cnt = tr[rt].c = ;
tr[rt].lf = y[l]; tr[rt].rf = y[r]; //相当于离散化,把实际左右坐标离散成l,r
if(l+==r) return;
int mid = (l+r)/;
build(l, mid, *rt);
build(mid, r, *rt+); //注意是mid,不是mid+1,因为要所有段覆盖
}
void calen(int rt)
{
if(tr[rt].c>) //如果这段被覆盖,就更新这段的长度为实际长度
{
tr[rt].cnt = tr[rt].rf-tr[rt].lf;
return;
}
if(tr[rt].l+==tr[rt].r) tr[rt].cnt = ; //如果这段被撤销,而且是最后的就把长度变为0
else tr[rt].cnt = tr[*rt].cnt+tr[*rt+].cnt; //如果被撤销但不是最后的,就加一下左右
}
void update(int rt, Line e) //加入或者减去一条线段后的更新
{
if(e.y1==tr[rt].lf && e.y2==tr[rt].rf)
{
tr[rt].c += e.f; //改变覆盖情况
calen(rt);
return;
}
if(e.y2<=tr[*rt].rf) update(*rt, e);
else if(e.y1>=tr[*rt+].lf) update(*rt+, e);
else //跨区间的情况
{
Line tmp = e;
tmp.y2 = tr[*rt].rf;
update(*rt, tmp);
tmp = e;
tmp.y1 = tr[*rt+].lf;
update(*rt+, tmp);
}
calen(rt);
}
int main()
{
int i, ca=, cnt;
double x1, x2, y1, y2, ans;
while(~scanf("%d", &n)&&n)
{
cnt = ; ans = ;
for(i = ; i < n; i++)
{
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
line[cnt].x = x1; line[cnt].y1 = y1;
line[cnt].y2 = y2; line[cnt].f = ;
y[cnt++] = y1;
line[cnt].x = x2; line[cnt].y1 = y1;
line[cnt].y2 = y2; line[cnt].f = -;
y[cnt++] = y2;
}
cnt--;
sort(line+, line+cnt+, cmp); //按x从小到大排序
sort(y+, y+cnt+); //按照y排序
build(, cnt, ); update(, line[]);
for(i = ; i <= cnt; i++)
{
ans += tr[].cnt*(line[i].x-line[i-].x); //tr[1].cnt记录全段中当前覆盖的值
update(, line);
}
printf("Test case #%d\n", ca++);
printf("Total explored area: %.2lf\n\n", ans);
}
return ;
}

感觉这代码好棒。

暑假学的,好久没看又忘了,又重新写了一遍,怎么说这题都是我扫描线第一发。

一篇介绍扫描线很好的博客。(http://www.cnblogs.com/scau20110726/archive/2013/04/12/3016765.html

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define N 210
using namespace std;
struct Line
{
double x1,x2,y;
int f;
} line[N];
struct node
{
double lf,rf,cnt;
int l,r,c;
} q[*N];
int n,tt;
double X[N];
void add(double x1,double y1,double x2,double y2)
{
line[tt].x1=x1;
line[tt].x2=x2;
line[tt].y=y1;
X[tt]=x1;
line[tt++].f=;
line[tt].x1=x1;
line[tt].x2=x2;
line[tt].y=y2;
X[tt]=x2;
line[tt++].f=-;
}
bool cmp(Line a,Line b)
{
return a.y<b.y;
}
void build(int l,int r,int rt)
{
q[rt].l=l;
q[rt].r=r;
q[rt].cnt=q[rt].c=;
q[rt].lf=X[l];
q[rt].rf=X[r];
if(l+==r) return ;
int mid=(l+r)>>;
build(l,mid,rt<<);
build(mid,r,rt<<|);
}
void calen(int rt)
{
if(q[rt].c>)
{
q[rt].cnt=q[rt].rf-q[rt].lf;
return ;
}
if(q[rt].l+==q[rt].r) q[rt].cnt=;
else q[rt].cnt=q[rt<<].cnt+q[rt<<|].cnt;
}
void update(Line e,int rt)
{
if(e.x1<=q[rt].lf&&e.x2>=q[rt].rf)
{
q[rt].c+=e.f;
calen(rt);
return ;
}
if(e.x2<=q[rt<<].rf) update(e,rt<<);
else if(e.x1>=q[rt<<|].lf) update(e,rt<<|);
else
{
Line tmp=e;
tmp.x2=q[rt<<].rf;
update(tmp,rt<<);
tmp=e;
tmp.x1=q[rt<<|].lf;
update(tmp,rt<<|);
}
calen(rt);
}
int main()
{
int K=;
double x1,x2,y1,y2;
while(scanf("%d",&n)!=EOF&&n!=)
{
tt=;
printf("Test case #%d\n",++K);
for(int i=; i<n; i++)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
add(x1,y1,x2,y2);
}
sort(X+,X+tt);
sort(line+,line+tt,cmp);
build(,tt-,);
update(line[],);
double sum=;
for(int i=; i<tt; i++)
{
sum+=q[].cnt*(line[i].y-line[i-].y);
update(line[i],);
}
printf("Total explored area: %.2lf\n\n", sum);
}
return ;
}

hdu1542 Atlantis(矩阵面积的并)的更多相关文章

  1. HDU1542 Atlantis(矩形面积并)

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

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

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

  3. POJ 1151 Atlantis 求矩阵面积并 扫描线 具体解释

    题意: 给定n个矩阵的左下角和右上角坐标,求矩阵面积并(矩阵总是正放的,即与x轴y轴都平行) 思路: 扫描线裸题 http://www.cnblogs.com/fenshen371/p/3214092 ...

  4. 2017ICPC南宁赛区网络赛 Overlapping Rectangles(重叠矩阵面积和=离散化模板)

    There are nnn rectangles on the plane. The problem is to find the area of the union of these rectang ...

  5. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并

    D. Vika and Segments     Vika has an infinite sheet of squared paper. Initially all squares are whit ...

  6. 【 HDU 1255】 覆盖的面积(矩阵面积交,线段树,扫描法)

    [题目] 覆盖的面积 Problem Description 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input 输入数据的第一行是一个正整数T(1<=T<=100 ...

  7. 第二大矩阵面积--(stack)牛客多校第二场-- Second Large Rectangle

    题意: 给你一幅图,问你第二大矩形面积是多少. 思路: 直接一行行跑stack求最大矩阵面积的经典算法,不断更新第二大矩形面积,注意第二大矩形可能在第一大矩形里面. #define IOS ios_b ...

  8. POJ 1151 Atlantis(经典的线段树扫描线,求矩阵面积并)

    求矩阵的面积并 采用的是区间更新 #include <iostream> #include <stdio.h> #include <string.h> #inclu ...

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

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

随机推荐

  1. 【资源大全】.NET资源大全中文版(Awesome最新版)

    算法与数据结构(Algorithms and Data structures) 应用程序接口(API) 应用程序框架(Application Frameworks) 模板引擎(Application ...

  2. Android设计和开发系列第二篇:Action Bar(Develop—API Guides)

    Action Bar IN THIS DOCUMENT Adding the Action Bar Removing the action bar Using a logo instead of an ...

  3. Win8交互UX——笔交互

    针对触摸输入优化 Window 应用商店应用设计,并在默认情况下获得基本的笔支持. 本主题介绍笔交互的设计注意事项.有关实现笔交互的信息,请参阅响应笔和触笔交互. 笔交互 通过使用笔创建手写便笺.绘图 ...

  4. 【python】解决No module named _sqlite3的问题

    环境版本说明: centos 7 python3.6.4 django 2.0 在创建django项目时候.报错以下信息 详细错误信息如下: 原因: 未安装sqlite 模块 解决: 1 安装 sql ...

  5. List的五种去重方式

    //set集合去重,不改变原有的顺序 public static void pastLeep1(List<String> list){ System.out.println("l ...

  6. PhpDocumentor

    phpDocument是一个通用的生成PHP文档的工具,他本身也是用PHP写的,跟JAVADoc有些相似,但他有不同于PHPDoc,他比PHPDOC要快,而且能够解析的PHP范围更广,他本身包含了11 ...

  7. sonarqube插件开发(三) 调试插件

    环境 windows + eclipse +sonarqube server 5.6.4 准备 新建一个maven项目,其中加入了一些插件所用的jar <project xmlns=" ...

  8. 画一条0.5px的边

    1.scale方法 { height: 1px; transform: scaleY(0.5); transform-origin: 50% 100%; // 要指定origin值, 要不然会模糊 c ...

  9. .net C#中页面之间传值传参的六种方法

    1.QueryString是一种非常简单的传值方式,他可以将传送的值显示在浏览器的地址栏中.如果是传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法.但是对于传递数组或对象的话,就不能 ...

  10. python selenium中等待元素出现及等待元素消失操作

    在自动化测试中,很多时候都会有等待页面某个元素出现后能进行下一步操作,或者列表中显示加载,直到加载完成后才进行下一步操作,但时间都不确定,如下图所示 幸运的是,在selenium 2后有一个模块exp ...