poj 1177(线段树扫描线)

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


题意:

给出n个矩形,求出矩形形成的图形的总周长。


思路:

求总周长就是求出图形的水平方向的总周长和数值方向的总周长,这题和poj1151很相似,都是用线段树扫描线的方法实现。

首先计算水平方向的线段,将上底标记为1下底标记为-1,每次扫描时记录每段的标记值,如果这段标记由0变为1,周长加上这段的数值(上底),如果标记由1变为0,周长加上这段数值(下底)。

数值方向上的与水平相同即可。


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<bitset>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<math.h>
#include<map>
using namespace std;
const int maxn=5005;
int sumtree[4*maxn];
int flag[4*maxn],ff[4*maxn];
int X[4*maxn],Y[4*maxn],ifcover[4*maxn];
struct line
{
	int x1,x2,y;
	int mark;
	line(int a,int b,int c,int d):x1(a),x2(b),y(c),mark(d){}
	line(){}
	 bool operator <(const line &S) const
    {
        return y<S.y;
    }
}l1[maxn*4],l2[maxn*4];

bool cmp(line a,line b)
{
	if(a.y<b.y)return true;
	return false;
}
int n;
int searchx(int x,int len)
{
	int l=1,r=len;
	while(l<=r)
	{
		int mid=(l+r)/2;
		if(X[mid]==x)
			return mid;
		if(X[mid]<x)
			l=mid+1;
		else r=mid;
	}
	return l;
}
int searchy(int x,int len)
{
	int l=1,r=len;
	while(l<=r)
	{
		int mid=(l+r)/2;
		if(Y[mid]==x)
			return mid;
		if(Y[mid]<x)
			l=mid+1;
		else r=mid;
	}
	return l;
}
int change(int node,int start,int end,int left,int right,int mark,int kk)
{
	if(left<=start&&right>=end)
	{
		if(ff[node]!=0)
		{
			flag[node*2]+=ff[node];
			flag[node*2+1]+=ff[node];
			ifcover[node*2]+=ff[node];
			ifcover[node*2+1]+=ff[node];
			ff[node*2]+=ff[node];
			ff[node*2+1]+=ff[node];
			ff[node]=0;
		}
		if((ifcover[node]==0&&mark==1)||(ifcover[node]==1&&mark==-1))
		{
		if(flag[node])
		{
			flag[node]+=mark;
			ff[node]+=mark;
			ifcover[node]+=mark;
			if(!flag[node])
			{
				if(kk==1)
				return X[end+1]-X[start];
				else return Y[end+1]-Y[start];
			}
			else return 0;
		}
		else 
		{
			flag[node]+=mark;
			ff[node]+=mark;
			ifcover[node]+=mark;
			if(flag[node])
			{
				if(kk==1)
				return X[end+1]-X[start];
				else return Y[end+1]-Y[start];
			}
			else return 0;
		}
		}
		else
		{
			flag[node]+=mark;
			if(start==end)
			{
				ff[node]+=mark;
				ifcover[node]+=mark;
				return 0;
			}
			else
			{
				ifcover[node]+=mark;
				int mid=(start+end)/2;
				return change(node*2,start,mid,left,right,mark,kk)+change(node*2+1,mid+1,end,left,right,mark,kk);
			}
		}
	}
	if(ff[node]!=0)
	{
		flag[node*2]+=ff[node];
		flag[node*2+1]+=ff[node];
		ifcover[node*2]+=ff[node];
		ifcover[node*2+1]+=ff[node];
		ff[node*2]+=ff[node];
		ff[node*2+1]+=ff[node];
		ff[node]=0;
	}
	int sum=0;
	int mid=(start+end)/2;
	if(left<=mid) sum+=change(node*2,start,mid,left,right,mark,kk);
	if(right>mid) sum+=change(node*2+1,mid+1,end,left,right,mark,kk);
	ifcover[node]+=mark;
	return sum;
}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int kk=1;
	while(scanf("%d",&n)!=EOF)
	{
		int num=0;
		memset(sumtree,0,sizeof(sumtree));
		memset(flag,0,sizeof(flag));
		memset(ff,0,sizeof(ff));
		memset(X,0,sizeof(X));
		memset(Y,0,sizeof(Y));
		memset(ifcover,0,sizeof(ifcover));
		for(int i=0;i<n;i++)
		{
			int x1,x2,y1,y2;
			scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
			l1[++num]=line(x1,x2,y1,1);
			l2[num]=line(y1,y2,x1,1);
			Y[num]=y1;
			X[num]=x1;
			l1[++num]=line(x1,x2,y2,-1);
			l2[num]=line(y1,y2,x2,-1);
			Y[num]=y2;
			X[num]=x2;
		}
		sort(X+1,X+num+1);
		sort(Y+1,Y+num+1);
		sort(l1+1,l1+num+1);
		sort(l2+1,l2+num+1);
		int xnum=1;
		for(int i=2; i<=num; i++)
            if(X[i]!=X[i-1]) X[++xnum]=X[i];
		int ynum=1;
		for(int i=2; i<=num; i++)
            if(Y[i]!=Y[i-1]) Y[++ynum]=Y[i];
		int ans=0;
		for(int i=1;i<=num;i++)
		{
			int lx=searchx(l1[i].x1,xnum),rx=searchx(l1[i].x2,xnum)-1;
			int cha=change(1,1,xnum,lx,rx,l1[i].mark,1);
			ans+=cha;

		}
		memset(sumtree,0,sizeof(sumtree));
		memset(flag,0,sizeof(flag));
		memset(ff,0,sizeof(ff));
		memset(ifcover,0,sizeof(ifcover));
		for(int i=1;i<=num;i++)
		{
			int ly=searchy(l2[i].x1,ynum),ry=searchy(l2[i].x2,ynum)-1;
			int cha=change(1,1,ynum,ly,ry,l2[i].mark,2);
			ans+=cha;
		}
        printf("%d\n",ans);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值