P - Atlantis HDU - 1542

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 file 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 
#include<bits/stdc++.h>

using namespace std;

const int MAXN = 110;
struct f
{
	double l, r, h;
	int flag;
}line[MAXN * 2];
struct node
{
	int l, r;
	double len;
	bool ye;
	int lazy;
}tree[MAXN * 8];
double temp[MAXN * 2];

bool com(f u, f v)
{
	return u.h < v.h;
}
void build( int l, int r, int rt )
{
	tree[rt].l = l; tree[rt].r = r;
	tree[rt].len = 0;
	tree[rt].lazy = 0;
	tree[rt].ye = 0;
	if( l + 1 == r )
	{
		tree[rt].ye = 1;
		return;
	}
	int mid = ( l + r ) >> 1;
	build( l, mid, rt << 1 );
	build( mid, r, rt << 1 | 1 );
}

void update(int x, int y, int l, int r, int rt, int q)
{
	if(l >= x && r <= y)
	{
		tree[rt].lazy += q;
		if(tree[rt].lazy > 0)
			tree[rt].len = temp[tree[rt].r] - temp[tree[rt].l];
		else
		{
			if(l + 1 == r)
				tree[rt].len = 0;
			else
				tree[rt].len = tree[rt << 1].len + tree[rt << 1 | 1].len;
		}

		return;
	}
	int mid = (l + r) >> 1;
	if(mid > x)
		update(x, y, l, mid, rt << 1, q);
	if(mid < y)
		update(x, y, mid, r, rt << 1 | 1, q);
	if(tree[rt].lazy <= 0)
	{
		tree[rt].len = tree[rt << 1].len + tree[rt << 1 | 1].len;
	}
}
int main()
{
	int T;
	int cas = 0;
	double x1, y1, x2, y2;
	int n;
	while( ~scanf("%d", &n) && n )
	{
		cas ++;
		int tot = 0;
		for( int i = 1; i <= n; i ++ )
		{
			scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
			tot ++;
			line[tot].l = x1; line[tot].r = x2;
			line[tot].h = y1; line[tot].flag = 1;
			temp[tot] = x1;
			tot ++;
			line[tot].l = x1; line[tot].r = x2;
			line[tot].h = y2; line[tot].flag = -1;
			temp[tot] = x2;
		}
		sort( temp + 1, temp + tot + 1 );
		sort( line + 1, line + tot + 1, com );
		int k = 1;
		for( int i = 2; i <= tot; i ++ )
		{
			if( temp[i] != temp[i - 1] )
			{
				k ++ ;
				temp[k] = temp[i];
			}
		}
		//cout << k << endl;
		build( 1, k , 1 );
		double ans = 0;
		printf( "Test case #%d\n", cas );
		//for(int i = 1; i <= k ; i ++)
		//	cout << temp[i] << " " ;
		//cout << endl;
		for( int i = 1; i < tot; i ++ )
		{
			int L = lower_bound( temp + 1, temp + k + 1, line[i].l ) - temp;
			int R = lower_bound( temp + 1, temp + k + 1, line[i].r ) - temp;
			//cout << L << " " << R << endl;
			update( L, R, 1, k, 1, line[i].flag );
			//cout << "!" << tree[1].len << endl;
			//for(int j = 1; j <= 7 ; j ++)
			//	cout << tree[j].lazy << " " << tree[j].len << endl;
			ans += 1.00 * tree[1].len * ( line[i + 1].h - line[i].h);
			//cout << "!!" << ans << endl;
		}
		cout << "Total explored area: ";
		cout.precision(2);
		cout << fixed << ans << endl << endl;

	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Ant_e_zz/article/details/81431922
今日推荐