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

hdu1828 Picture

时间:2015-05-17 21:58:45      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3215    Accepted Submission(s): 1679


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

Please process to the end of file.
 

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



轮廓线确实有些屌………………………………

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
int hs[10010];
struct Tree
{
	int l,r,num,cnt,val;
	bool flag_l,flag_r;
}tree[10000<<2];
void create(int l,int r,int k)
{
	tree[k].l=l;
	tree[k].r=r;
	tree[k].val=0;
	tree[k].num=tree[k].cnt=0;
	tree[k].flag_l=tree[k].flag_r=0;
	if(l+1==r)
		return;
	int m=l+r>>1;
	create(l,m,k<<1);
	create(m,r,k<<1|1);
}
void update(int l,int r,int k,int flag)
{
	if(tree[k].l==l&&tree[k].r==r)
	{
		tree[k].num+=flag;
		if(tree[k].num==0)
		{
			tree[k].val=l+1==r?0:tree[k<<1].val+tree[k<<1|1].val;
			if(l+1==r)
			{
				tree[k].cnt=0;
				tree[k].flag_l=tree[k].flag_r=0;
			}
			else
			{
				tree[k].cnt=tree[k<<1].cnt+tree[k<<1|1].cnt;
				if(tree[k<<1].flag_r&&tree[k<<1|1].flag_l)
					tree[k].cnt--;
				tree[k].flag_l=tree[k<<1].flag_l;
				tree[k].flag_r=tree[k<<1|1].flag_r;
			}
		}
		else
		{
			tree[k].cnt=1;
			tree[k].val=hs[r]-hs[l];
			tree[k].flag_l=tree[k].flag_r=1;
		}
		return;
	}
	int m=tree[k].l+tree[k].r>>1;
	if(r<=m)
		update(l,r,k<<1,flag);
	else if(l>=m)
		update(l,r,k<<1|1,flag);
	else
	{
		update(l,m,k<<1,flag);
		update(m,r,k<<1|1,flag);
	}
	if(tree[k].num==0)
	{
		tree[k].val=tree[k<<1].val+tree[k<<1|1].val;
		tree[k].cnt=tree[k<<1].cnt+tree[k<<1|1].cnt;
		if(tree[k<<1].flag_r&&tree[k<<1|1].flag_l)
			tree[k].cnt--;
		tree[k].flag_l=tree[k<<1].flag_l;
		tree[k].flag_r=tree[k<<1|1].flag_r;
	}
}
struct Seg
{
	int flag,l,r,y;
	bool operator <(Seg one)const
	{
		return y<one.y;
	}
}seg[10010];
int main()
{
	int n;
	while(cin>>n)
	{
		vector<int>box;
		for(int i=0;i<n;i++)
		{
			int x1,y1,x2,y2;
			cin>>x1>>y1>>x2>>y2;
			seg[i<<1].l=seg[i<<1|1].l=x1;
			seg[i<<1].r=seg[i<<1|1].r=x2;
			seg[i<<1].y=y1;
			seg[i<<1|1].y=y2;
			seg[i<<1].flag=1;
			seg[i<<1|1].flag=-1;
			box.push_back(x1);
			box.push_back(x2);
		}
		sort(box.begin(),box.end());
		box.erase(unique(box.begin(),box.end()),box.end());
		for(int i=0;i<box.size();i++)
			hs[i+1]=box[i];
		n*=2;
		sort(seg,seg+n);
		int ans=0,p=0;
		create(1,box.size(),1);
		for(int i=0;i<n;i++)
		{
			int l=1+lower_bound(box.begin(),box.end(),seg[i].l)-box.begin();
			int r=1+lower_bound(box.begin(),box.end(),seg[i].r)-box.begin();
			update(l,r,1,seg[i].flag);
			ans+=abs(tree[1].val-p);
			p=tree[1].val;
			if(i!=n-1)
				ans+=(seg[i+1].y-seg[i].y)*tree[1].cnt*2;
		}
		printf("%d\n",ans);
	}
}


hdu1828 Picture

标签:

原文地址:http://blog.csdn.net/stl112514/article/details/45795897

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