POJ 1177 Picture(线段树:扫描线求轮廓周长)
题目链接:http://poj.org/problem?id=1177
题目大意:若干个矩形,求这些矩形重叠形成的图形的轮廓周长。
解题思路:这里引用一下大牛的思路:kuangbin
总体思路:
1.沿X轴离散化建树
2.按Y值从小到大排序平行与X轴的边,然后顺序处理
如果遇到矩形下面那条边则插入到线段树中,遇到矩形上面的边则将相应的边删除掉
根据线段树当前的状态统计长度
第二点是本题的核心思想,偶再举个例:

第一次求出的部分很好理解.
第二次求出的为什么会少了中间那部分.那是因为插入的新线段覆盖了第一条,此时线段树返回的长度是新的那一条的长度,将这个值再减去上次的就少了中间那部分
第三次因为是矩形的上边,所以要删除在那条长的线段.此时的线段树返回的则是第一次的长度,将此值减去第二次返回值,再取其负值就是红色X轴那部分了
最后那条X轴的,再补上就行了。
我写的稍微有点不同,不过大概也就是这个思路了。总结一下就是矩形下位边cnt+1,上位边cnt-1,求tree[1].sum,这些步骤跟求矩形面积并是一样的。
然后按照扫描线从小到大一次跟新,ans+=abs(上一次的测度-更新后的测度)。
然后就按x,y方向扫描线各求一遍,加在一起就是总的周长。(好像还是有点问题,就是重边会出错,不会改啊!!!算了,等以后变强了再来改0 0)
这题我没有离散化,因为数据不是很大也没有小数所以就懒得写了。。。
代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<string>
#define LC(a) (a<<1)
#define RC(a) (a<<1|1)
#define MID(a,b) ((a+b)>>1)
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int N=1e4+; struct Node{
int l,r,cnt,sum;//sum为测度
}tree[N**]; struct node{
int l,r,h,flag;
node(){}
node(double a,double b,double c,double d){
l=a;r=b;h=c;flag=d;
}
}a[N],b[N]; bool cmp(node a,node b){
return a.h<b.h;
}
//因为每条线段对应删除的线段是严格出现的,不需要Lazy也就不需要pushdown()
void pushup(int p) {
if(tree[p].cnt>)
tree[p].sum=tree[p].r-tree[p].l+;
else if(tree[p].l==tree[p].r)
tree[p].sum=;
else
tree[p].sum=tree[LC(p)].sum+tree[RC(p)].sum;
} void build(int p,int l,int r){
tree[p].l=l;
tree[p].r=r;
if(l==r){
tree[p].cnt=tree[p].sum=;
return;
}
build(LC(p),l,MID(l,r));
build(RC(p),MID(l,r)+,r);
pushup(p);
} void update(int p,int l,int r,int cnt){
if(l>tree[p].r||r<tree[p].l)
return;
if(l<=tree[p].l&&r>=tree[p].r&&tree[p].cnt!=-){
tree[p].cnt+=cnt;
pushup(p);
return;
}
update(LC(p),l,r,cnt);
update(RC(p),l,r,cnt);
pushup(p);
} int main(){
int n;
while(~scanf("%d",&n)){
int m1=,m2=;
for(int i=;i<=n;i++){
int x1,x2,y1,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x1+=N,x2+=N,y1+=N,y2+=N;//防止x,y出现负数
a[++m1]=node(x1,x2,y1,);
b[m1]=node(y1,y2,x1,);
a[++m1]=node(x1,x2,y2,-);
b[m1]=node(y1,y2,x2,-);
}
sort(a+,a++m1,cmp);
sort(b+,b++m1,cmp);
//自下往上扫描一遍
build(,,*N-);
int ans=;
for(int i=;i<=m1;i++){
int last=tree[].sum;
int l=a[i].l;
int r=a[i].r-;
update(,l,r,a[i].flag);
ans+=abs(tree[].sum-last);
}
//自左往右扫描一遍
build(,,*N-);
for(int i=;i<=m1;i++){
int last=tree[].sum;
int l=b[i].l;
int r=b[i].r-;
update(,l,r,b[i].flag);
ans+=abs(tree[].sum-last);
}
printf("%d\n",ans);
}
return ;
}
POJ 1177 Picture(线段树:扫描线求轮廓周长)的更多相关文章
- poj 1177 --- Picture(线段树+扫描线 求矩形并的周长)
题目链接 Description A number of rectangular posters, photographs and other pictures of the same shape a ...
- poj 1177 Picture (线段树 扫描线 离散化 矩形周长并)
题目链接 题意:给出n个矩形,每个矩形给左下 和 右上的坐标,求围成的周长的长度. 分析: 首先感谢大神的博客,最近做题经常看大神的博客:http://www.cnblogs.com/kuangbin ...
- POJ 1177 Picture(线段树 扫描线 离散化 求矩形并面积)
题目原网址:http://poj.org/problem?id=1177 题目中文翻译: 解题思路: 总体思路: 1.沿X轴离散化建树 2.按Y值从小到大排序平行与X轴的边,然后顺序处理 如果遇到矩形 ...
- HDU 1828“Picture”(线段树+扫描线求矩形周长并)
传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ...
- HDU 1828 / POJ 1177 Picture --线段树求矩形周长并
题意:给n个矩形,求矩形周长并 解法:跟求矩形面积并差不多,不过线段树节点记录的为: len: 此区间线段长度 cover: 此区间是否被整个覆盖 lmark,rmark: 此区间左右端点是否被覆盖 ...
- Picture POJ - 1177 (线段树-扫描线)
A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wa ...
- hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- POJ 1177 Picture(线段树周长并)
描述 A number of rectangular posters, photographs and other pictures of the same shape are pasted on ...
- hdu1542 Atlantis 线段树--扫描线求面积并
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...
- hdu1828 Picture(线段树+扫描线+矩形周长)
看这篇博客前可以看一下扫描线求面积:线段树扫描线(一.Atlantis HDU - 1542(覆盖面积) 二.覆盖的面积 HDU - 1255(重叠两次的面积)) 解法一·:两次扫描线 如图我们可以 ...
随机推荐
- 使用javaScript和JQuery制作经典面试题:光棒效果
使用javaScript与jQuery添加CSS样式的区别和步骤 使用javaScript制作光棒效果 --首先是javaScript <script> $(function () { v ...
- wildcard ,notdir ,patsubst ,obj=$(dir:%.c=%.o)
Makefile中wildcard的介绍 在Makefile规则中,通配符会被自动展开.但在变量的定义和函数引用时,通配符将失效.这种情况下如果需要通配符有效,就需要使用函数“wildcard”,它的 ...
- IE下textarea去除回车换行符
在textarea中回车,会产生转义字符\r\n,有些时候我们不需要这两个转移字符,也就是清空textarea.下面的方法并不是清空,但是能够起到差不多的效果. 如果在textarea中按回车,内容提 ...
- HDU1255 扫描线 矩形交面积 离散化
覆盖的面积 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- idea 创建多模块时模块类无法引入
我的原因是类的位置方的不对,由于刚搭建的项目,本来只想做个测试,就直接在java下创建类,然而这居然是个深坑,模块引入了也无法引入这个模块的类. 解决方法:创建com.***.***包后的类可以正常引 ...
- springboot整合thumbnailator实现图片压缩
springboot整合thumbnailator实现图片压缩 前言 最近由于首页产品列表图片显示太慢,经过研究发现是用户上传的图片太大. 针对这个问题,想到的解决方案是: 1. 产品上传时,限定图片 ...
- [LeetCode] 15. 3Sum ☆☆
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- js addDays ,addYears
//添加天 Date.prototype.addDays = function (d) { this.setDate(this.getDate() + d); }; //添加周 Date.protot ...
- UVA 808 Bee Breeding (坐标法)
https://vjudge.net/problem/UVA-808 #include<cmath> #include<cstdio> #include<algorith ...
- 【Codeforces629C】Famil Door and Brackets [DP]
Famil Door and Brackets Time Limit: 20 Sec Memory Limit: 512 MB Description Input Output Sample Inp ...