hdu1542 Atlantis(矩阵面积的并)
这个题算是我的第一个扫描线的题,扫描线算是一种思想吧,用到线段树+离散化。感觉高大上。
主要参考了这位大神的博客。
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(矩阵面积的并)的更多相关文章
- HDU1542 Atlantis(矩形面积并)
#pragma warning (disable:4996) #include<iostream> #include<cstring> #include<string&g ...
- hdu1542 Atlantis (线段树+矩阵面积并+离散化)
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...
- POJ 1151 Atlantis 求矩阵面积并 扫描线 具体解释
题意: 给定n个矩阵的左下角和右上角坐标,求矩阵面积并(矩阵总是正放的,即与x轴y轴都平行) 思路: 扫描线裸题 http://www.cnblogs.com/fenshen371/p/3214092 ...
- 2017ICPC南宁赛区网络赛 Overlapping Rectangles(重叠矩阵面积和=离散化模板)
There are nnn rectangles on the plane. The problem is to find the area of the union of these rectang ...
- 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 ...
- 【 HDU 1255】 覆盖的面积(矩阵面积交,线段树,扫描法)
[题目] 覆盖的面积 Problem Description 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input 输入数据的第一行是一个正整数T(1<=T<=100 ...
- 第二大矩阵面积--(stack)牛客多校第二场-- Second Large Rectangle
题意: 给你一幅图,问你第二大矩形面积是多少. 思路: 直接一行行跑stack求最大矩阵面积的经典算法,不断更新第二大矩形面积,注意第二大矩形可能在第一大矩形里面. #define IOS ios_b ...
- POJ 1151 Atlantis(经典的线段树扫描线,求矩阵面积并)
求矩阵的面积并 采用的是区间更新 #include <iostream> #include <stdio.h> #include <string.h> #inclu ...
- hdu1542 Atlantis 线段树--扫描线求面积并
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...
随机推荐
- SqlServer中循环查询结果集
); begin ; open c_test_main;--打开游标 --开始循环 begin fetch next from c_test_main into @id,@value; --赋值到变量 ...
- matplotlib包画基本的图
画直线图 1.最简单的用法: import matplotlib.pyplot as plt import numpy as np x=np.linspace(-3,3,50) #在(-1,1)范围内 ...
- Objective-c官方文档 怎么自定义类
通过类别来给已经存在的类添加方法来实现自定义类 如果你需要添加一个方法给一个已经存在的类,也许能增加新的功能使你更容易来在我们的应用里处理一些事情.最简单的方法是用类别. 这个语法有点想类的接口描述但 ...
- 【软件分析与挖掘】An Empirical Study of Bugs in Build Process
摘要 对软件构建过程中所产生的错误(build process bugs)进行实证研究. 5个开源项目:CXF, Camel, Felix,Struts, and Tuscany. 把build pr ...
- Artech的MVC4框架学习——第六章Model的验证
第一Model验证旨在为通过Model绑定生成参数进行检验以确保用户输入数据的有效性(p318) 第二Model验证分两种:服务器端(三种解决方案 p256)和客户端(ajax\jQuery) 第三服 ...
- jenkins - svn: E170001报错的原因以及解决方案
1. 什么问题What? 使用Jenkins配置的svn拉取项目,Jenkins报错:svn: E170001; Your credentials to connect to the reposito ...
- 替换Quartus 自带编辑器 (转COM张)
正文 此处以Quartus II 11.1和Notepad++ v5.9.6.2为例. 1. 使用QII自动调用Notepad++来打开HDL.sdc.txt等文件:并且可以在报错的时候,Notepa ...
- 【BZOJ4361】isn 动态规划+树状数组+容斥
[BZOJ4361]isn Description 给出一个长度为n的序列A(A1,A2...AN).如果序列A不是非降的,你必须从中删去一个数, 这一操作,直到A非降为止.求有多少种不同的操作方案, ...
- Thinkphp框架下设置session的过期时间
打开项目中的配置文件,添加session的过期配置,如下: 'SESSION_OPTIONS' => array( 'name' => 'BJYSESSION', //设置session名 ...
- 初学filter
一. Filter介绍 Filter可以认为是Servlet的一种加强版,它主要用于对用户请求进行预处理,也可以对HTTPServletResponse进行后处理,是个典型的处理链.它的完整处理流程是 ...