题目大意:给若干个矩形,求轮廓边长。

题目分析:与求面积类似。按从下往上扫描,仍然是底边添加,上边删除。但要同时维护竖边的数目,每次扫描对答案的贡献为扫描线上总覆盖长度的变化量加上竖边的增量。总覆盖长度的变化为正说明下轮廓增加,为负以为着碰到了上轮廓增加。

代码如下:

# 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(扫描线)的更多相关文章

  1. 51nod 1206 && hdu 1828 Picture (扫描线+离散化+线段树 矩阵周长并)

    1206 Picture  题目来源: IOI 1998 基准时间限制:2 秒 空间限制:131072 KB 分值: 160 难度:6级算法题  收藏  关注 给出平面上的N个矩形(矩形的边平行于X轴 ...

  2. HDU 1828 Picture(长方形的周长和)

    HDU 1828 Picture 题目链接 题意:给定n个矩形,输出矩形周长并 思路:利用线段树去维护,分别从4个方向扫一次,每次多一段的时候,就查询该段未被覆盖的区间长度,然后周长就加上这个长度,4 ...

  3. HDU 1828 Picture(线段树扫描线求周长)

    Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  4. HDU 1828“Picture”(线段树+扫描线求矩形周长并)

    传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ...

  5. hdu 1828 Picture(线段树 || 普通hash标记)

    http://acm.hdu.edu.cn/showproblem.php?pid=1828 Picture Time Limit: 6000/2000 MS (Java/Others)    Mem ...

  6. HDU 1828 Picture (线段树+扫描线)(周长并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1828 给你n个矩形,让你求出总的周长. 类似面积并,面积并是扫描一次,周长并是扫描了两次,x轴一次,y ...

  7. POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算

    求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...

  8. (中等) HDU 1828 Picture,扫描线。

    Problem Description A number of rectangular posters, photographs and other pictures of the same shap ...

  9. hdu 1828 Picture(线段树扫描线矩形周长并)

    线段树扫描线矩形周长并 #include <iostream> #include <cstdio> #include <algorithm> #include &l ...

  10. hdu 1828 Picture(线段树,扫描线)

    A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wa ...

随机推荐

  1. PDF创建及动态转换控件程序包ActivePDF Portfolio

    ActivePDF Portfolio是将4个activePDF最优秀的服务器产品捆绑成一个价格适中的控件程序包.它提供了开发一个完整的服务器端的PDF解决方案所需的一切. 具体功能: activeP ...

  2. JS事件大全

    橙色表示“非常常用”  绿色表示“常用” onClick IE3|N2|O3 鼠标点击事件,多用在某个对象控制的范围内的鼠标点击 onDblClick IE4|N4|O 鼠标双击事件 onMouseD ...

  3. bjui简单了解

    bjui官网:http://demo.b-jui.com/ 其他的我就不是很了解,恶心.

  4. ODI 12.1.3发布,提升支持大数据的能力

    此次发布的ODI新版本,目的是更好的支持当前市场上的大数据平台. 大数据基因在不改变ODI工作效率的情况下,ODI增加了越来越多的数据源集成能力.ODI是在Oracle平台上标准的E-LT工具,事实上 ...

  5. php的字符串处理

    字符串处理: strlen("aaa");取字符串的长度 *** strcmp("aaa","aaa");比较两个字符串,相同的话输出0,不 ...

  6. JDK的下载与安装

    一.下载 在Oracle公司的官方网站(www.oracle.com)下载. 二.安装 1.双击运行JDK程序,弹出JDK安装导向窗口,点击“下一步” 2.点击“更改",将安装地址修改为 C ...

  7. iOS对象序列化

    系统对象的归档我就不介绍了,这个不复杂,自己看一下就会了. 我在这里主要介绍自定义对象的归档. Sample.h文件 // //  Sample.h //  Serialization // //   ...

  8. mongodb 几个要注意的问题

    1. moongo db 会尽量将 所有 索引和 热数据 放入内存中来进行比较,从而来获得更好的查询速度,同时,mongodb在写的时候,也是先写入内存,然后定期同步到磁盘上面去,这样可以达成顺序写的 ...

  9. Sql Server REPLACE函数的使用;SQL中 patindex函数的用法

    Sql Server REPLACE函数的使用 REPLACE用第三个表达式替换第一个字符串表达式中出现的所有第二个给定字符串表达式. 语法REPLACE ( ''string_replace1'' ...

  10. IOS之Core Foundation框架和Cocoa Foundation框架的区别

    Core Foundation框架 (CoreFoundation.framework) 是一组C语言接口,它们为iOS应用程序提供基本数据管理和服务功能.下面列举该框架支持进行管理的数据以及可提供的 ...