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\)轴扫) ...
随机推荐
- Cocos Creator更改底层,点击节点获取路径
Mac : 右键creator->显示包内容->Contents->Resources->engine->bin->cocos2d-js-for-preview.j ...
- Bean的生命周期与JVM**
案例: 在service里定义了一个全局变量,这类变量只能是final的.如果不是,在代码中一旦有地方给该变量进行业务赋值.当不满足赋值条件时变量仍然保留上次的赋值的值. 这是因为Bean没有销毁. ...
- 前端之JQuery:JQuery扩展和事件
jQuery之jQuery扩展和事件 一.jQuery事件 常用事件 blur([[data],fn]) 失去焦点 focus([[data],fn]) 获取焦点( 搜索框例子) change([[d ...
- 【leetcode】698. Partition to K Equal Sum Subsets
题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...
- mybatis中延迟加载Lazy策略
延迟加载: lazy策略原理:只有在使用查询sql返回的数据是才真正发出sql语句到数据库,否则不发出(主要用在多表的联合查询) 1.一对一延迟加载: 假设数据库中有person表和card表:其中p ...
- WEB实现大文件上传和下载
我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...
- [UVA160]Factors and Factorials 题解
前言 这道题目本身毫无技术含量珂言,但是输出格式珂以调一年 题解 这道题让我们求\(N!\)中每个质数的个数. 一种方法是直接模拟,枚举\(N!\)中的每个元素,然后暴力查看每个数含有有多少质数. 但 ...
- Laya layout算法
/** * <p>重置对象的 <code>X</code> 轴(水平方向)布局.</p> * @private */ public function r ...
- Content-type的几种常见类型
一.是什么? 是Http的实体首部字段,用于说明请求或返回的消息主体是用何种方式编码,在request header和response header里都存在. 二.几个常用类型: 1.applicat ...
- ThreadLocal在Spring事务管理中的应用
ThreadLocal是用来处理多线程并发问题的一种解决方案.ThreadLocal是的作用是提供线程的局部变量,在多线程并发环境下,提供了与其他线程隔离的局部变量.通常这样的设计的情况是因为这个局部 ...