HDU-1828 Picture(扫描线)
题目大意:给若干个矩形,求轮廓边长。
题目分析:与求面积类似。按从下往上扫描,仍然是底边添加,上边删除。但要同时维护竖边的数目,每次扫描对答案的贡献为扫描线上总覆盖长度的变化量加上竖边的增量。总覆盖长度的变化为正说明下轮廓增加,为负以为着碰到了上轮廓增加。
代码如下:
# include<bits/stdc++.h>
using namespace std;
# define mid (l+(r-l)/2)
# define LL long long const int N=5000; struct Segment
{
int x1,x2,y;
int d;
};
struct Node
{
int cnt; ///覆盖的竖线数目
int len; ///被覆盖总长度
int time; ///被覆盖覆盖的次数
bool cl,cr; ///左右端点是否被覆盖
};
Segment seg[N<<1];
Node nde[(N<<4)+5]; bool comp(Segment &s1,Segment &s2)
{
return s1.y<s2.y;
} void build(int rt,int l,int r)
{
nde[rt].cnt=0;
nde[rt].len=0;
nde[rt].time=0;
nde[rt].cl=nde[rt].cr=false;
if(l==r) return ;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
} void pushUp(int rt,int l,int r)
{
if(nde[rt].time>0){
nde[rt].cl=nde[rt].cr=true;
nde[rt].cnt=2;
nde[rt].len=r-l+1;
}else if(l==r){
nde[rt].cl=nde[rt].cr=false;
nde[rt].cnt=0;
nde[rt].len=0;
}else{
nde[rt].cl=nde[rt<<1].cl;
nde[rt].cr=nde[rt<<1|1].cr;
nde[rt].len=nde[rt<<1].len+nde[rt<<1|1].len;
nde[rt].cnt=nde[rt<<1].cnt+nde[rt<<1|1].cnt;
if(nde[rt<<1].cr&&nde[rt<<1|1].cl)
nde[rt].cnt-=2;
}
} void update(int rt,int l,int r,int L,int R,int d)
{
if(L<=l&&r<=R){
nde[rt].time+=d;
pushUp(rt,l,r);
}else{
if(L<=mid) update(rt<<1,l,mid,L,R,d);
if(R>mid) update(rt<<1|1,mid+1,r,L,R,d);
pushUp(rt,l,r);
}
} int main()
{
int n;
while(~scanf("%d",&n))
{
int L=10000,R=-10000;
int x1,y1,x2,y2;
for(int i=0;i<n;++i){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
seg[i<<1].x1=seg[i<<1|1].x1=x1,seg[i<<1].y=y1;
seg[i<<1].x2=seg[i<<1|1].x2=x2,seg[i<<1|1].y=y2;
seg[i<<1].d=1;
seg[i<<1|1].d=-1;
L=min(x1,L);
R=max(x2,R);
}
n<<=1;
build(1,L,R-1);
sort(seg,seg+n,comp);
int ans=0;
int last=0;
seg[n].y=seg[n-1].y;
for(int i=0;i<n;++i){
update(1,L,R-1,seg[i].x1,seg[i].x2-1,seg[i].d);
ans+=nde[1].cnt*(seg[i+1].y-seg[i].y);
ans+=abs(nde[1].len-last);
last=nde[1].len;
}
printf("%d\n",ans);
}
return 0;
}
HDU-1828 Picture(扫描线)的更多相关文章
- 51nod 1206 && hdu 1828 Picture (扫描线+离散化+线段树 矩阵周长并)
1206 Picture 题目来源: IOI 1998 基准时间限制:2 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 收藏 关注 给出平面上的N个矩形(矩形的边平行于X轴 ...
- HDU 1828 Picture(长方形的周长和)
HDU 1828 Picture 题目链接 题意:给定n个矩形,输出矩形周长并 思路:利用线段树去维护,分别从4个方向扫一次,每次多一段的时候,就查询该段未被覆盖的区间长度,然后周长就加上这个长度,4 ...
- HDU 1828 Picture(线段树扫描线求周长)
Picture Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- HDU 1828“Picture”(线段树+扫描线求矩形周长并)
传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ...
- hdu 1828 Picture(线段树 || 普通hash标记)
http://acm.hdu.edu.cn/showproblem.php?pid=1828 Picture Time Limit: 6000/2000 MS (Java/Others) Mem ...
- HDU 1828 Picture (线段树+扫描线)(周长并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1828 给你n个矩形,让你求出总的周长. 类似面积并,面积并是扫描一次,周长并是扫描了两次,x轴一次,y ...
- POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算
求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...
- (中等) HDU 1828 Picture,扫描线。
Problem Description A number of rectangular posters, photographs and other pictures of the same shap ...
- hdu 1828 Picture(线段树扫描线矩形周长并)
线段树扫描线矩形周长并 #include <iostream> #include <cstdio> #include <algorithm> #include &l ...
- hdu 1828 Picture(线段树,扫描线)
A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wa ...
随机推荐
- 基于百度定位及天气获取的DEMO
demo基于百度定位APIv4.0版.新浪天气(不用查询城市代码). 需求: 1.button实现触发定位监听和天气捕获 2.两个textview 分别显示详细地址.天气. 界面很简陋,侧重功能实现. ...
- Apache Ant运行时Unable to locate tools.jar解决方法
下载Apache Ant 一.解压ant安装包在D:\ant下 二.环境变量配置 ANT_HOME D:\ant\apache-ant-1.9.0 CLASSPATH ;%ANT_HOME%lib; ...
- PHP Array 函数
PHP Array 简介 array 函数允许您对数组进行操作. PHP 支持单维和多维的数组.同时提供了用数据库查询结果来构造数组的函数. 安装 array 函数是 PHP 核心的组成部分.无需安装 ...
- C++ primer的第一章的主要内容
第一章主要是把C++的主要的部分简单的介绍了一下,让读者对C++开始有一个简单的了解.看完第一章的收获就是知道如何去读入不确定数目的输入,主要是形式是:whlie(cin>>s){},利用 ...
- poj1458
//Accepted 4112 KB 16 ms //最长公共子串 #include <cstdio> #include <cstring> #include <iost ...
- Linux信号(signal) 机制分析
Linux信号(signal) 机制分析 [摘要]本文分析了Linux内核对于信号的实现机制和应用层的相关处理.首先介绍了软中断信号的本质及信号的两种不同分类方法尤其是不可靠信号的原理.接着分析了内核 ...
- mysql 创建存储过程注意
最近在利用navicat创建存储过程时,总是报1064语法错误,而且每次都是指向第一行,百思不得姐,如下图: 后来发现,原来是输入参数没有定义长度导致,所以以后真要注意 加上入参长度即可:IN `sT ...
- 怎么用sql语句查看某个字段值是否是唯一的
select count(*) from table group by zd having count(*)>1 这是不唯一的过滤出来的语句
- iOS 开发之重力动画效果
步骤:1.使用single view application创建新的项目 2.在viewcontroller.h文件中创建一个图片实例并与相关图片相连,然后创建一个UIDynamicAnimator ...
- BZOJ 2442 修剪草坪
NOIP的时候一定要看清楚数据范围. #include<iostream> #include<cstdio> #include<cstring> #include& ...