Atlantis poj1151 线段树扫描线
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 线段树扫描线的更多相关文章
- hdu1542 Atlantis (线段树+扫描线+离散化)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- POJ1151+线段树+扫描线
/* 线段树+扫描线+离散化 求多个矩形的面积 */ #include<stdio.h> #include<string.h> #include<stdlib.h> ...
- poj1151 Atlantis (线段树+扫描线+离散化)
有点难,扫描线易懂,离散化然后线段树处理有点不太好理解. 因为这里是一个区间,所有在线段树中更新时,必须是一个长度大于1的区间才是有效的,比如[l,l]这是一根线段,而不是区间了. AC代码 #inc ...
- P - Atlantis (线段树+扫描线)
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Som ...
- HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- Atlantis(POJ1151+线段树+扫描线)
题目链接:http://poj.org/problem?id=1151 题目: 题意:求所有矩形的面积,重合部分只算一次. 思路:扫描线入门题,推荐几篇学扫描线的博客: 1.http://www.cn ...
- HDU 1542"Atlantis"(线段树+扫描线求矩形面积并)
传送门 •题意 给你 n 矩形,每个矩形给出你 $(x_1,y_1),(x_2,y_2)$ 分别表示这个矩形的左下角和右上角坐标: 让你求这 n 个矩形并的面积: 其中 $x \leq 10^{5} ...
- POJ 1151 Atlantis(线段树-扫描线,矩形面积并)
题目链接:http://poj.org/problem?id=1151 题目大意:坐标轴上给你n个矩形, 问这n个矩形覆盖的面积 题目思路:矩形面积并. 代码如下: #include<stdio ...
- 【POJ1151】Atlantis(线段树,扫描线)
[POJ1151]Atlantis(线段树,扫描线) 题面 Vjudge 题解 学一学扫描线 其实很简单啦 这道题目要求的就是若干矩形的面积和 把扫描线平行于某个轴扫过去(我选的平行\(y\)轴扫) ...
随机推荐
- 【leetcode】491. Increasing Subsequences
题目如下: 解题思路:这题把我折腾了很久,一直没找到很合适的方法,主要是因为有重复的数字导致结果会有重复.最后尝试用字典记录满足条件的序列,保证不重复,居然Accept了. 代码如下: class S ...
- webpack中require.context 用法
1.require.context(directory, useSubdirectories = false, regExp = /^\.\//) Examples: require.context( ...
- 华为云服务器centos7.3 安装jdk
1. 进入oracle官网 https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 服 ...
- 3,ActiveMQ-入门(基于JMS发布订阅模型)
一.Pub/Sub-发布/订阅消息传递模型 在发布/订阅消息模型中,发布者发布一个消息,该消息通过topic传递给所有的客户端.在这种模型中,发布者和订阅者彼此不知道对方,是匿名的且可以动态发布和订阅 ...
- python 获取list某个元素下标
index() 函数用于从列表中找出某个值第一个匹配项的索引位置. list.index(x, start, end) #start end 指示搜索的起始和结尾位置,缺省为整个数组 x-- 查找的对 ...
- Move Controller UE4键位
工作中需要,就总结了一下,如下图:
- Window7系统安装Ubuntu16双系统
在电脑上插入ubuntu系统启动盘,之前做好的u盘启动盘,重启计算机,进入BIOS设置界面,设置系统启动为u盘启动,保存后退出.之后进入ubuntu系统安装界面. 在安装界面中选择系统语言,选择安装u ...
- Linux驱动开发3——devfs udev procfs sysfs debugfs傻傻地分不清楚
Linux调试文件系统 1.1.procfs 早期的Linux内核中,内核通过procfs输出调试信息,可以在用户态通过读写procfs节点与内核进行交互,用来获取处理器.内存.设备驱动.进程等各种信 ...
- UEFI手札
基于Intel TianoCore衍生的EDK-II诞生的UEFI,用来取代Legacy BIOS. INF文件 Module Information File,模块描述文件.Module可以是可执行 ...
- 006-Spring Boot自动配置-Condition、Conditional、Spring提供的Conditional自动配置
一.接口Condition.Conditional(原理) 主要提供一下方法 boolean matches(ConditionContext context, AnnotatedTypeMetada ...