poj-1151矩形面积并-线段树
title: poj-1151矩形面积并-线段树
date: 2018-10-30 22:35:11
tags:
- acm
- 刷题
categoties: - ACM-线段树
概述
线段树问题里的另一个问题,,,矩形面积并,,,,
之前看lazy更新时看到下面这个的讲解,,,一大堆文字还有一大堆的图,,,,当时果断跳过,,,
今天花了一下午加一晚上的时间看了看这块知识,,,然后尝试自己写出代码,,,算是简单的了解一下这块,,,
题意
这道矩形面积并问题的大意是给很多个矩形,,矩形之间可能有交集,,,然后问你这一大片的图形面积是多少,,,,
数据量不大,,看到有很多人是暴力过的,,,
但是用线段树来当作练习题锻炼锻炼思维还是很好的QAQ
思路
一开始我是看这篇博客有关矩形面积并的知识,,,
这篇博客讲解的思路很不错,,,一遍之后大致了解了整个解决问题的思路,,,,但是它没有相应的练习题以及代码,,,,我完全不知道该从哪里下手,,,线段树的具体如何实现一脸懵逼,,,,还有,,,一般这种题都是要将一个方向的坐标 离散化,,,,嗯,,又是这个东西,,,,更是一脸的懵逼,,,,
然后看了这篇博客,,对着代码,,,然后顺着思路写出来了,,,
主要的几点:
- 前面两个博客的图很形象的把思路理了一遍,,,,就是枚举一个方向,,比如y方向,,然后,,将x方向的坐标离散化,,分成若干个 单位线段,,,,线段树维护这个单位线段,,,还是那个博客形象一些
- 整个图形的面积可以分成若干个小的矩形,,,然后加起来就行,,,,
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define aaa cout << x[r + 1] << "----" << x[l] << endl;
const int maxn = 205;
double x[maxn << 2]; //所有的x的数据
//每一条线段
struct segment
{
double y;
double l;
double r;
int flag; //1 or -1: 入边or出边
segment(){}
segment(double y, double l , double r , int flag):y(y) , l(l) , r(r) , flag(flag){}
bool operator < (const segment &res)
{
return y < res.y;
}
}seg[maxn << 1];
//线段树维护所有的单位线段(离散后的)
struct node
{
int cov;
double len;
}node[maxn << 2];
void pushdown(int rt , int l , int r)
{
if(node[rt].cov)
node[rt].len = x[r + 1] - x[l];
else if(l == r)
node[rt].len = 0;
else
node[rt].len = node[rt << 1].len + node[rt << 1 | 1].len;
}
void update(int rt , int l , int r , int L , int R , int cov)
{
if(L <= l && r <= R)
{
node[rt].cov += cov;
pushdown(rt , l , r);
return;
}
int mid = (l + r) >> 1;
if(L <= mid) update(rt << 1 , l , mid , L , R , cov);
if(R > mid) update(rt << 1 | 1 , mid + 1 , r , L , R , cov);
//pushdown
pushdown(rt , l , r);
return;
}
int main()
{
int n;
int q = 1;
while(scanf("%d" , &n) && n)
{
memset(x , 0 , sizeof x);
double x1 , y1 , x2 , y2;
int count = 0;
for(int i = 0; i < n; ++i)
{
scanf("%lf%lf%lf%lf" , &x1 , &y1 , &x2 , &y2);
seg[count]=segment(y1 , x1 , x2 , 1);
x[count++] = x1;
seg[count]=segment(y2 , x1 , x2 , -1);
//segment[i].y = y1;segment[i].l = x1;segment[i].r = x2;segment[i].flag = 1;
//segment[i + 1].y = y2;segment[i + n].l = x1;segment[i + n].r = x2;segment[i + n].flag = -1;
x[count++] = x2;
}
//离散
sort(seg , seg + count);
sort(x , x + count);
int sz = unique(x , x + count) - x;
double ans = 0;
for(int i = 0; i < count; ++i)
{
int l = lower_bound(x , x + sz , seg[i].l) - x;
int r = lower_bound(x , x + sz , seg[i].r) - x - 1;
update(1 , 0 , sz , l , r , seg[i].flag);
ans += node[1].len * (seg[i + 1].y - seg[i].y);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n",q++,ans);
}
}
总结
算了,,,先鸽了,,,细节那天再补一下,,,,
(loading,,,,)
poj-1151矩形面积并-线段树的更多相关文章
- POJ 1151Atlantis 矩形面积并[线段树 离散化 扫描线]
Atlantis Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21734 Accepted: 8179 Descrip ...
- POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并
题意:给出矩形两对角点坐标,求矩形面积并. 解法:线段树+离散化. 每加入一个矩形,将两个y值加入yy数组以待离散化,将左边界cover值置为1,右边界置为2,离散后建立的线段树其实是以y值建的树,线 ...
- POJ1151Atlantis 矩形面积并[线段树 离散化 扫描线]
Atlantis Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21734 Accepted: 8179 Descrip ...
- HDU1542 Atlantis —— 求矩形面积并 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/HDU-1542 There are several ancient Greek texts that contain descript ...
- hdu 1255 覆盖的面积(线段树 面积 交) (待整理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 Description 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. In ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- HDU - 1255 覆盖的面积(线段树求矩形面积交 扫描线+离散化)
链接:线段树求矩形面积并 扫描线+离散化 1.给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. 2.看完线段树求矩形面积并 的方法后,再看这题,求的是矩形面积交,类同. 求面积时,用被覆 ...
- HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)
Description Ted has a new house with a huge window. In this big summer, Ted decides to decorate the ...
- POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算
求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...
随机推荐
- 10个好用的JQuery代码片段收集
1.预加载图片 (function($) { var cache = []; // Arguments are image paths relative to the current page. $. ...
- 正则表达式 grep文本查询 sed流处理 应用
一.正则表达: ^:以什么什么开头,^a:以a字符开头 $:以什么什么结尾,b$:以b字符结尾 *:左边字符0-无穷个 +:左边字符1-无穷个 .:代表单字符 ?:前导字符为零个或1个 {n}:左面字 ...
- POJ 1128 Frame Stacking (拓扑排序)
题目链接 Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ...
- 某大型网络社区传播性XSS分析
某大型网络社区传播性XSS分析 这个XSS存在于天涯某个不起眼的子论坛,通过发布新帖子可以触发. 漏洞分析 论坛针对XSS有一定过滤措施,例如转义单双引号,过滤左右尖括号等等.所 ...
- App测试需注意
APP测试的时候,建议让开发打好包APK和IPA安装包,测试人员自己安装应用,进行测试.在测试过程中需要注意的测试点如下: 1安装和卸载 ●应用是否可以在iOS不同系统版本或Android不同系统版本 ...
- JS获取元素内容属性以及修改
1.通过document对象
- Shell中三种引号的用法及区别
Linux Shell中有三种引号,分别为双引号(" ").单引号(' ')以及反引号(` `). 其中双引号对字符串中出现的$.''.`和\进行替换:单引号不进行替换,将字符串中 ...
- aarch64_m2
mingw32-leptonica-1.74.4-1.fc26.noarch.rpm 2017-06-12 17:20 1.0M fedora Mirroring Project mingw32-le ...
- Error: No resource found that matches the given name (at 'icon' with value '@mipmap/Icon')
问题: error: Error: No resource found that matches the given name (at 'icon' with value '@mipmap/Icon' ...
- HDU 6061 RXD and functions
题目链接:HDU-6061 题意:给定f(x),求f(x-A)各项系数. 思路:推导公式有如下结论: 然后用NTT解决即可. 代码: #include <set> #include < ...