首页 > 其他 > 详细

poj1151 Atlantis

时间:2015-05-17 18:42:50      阅读:154      评论:0      收藏:0      [点我收藏+]
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 18723   Accepted: 7105

Description

There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.

Input

The input consists of several test cases. Each test case starts with a line containing a single integer n (1 <= n <= 100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0 <= x1 < x2 <= 100000;0 <= y1 < y2 <= 100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area. 
The input file is terminated by a line containing a single 0. Don‘t process it.

Output

For each test case, your program should output one section. The first line of each section must be "Test case #k", where k is the number of the test case (starting with 1). The second one must be "Total explored area: a", where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point. 
Output a blank line after each test case.

Sample Input

2
10 10 20 20
15 15 25 25.5
0

Sample Output

Test case #1
Total explored area: 180.00 

Source

Mid-Central European Regional Contest 2000


以前线段树的写法十分没效率……写了个更屌的

#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;
double hs[210];
struct Tree
{
	int l,r,num;
	double val;
}tree[200<<2];
void create(int l,int r,int k)
{
	tree[k].l=l;
	tree[k].r=r;
	tree[k].val=0;
	tree[k].num=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;
		else
			tree[k].val=hs[r]-hs[l];
		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;
}
struct Seg
{
	int flag;
	double l,r,y;
	bool operator <(Seg one)const
	{
		return y<one.y;
	}
}seg[210];
int main()
{
	int n,cs=0;
	while(cin>>n&&n!=0)
	{
		vector<double>box;
		for(int i=0;i<n;i++)
		{
			double 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);
		double ans=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();
			if(i!=n-1)
			{
				update(l,r,1,seg[i].flag);
				ans+=tree[1].val*(seg[i+1].y-seg[i].y);
			}
		}
		printf("Test case #%d\n",++cs);
		printf("Total explored area: %.2f\n\n",ans);
	}
}

poj1151 Atlantis

原文:http://blog.csdn.net/stl112514/article/details/45790519

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!