概述
Language:
Picture
Description
A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter.
Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1. The corresponding boundary is the whole set of line segments drawn in Figure 2. The vertices of all rectangles have integer coordinates. Input
Your program is to read from standard input. The first line contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate.
0 <= number of rectangles < 5000 All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area. Output
Your program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.
Sample Input 7 -15 0 5 10 -5 8 20 25 15 -4 24 14 0 -6 16 4 2 15 10 22 30 10 36 20 34 0 40 16 Sample Output 228 Source
IOI 1998
|
题意:求矩阵并的周长
思路:参考kuangbin大大的博客的,上面的分析很详细(传送门),详见代码:
ps: poj数据很水,hdu1828数据较强。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define L(x) (x<<1)
#define R(x) (x<<1|1)
const int MAXN=10000+100;
int n,edge_cnt;
int y[MAXN];
struct node
{
bool lc,rc; //记录边界是否被覆盖
int l,r,len;
int lf,rf,cnt,lazy; //cnt记录当前区域沿x轴有多少对需要计算的边
}segtree[MAXN<<2];
struct Line
{
int flag;//flag=-1,表示是正方形的左边;flag=1,表示正方形的右边
int x,y1,y2;
}line[MAXN];
bool cmp(Line a,Line b)
{
return a.x<b.x;
}
void build(int rt,int l,int r)
{
segtree[rt].l=l; segtree[rt].r=r;
segtree[rt].lf=y[l]; segtree[rt].rf=y[r];
segtree[rt].lc=segtree[rt].rc=0;
segtree[rt].lazy=segtree[rt].len=segtree[rt].cnt=0;
if(l+1==r)
return ;
int mid=(l+r)>>1;
build(L(rt),l,mid); build(R(rt),mid,r);
}
void cal(int rt)
{
if(segtree[rt].lazy<0){
segtree[rt].len=segtree[rt].rf-segtree[rt].lf;
segtree[rt].cnt=1; // 当区域被覆盖,那么只能有一对,即区间左右端
segtree[rt].lc=segtree[rt].rc=1;
return ;
}
if(segtree[rt].l+1 == segtree[rt].r ){
segtree[rt].len=0; segtree[rt].cnt=0;
segtree[rt].lc=segtree[rt].rc=0;
}
else {
segtree[rt].len=segtree[L(rt)].len+segtree[R(rt)].len;
segtree[rt].cnt=segtree[L(rt)].cnt+segtree[R(rt)].cnt;
segtree[rt].lc=segtree[L(rt)].lc; segtree[rt].rc=segtree[R(rt)].rc;
if(segtree[L(rt)].rc && segtree[R(rt)].lc) segtree[rt].cnt--; //当两个子区间边界处重合,那么对数-1,即只有左子区间的左端和右子区间的右端形成的一对
}
}
void update(int rt,Line e)
{
if(segtree[rt].lf == e.y1 && segtree[rt].rf == e.y2){
segtree[rt].lazy+=e.flag; cal(rt);
return ;
}
if(e.y2<=segtree[L(rt)].rf) update(L(rt),e);
else if(e.y1>=segtree[R(rt)].lf) update(R(rt),e);
else {
Line temp=e; temp.y2=segtree[L(rt)].rf;
update(L(rt),temp);
temp=e; temp.y1=segtree[R(rt)].lf;
update(R(rt),temp);
}
cal(rt);
}
int main()
{
//freopen("text.txt","r",stdin);
int x1,y1,x2,y2;
while(~scanf("%d",&n)){
edge_cnt=0;
for(int i=1;i<=n;i++){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
line[++edge_cnt].x=x1; line[edge_cnt].flag=-1;
line[edge_cnt].y1=y1; line[edge_cnt].y2=y2; y[edge_cnt]=y1;
line[++edge_cnt].x=x2; line[edge_cnt].flag=1;
line[edge_cnt].y1=y1; line[edge_cnt].y2=y2; y[edge_cnt]=y2;
}
sort(line+1,line+edge_cnt+1,cmp);
sort(y+1,y+edge_cnt+1);
n=unique(y+1,y+edge_cnt+1)-(y+1);
build(1,1,n);
int ans=0,last=0;//用last记录上次的长度
for(int i=1;i<edge_cnt;i++){
update(1,line[i]);
ans+=segtree[1].cnt*2*(line[i+1].x-line[i].x);
ans+=abs(segtree[1].len-last);
last=segtree[1].len;
}
ans+=segtree[1].len;
printf("%dn",ans);
}
return 0;
}
最后
以上就是俭朴小兔子为你收集整理的poj1177 Picture 扫描线+线段树+离散化的全部内容,希望文章能够帮你解决poj1177 Picture 扫描线+线段树+离散化所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复