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

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

代码如下:

# 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. JavaScript中Date的一些细节

    对于开发人员来说,Date有时候或许会很重要,我们可以通过new Date()来创建一个日期对象.例如: var start = new Date(), //获取当前时间 today = new Da ...

  2. JDBC体会

    1.把mysql-connector XXXX版本导入buildpath 2.通过DriverManager 的getConnection 方法获得一个Connnection引用,方法的参数是 url ...

  3. 新版的tomcat里面get请求通过http协议的时候似乎默认是UTF-8的编码了吗?

    不在servler.xml的connector中添加URICoding=“UTF-8”,使用默认值一样没有乱码,而添加URICoding=“iso-8859-1”就是乱码了. POST请求还是用iso ...

  4. jQuery 常用动画

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. GCD的多线程实现方式,线程和定时器混合使用

    GCD (Grand Central Dispatch) 性能更好,追求性能的话 1.创建一个队列 //GCD的使用 //创建一个队列 dispatch_queue_t queue = dispatc ...

  6. eclipse安装spring和hibernate插件经验

    看网上的教程有时候不一定凑效,我是自己摸索的(看过尚硅谷的SSH视频),很多时候会安装不成功(或者安装结果与视频讲述不一致),但是安装过后,查看eclispe插件,会发现已经安装了(springIDE ...

  7. BZOJ 1657 奶牛的歌声

    单调栈. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm&g ...

  8. 【LeetCode OJ】Pascal's Triangle II

    Problem Link: http://oj.leetcode.com/problems/pascals-triangle-ii/ Let T[i][j] be the j-th element o ...

  9. ios oc ui 路径和颜色设置--崩溃解决方案

    - (id)init{ self = [super init]; if (self) { _lineColor = CGColorCreateCopy([UIColor whiteColor].CGC ...

  10. iOS-UITableView性能优化

    使用不透明视图.     不透明的视图可以极大地提高渲染的速度.因此如非必要,可以将table cell及其子视图的opaque属性设为YES(默认值).其中的特例包括背景色,它的alpha值应该为1 ...