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 ...
随机推荐
- 基于K2 BPM平台,中原地产实现了从2个人到5万多人的跨越
演讲人:吴付文 中原地产CIO 点击这里查看中原地产怎么使用BPM实现业绩的飞跃式发展.
- JAVA每日一记
1.两个最基本的java回收算法:复制算法和标记清理算法 复制算法:两个区域A和B,初始对象在A,继续存活的对象被转移到B.此为新生代最常用的算法 ...
- tableView中的“点击加载更多”点击不到
假设当前的tableView是_tableView,则可以这样设置 _tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0); 该属性用于设置当 ...
- POJ题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)
本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...
- 设置navigation baritem方法
UIButton *RightBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; [RightBtn setImage:[UIImage im ...
- android unique identifier
android get device mac address programmatically http://android-developers.blogspot.jp/2011/03/identi ...
- hdu 2058
PS:TLE了N次...虽然结果对了...后来看了公式才知道要枚举项数才行... 代码: #include "stdio.h"#include "math.h" ...
- codeforces Educational Codeforces Round 16-E(DP)
题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...
- Java 集合深入理解(11):LinkedList
点击查看 Java 集合框架深入理解 系列, - ( ゜- ゜)つロ 乾杯~ 今天心情鱼肚白,来学学 LinkedList 吧! 日常开发中,保存一组数据使用的最多的就是 ArrayList, 其次就 ...
- CODEVS1001 舒适的路线 (并查集)
对所有边从大到小排序,枚举最大边,O(m)验证,用并查集维护图是否联通. program CODEVS1001; ; maxn=; INF=; type arr=record u,v,w:int64; ...