POJ 1177 Picture

105 篇文章 0 订阅
12 篇文章 0 订阅

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

扫描线+线段树~

扫描线经典题目矩形周长并~

我们将每个矩形拆成两条竖着的边,左面的flag=1右面的flag=-1,表示加入和删除。然后用线段树维护目前的横向线段数以及纵向总长度,线段树a[k]包含l、r(左右边界,[l,mid],[mid,r]要包含mid),num(线段数量),len(覆盖长度),cover(区间是否被覆盖),ls、rs(左节点左和右节点右是否被覆盖)。

要注意的是这里l==r-1时要返回。

(因为读入时1.txt写成了1,txt检查了一晚上+一上午……)

(POJ挂掉了……程序没有测过~)


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int n,x1,x2,y1,y2,cnt,xx[10001],len,now,ans,las;

struct node{
	int x,y1,y2,flag;
}l[10001];

struct node1{
	int l,r,num,len,cover;
	bool ls,rs;
}a[10001<<2];

bool operator < (node u,node v)
{
	return u.x==v.x ? u.flag>v.flag:u.x<v.x;
}

int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0' && ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return x*f;
}

void build(int l,int r,int k)
{
	a[k].l=l;a[k].r=r;
	if(l==r-1) return;
	int mid=l+r>>1;
	build(l,mid,k<<1);build(mid,r,k<<1|1);
}

void update(int k)
{
	if(a[k].cover>0)
	{
		a[k].num=a[k].ls=a[k].rs=1;
		a[k].len=xx[a[k].r]-xx[a[k].l];
		return;
	}
	if(a[k].l==a[k].r-1)
	{
		a[k].num=a[k].len=a[k].ls=a[k].rs=a[k].cover=0;return;
	}
	a[k].ls=a[k<<1].ls;a[k].rs=a[k<<1|1].rs;
	a[k].len=a[k<<1].len+a[k<<1|1].len;
	a[k].num=a[k<<1].num+a[k<<1|1].num-(a[k<<1].rs&a[k<<1|1].ls);
}

void chan(int k,int z)
{
	int ll=a[k].l,r=a[k].r,mid=ll+r>>1;
	if(xx[ll]>=l[z].y1 && xx[r]<=l[z].y2)
	{
		a[k].cover+=l[z].flag;update(k);return;
	}
	if(ll==r-1) return;
	if(xx[mid]>=l[z].y1) chan(k<<1,z);
	if(xx[mid]<l[z].y2) chan(k<<1|1,z);
	update(k);
}

int main()
{
	n=read();
	for(int i=1;i<=n;i++)
	{
		x1=read(),y1=read(),x2=read(),y2=read();
		xx[++cnt]=y1;l[cnt]=(node){x1,y1,y2,1};
		xx[++cnt]=y2;l[cnt]=(node){x2,y1,y2,-1};
	}
	sort(xx+1,xx+cnt+1);
	len=unique(xx+1,xx+cnt+1)-xx-1;
	sort(l+1,l+cnt+1);
	build(1,len,1);
	for(int i=1;i<=cnt;i++)
	{
		chan(1,i);
		if(i>1) ans+=now*2*(l[i].x-l[i-1].x);
		ans+=abs(a[1].len-las);
		las=a[1].len;now=a[1].num;
	}
	printf("%d\n",ans);
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值