码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1828 Picture(线段树,扫描线)

时间:2017-12-14 14:50:52      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:horizon   totally   ati   number   space   photo   algo   stand   ast   

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.

InputYour 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. 

Please process to the end of file.OutputYour 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

题解:
  扫描线,横着一次,竖着一次,就可以了,
  每次是局对值相减,维护一个l,r(这样不需要坐标转化),然后一个cnt表示这条线段被覆盖了多少次,
  以及该线段多少区域,被覆盖了多少次,向上更新即可。
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstdio>

#define N 5007
using namespace std;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch>‘9‘||ch<‘0‘){if (ch==‘-‘) f=-1;ch=getchar();}
	while(ch<=‘9‘&&ch>=‘0‘){x=(x<<3)+(x<<1)+ch-‘0‘;ch=getchar();}
	return x*f;
}

int n,top,ans,minn,maxx;
struct Node
{
	int a,b,c,d;
}a[N],cl[N*2];

struct date
{
	int l,r,sum,cnt;
	void init(){sum=cnt=0;}
}tr[40007];

void build(int p,int l,int r)
{
	tr[p].init();tr[p].l=l,tr[p].r=r;
	if (l==r) return;
	int mid=(l+r)>>1;
	build(p<<1,l,mid),build(p<<1|1,mid+1,r);
}
bool cmp(Node x,Node y){return x.a<y.a;}
void update(int p)
{
	if (tr[p].cnt) tr[p].sum=tr[p].r-tr[p].l+1;
	else tr[p].sum=tr[p<<1].sum+tr[p<<1|1].sum;
}
void change(int p,int l,int r,int x,int y,int z)
{
	if (l==x&&y==r)
	{
		tr[p].cnt+=z;
		update(p);
		return ;
	}
	int mid=(l+r)>>1;
	if (y<=mid) change(p<<1,l,mid,x,y,z);
	else if (x>mid) change(p<<1|1,mid+1,r,x,y,z);
	else change(p<<1,l,mid,x,mid,z),change(p<<1|1,mid+1,r,mid+1,y,z);
	update(p);
}
void solve()
{
	build(1,minn,maxx);
	sort(cl+1,cl+top+1,cmp);
	int last=0;
	for (int i=1;i<=top;i++)
	{
		change(1,minn,maxx,cl[i].b,cl[i].c-1,cl[i].d);
		ans+=abs(tr[1].sum-last);
		last=tr[1].sum;
	}
}
int main()
{
	while(~scanf("%d",&n))
	{
		minn=10000,maxx=-10000;
		for (int i=1;i<=n;i++)
		{
			a[i].a=read(),a[i].b=read(),a[i].c=read(),a[i].d=read();	
			minn=min(minn,a[i].a),minn=min(minn,a[i].b),minn=min(minn,a[i].c),minn=min(minn,a[i].d);
			maxx=max(maxx,a[i].a),maxx=max(maxx,a[i].b),maxx=max(maxx,a[i].c),maxx=max(maxx,a[i].d);
		}
		for (int i=1;i<=n;i++)
		{
			cl[++top].a=a[i].b;
			cl[top].b=a[i].a,cl[top].c=a[i].c;
			cl[top].d=1;
			cl[++top].a=a[i].d;
			cl[top].b=a[i].a,cl[top].c=a[i].c;
			cl[top].d=-1;
		}
		solve(),top=0;
		for (int i=1;i<=n;i++)
		{
			cl[++top].a=a[i].a;
			cl[top].b=a[i].b,cl[top].c=a[i].d;
			cl[top].d=1;
			cl[++top].a=a[i].c;
			cl[top].b=a[i].b,cl[top].c=a[i].d;
			cl[top].d=-1;
		}
		solve();
		printf("%d\n",ans);
	}
}

  

hdu 1828 Picture(线段树,扫描线)

标签:horizon   totally   ati   number   space   photo   algo   stand   ast   

原文地址:http://www.cnblogs.com/fengzhiyuan/p/8037302.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!