POJ 1177-Picture(线段树+离散化+扫描线)

Picture
Time Limit: 2000MS Memory Limit: 10000K
Total Submissions: 12672 Accepted: 6699

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

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top


和HDU 1542类似,唯一的不同便是这题求的是周长,但是考察点是类似的,毫无疑问需要扫描线搞搞。

这里就详细介绍一下关于扫描线的具体代码。更多的讲解看一篇帮助理解的博客:大牛的扫描线

首先建立一个node结构体用来保存每条扫描线,node中有信息:

l: 表示扫描线的左端x坐标

r:表示扫描线的右端x坐标

h: 表示扫描线的高度

d: 为1或-1,标记扫描线是矩形的上位还是下位边.

我们首先读入所有矩形的信息,每读入一个矩形信息我们就更新两条扫描线,并且把矩形的两个端点x坐标放入X[MAXN]数组中,

然后我们对node和X都排序,node按h值从小到大排序.

X按从小到大排序.

然后我们在X的本地数组内,对X去重,并且用k表示一共有多少个X.

当我们需要找到第i个区域的两端点坐标时,只需要X[i]和X]i+1].


本题中因为要求周长,横向扫和纵向扫是一样的,本题采用的纵向扫。

扫描线保存4个信息:该线的横坐标、该线的上下定点、该线是入边还是出边(矩形的左边还是右边)

线段树保存5个信息:当前节点被覆盖的长度、当前节点少被完全覆盖多少次、当前节点的最左端和最右段分别是否被覆盖、当前节点有多少个不续的覆盖区域

剩下的就是将纵坐标离散化一波(因为y很大),然后二分查找即可

要注意的是线段树的叶子节点只有len-1个(len为y离散化后的点数),很好理解,因为你线段树上点的意义是

y[x]与y[x+1]这一段距离,故len个点肯定是len-1个区间啦,剩下的就具体看代码吧

#include<map>  
#include<stack>  
#include<queue>  
#include<vector>  
#include<math.h>  
#include<stdio.h>  
#include<iostream>  
#include<string.h>  
#include<stdlib.h>  
#include<algorithm>  
using namespace std;  
typedef __int64  ll;  
#define inf 1000000000  
#define mod 1000000007 
#define  maxn  100006
#define  lowbit(x) (x&-x)  
#define  eps 1e-10  
struct node//扫描线
{
	int x,h,d,flag;
}a[maxn];
int pos[maxn];
struct node1 
{
	int num,lc,rc,cnt,s1;
}t[maxn];
bool comp(node a,node b)
{
	if(a.x<b.x || a.x==b.x && a.flag==1 && b.flag==1)//x相等时矩形入边的扫描线排序是一定要放在前边
		return 1;
	return 0;
}
int Bsearch(int l,int r,int x)
{
	int m=(l+r)/2;
	if(pos[m]<x)
		return Bsearch(m+1,r,x);
	if(pos[m]>x)
		return Bsearch(l,m-1,x);
	return m;
}
void work(int l,int r,int id)
{
	if(t[id].cnt>=1)
		t[id].s1=pos[r+1]-pos[l],t[id].num=1,t[id].lc=t[id].rc=1;
	else
		t[id].s1=t[id*2].s1+t[id*2+1].s1,t[id].num=0,t[id].lc=t[id].rc=0;
	if(t[id].cnt==0 && l!=r)
	{
		if(t[id*2].lc==1)
			t[id].lc=1;
		if(t[id*2+1].rc==1)
			t[id].rc=1;
		t[id].num=t[id*2].num+t[id*2+1].num;//当前节点不连续覆盖区域的个数等于两个儿子不连续覆盖区域的个数之和
		if(t[id*2].rc && t[id*2+1].lc)//如果左儿子的右端和右儿子的左端连续,那么当前节点不连续覆盖区域的个数就要-1
			t[id].num--;
	}
}
void updata(int id,int l,int r,int a,int b,int x)
{
	if(l>=a && r<=b)
	{
		t[id].cnt+=x;
		work(l,r,id);
		return;
	}
	int m=(l+r)/2;
	if(a<=m)
		updata(id*2,l,m,a,b,x);
	if(b>m)
		updata(id*2+1,m+1,r,a,b,x);
	work(l,r,id);
}
int  main(void)
{
	int ax,ay,bx,by,i,j,len,sum,n;
	while(scanf("%d",&n)!=EOF)
	{
		len=1;sum=0;
		for(i=1;i<=n;i++)
		{
			scanf("%d%d%d%d",&ax,&ay,&bx,&by);
			a[2*i-1].d=a[i*2].d=ay;
			a[2*i-1].h=a[i*2].h=by;
			a[2*i-1].x=ax;a[2*i].x=bx;
			a[2*i-1].flag=1;a[i*2].flag=-1;
			pos[i*2-1]=ay;pos[i*2]=by;
		}
		n=n*2;
		sort(pos+1,pos+n+1);
		sort(a+1,a+n+1,comp);
		for(i=2;i<=n;i++)
			if(pos[i]!=pos[i-1])
				pos[++len]=pos[i];
		for(i=1;i<=n;i++)
		{
			ax=Bsearch(1,len,a[i].d);
			ay=Bsearch(1,len,a[i].h);
			node1 tmp=t[1];
			updata(1,1,len-1,ax,ay-1,a[i].flag);//len个坐标,说明有len-1个区域
			sum+=abs(t[1].s1-tmp.s1)+tmp.num*2*(a[i].x-a[i-1].x);
			//每遇到一个扫描线,便拿更新后节点覆盖的长度-更新前节点被覆盖的长度
			//并加上两条扫描线之间的距离*2*不连续覆盖的区域个数
		}
		printf("%d\n",sum);
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值