Atlantis poj1151 线段树扫描线

题意

题目给了n个矩形,每个矩形给了左下角右上角的坐标,矩形可能会重叠,求的是矩形最后的面积。

题解思路

这个是我线段树扫描线的第一题,听了学长的讲解,仔细研读了学长的代码,也算初步入门。

这里我们竖着的扫描线,从左到右来进行扫描的。

线段树这里端点代表的是一个区间

这里的y范围比较大,所以咱们需要离散化。

代码实现

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#define mid ((tre[rt].l+tre[rt].r)>>1)
#define ls (rt<<1)
#define rs (rt<<1|1)
using namespace std;
const int maxn=1e2+7;
struct node{ //四元组
double x, y1, y2;
int k; //左边的边k是1, 右边的k为-1
bool friend operator < (node a, node b)
{
return a.x < b.x;
}
}a[maxn<<1]; struct tree{
int l, r, cnt; //cnt记录区间被标记的次数
double len;
}tre[maxn<<3]; map<double, int> mp;
double raw[maxn<<1];
int n,num; void build(int rt, int l, int r)
{
tre[rt].l=l;
tre[rt].r=r;
tre[rt].len=tre[rt].cnt=0;
if(l==r) return ;
build(ls, l, mid);
build(rs, mid+1, r);
} void change(int rt, int l, int r, int k)
{
if(l <= tre[rt].l && tre[rt].r <= r)
{
tre[rt].cnt+=k;
if(tre[rt].cnt>0)
{
tre[rt].len=raw[tre[rt].r+1] - raw[tre[rt].l];
}
else if(tre[rt].l==tre[rt].r) //到达端点了
{
tre[rt].len=0;
}
else tre[rt].len=tre[ls].len+tre[rs].len; return ;
}
if(l <= mid) change(ls, l, r, k);
if(r > mid) change(rs, l, r, k);
tre[rt].len = tre[rt].cnt > 0 ? raw[tre[rt].r+1] - raw[tre[rt].l] : tre[ls].len+tre[rs].len;
} int main()
{
while(scanf("%d", &n) && n)
{
int cnt;
double x1, y1, x2, y2;
for(int i=1; i<=n; i++)
{
cnt=i<<1;
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
a[cnt-1].x=x1; a[cnt-1].y1=y1; a[cnt-1].y2=y2; a[cnt-1].k=1;
a[cnt].x=x2; a[cnt].y1=y1; a[cnt].y2=y2; a[cnt].k=-1;
raw[cnt-1]=y1; raw[cnt]=y2;
}
n<<=1;
sort(raw+1, raw+1+n);
int m=unique(raw+1, raw+n+1)-(raw+1);
for(int i=1; i<=m; i++)
{
mp[raw[i]]=i;
}
sort(a+1, a+n+1);
build(1, 1, m-1); //这里线段树的点记录的区域,因为有m个y,所以就相当于m-1个区域
double ans=0;
for(int i=1; i<n; i++) //只需要处理到倒数第二个点即可
{
int l=mp[a[i].y1], r=mp[a[i].y2]-1;
change(1, l, r, a[i].k);
ans += tre[1].len*(a[i+1].x - a[i].x);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n", ++num, ans);
}
return 0;
}

Atlantis poj1151 线段树扫描线的更多相关文章

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

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

  2. POJ1151+线段树+扫描线

    /* 线段树+扫描线+离散化 求多个矩形的面积 */ #include<stdio.h> #include<string.h> #include<stdlib.h> ...

  3. poj1151 Atlantis (线段树+扫描线+离散化)

    有点难,扫描线易懂,离散化然后线段树处理有点不太好理解. 因为这里是一个区间,所有在线段树中更新时,必须是一个长度大于1的区间才是有效的,比如[l,l]这是一根线段,而不是区间了. AC代码 #inc ...

  4. P - Atlantis (线段树+扫描线)

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

  5. HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)

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

  6. Atlantis(POJ1151+线段树+扫描线)

    题目链接:http://poj.org/problem?id=1151 题目: 题意:求所有矩形的面积,重合部分只算一次. 思路:扫描线入门题,推荐几篇学扫描线的博客: 1.http://www.cn ...

  7. HDU 1542"Atlantis"(线段树+扫描线求矩形面积并)

    传送门 •题意 给你 n 矩形,每个矩形给出你 $(x_1,y_1),(x_2,y_2)$ 分别表示这个矩形的左下角和右上角坐标: 让你求这 n 个矩形并的面积: 其中 $x \leq 10^{5} ...

  8. POJ 1151 Atlantis(线段树-扫描线,矩形面积并)

    题目链接:http://poj.org/problem?id=1151 题目大意:坐标轴上给你n个矩形, 问这n个矩形覆盖的面积 题目思路:矩形面积并. 代码如下: #include<stdio ...

  9. 【POJ1151】Atlantis(线段树,扫描线)

    [POJ1151]Atlantis(线段树,扫描线) 题面 Vjudge 题解 学一学扫描线 其实很简单啦 这道题目要求的就是若干矩形的面积和 把扫描线平行于某个轴扫过去(我选的平行\(y\)轴扫) ...

随机推荐

  1. Word快捷选取

    在word中,你知道鼠标单击选中一个词,双击选中一行,三击选中一个段落吗?

  2. python如何导入自定义文件和模块$PYTHONHOME$\Lib\site-packages 方法

    python 中如何引用自己创建的源文件(*.py)呢? 也就是所谓的模块. 假如,你有一个自定义的源文件,文件名:saySomething.py .里面有个函数,函数名:sayHello.如下图: ...

  3. 【bzoj1093】 [ZJOI2007]最大半连通子图

    *题目描述: 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意 两点u,v,存在一条u到v的有向路径或者从v到u的有向路 ...

  4. Improving Network Management with Software Defined Networking

    Name of article:Improving Network Management with  Software Defined Networking Origin of the article ...

  5. 记一下今天上下班学的Thread

    1 Thread 默认为前台线程,即主程序退出后,线程还可以继续(曾经就掉入这个坑中,使用两线程分别进行UDP收发,结果发线程结束了退出方法,收线程还在继续) 2 Thread 没有可以暂停,可以重新 ...

  6. Java Interger类,两对整数明明完全一样,为何一个输出true,一个输出false

    package text; public class MethodOverload { public static void main(String[] args) { Integer i1=100; ...

  7. 170831-关于JdbcTemplate声明式事务-操作步骤-例子

    创建一个动态web工程 加入jar包 3.创建一份jdbc.properties文件 4.在spring配置文件中配置数据源 5.测试数据源: 6.配置jdbcTemplate: 7.创建Dao类 & ...

  8. 个推基于 Zipkin 的分布式链路追踪实践

    作者:个推应用平台基础架构高级研发工程师 阿飞   01业务背景   随着微服务架构的流行,系统变得越来越复杂,单体的系统被拆成很多个模块,各个模块通过轻量级的通信协议进行通讯,相互协作,共同实现系统 ...

  9. javaweb阶段几个必会面试题

    1.jsp的9大隐式对象 response(page):response对象是javax.servlet.http.HttpServletResponse对象的一个实例.就像服务器创建request对 ...

  10. python 2和3 字符编码

    在字符编码问题上,python2 和python3 还是有点不同的.今日写篇博客,彻底理清这个问题.. 字符编码问题的由来: 这要从计算发展历史来看待这个问题了,一开始,歪果仁使用ASCII码,8位( ...