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 ...
随机推荐
- 发布Restful服务时出现IIS 指定了身份验证方案错误时的解决方案(IIS specified authentication schemes)
发布RESTful服务,当访问.svc文件时出现如下错误时: IIS 指定了身份验证方案“IntegratedWindowsAuthentication, Anonymous”,但绑定仅支持一种身份验 ...
- UIViewController
UIViewController 在MVC模式中就是C.关于MVC,可以看 UIViewController 主要具有什么功能呢? View Management When you define a ...
- poj2429 大数分解+dfs
//Accepted 172 KB 172 ms //该程序为随机性算法,运行时间不定 #include <cstdio> #include <cstring> #includ ...
- CentOS中配置LNMP环境打开提示File not found
在centos系统中配置好php环境了,但是发现能运行html页面并不能运行php文件了,这样我就在gg的帮助下一步不解决了,下面来看问题的具体解决过程. 安装之后测试发现,怎么Html能运行 ...
- GCD的多线程实现方式,线程和定时器混合使用
GCD (Grand Central Dispatch) 性能更好,追求性能的话 1.创建一个队列 //GCD的使用 //创建一个队列 dispatch_queue_t queue = dispatc ...
- MySQL中like的使用方法
Like的运用场合主要在模糊查询的时候,一般以查询字符串居多,这里据一些例子来说他的一般用法: <1>查询name字段中包含有“明”字的:例 select * from table1 wh ...
- (转)xcode5.0.2下国际化图文解说
原文:http://blog.csdn.net/dragoncheng/article/details/6703311 xcode5.0.2下国际化图文解说 分类: ...
- 自动获取UILabel的宽度高度
在使用UILabel存放字符串时,经常需要获取label的长宽数据,本文列出了部分常用的计算方法. 1.获取宽度,获取字符串不折行单行显示时所需要的长度 CGSize titleSize = [aSt ...
- java字节数组和16进制之间的转换
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ pac ...
- 如何去除内联元素(inline-block元素)之间的间距(转载)
如何去除内联元素(inline-block元素)之间的间距 前几天写一个专题页 div{width:900px;}div a{ display:inline-block; width:300px; ...