【POJ 1151】 Atlantis(离散化+扫描线)

【POJ 1151】 Atlantis(离散化+扫描线)

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20223   Accepted: 7634

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

开始补补数学的题了。。
扫描线,
+离散化……

额。。没用线段树,感觉n很小就直接上暴力了……
要注意的一点是,分割出来的是区间
一开始想错了 让每个点单表示每个y坐标……
导致出现一些边无法抠去

然后……关于扫描线网上蛮多的,就不详写了(

代码如下:
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout)

using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8;

struct Line
{
	double y1,y2;
	double x;
	int ad;
	bool operator < (const struct Line a)const
	{
		return x < a.x;
	}
};

Line ln[233];
double yp[233];
int yv[233];
int tpy;

int main()
{
	//fread();
	//fwrite();

	int n,z = 1;
	double x1,y1,x2,y2;

	while(~scanf("%d",&n) && n)
	{
		int tmp = 0;

		tpy = 0;
		for(int i = 0; i < n; ++i)
		{
			scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
			ln[tmp].x = x1;
			ln[tmp+1].x = x2;
			ln[tmp].y1 = ln[tmp+1].y1 = y1;
			ln[tmp].y2 = ln[tmp+1].y2 = y2;
			ln[tmp].ad = 1;
			ln[tmp+1].ad = -1;
			tmp += 2;
			yp[tpy++] = y1;
			yp[tpy++] = y2;
		}

		sort(yp,yp+tpy);
		tpy = 0;
		for(int i = 0; i < tmp; ++i)
		{
			if(!i || yp[i] > yp[tpy-1])
				yp[tpy++] = yp[i];
		}


		double ans = 0;
		memset(yv,0,sizeof(yv));
		sort(ln,ln+tmp);
		for(int i = 0; i < tmp; ++i)
		{
			int l,r;
			l = r = -1;
			for(int j = 0; j < tpy && (l == -1 || r == -1); ++j)
			{
				if(ln[i].y1 == yp[j]) l = j;
				if(ln[i].y2 == yp[j]) r = j;
			}

			while(l < r)
				yv[l++] += ln[i].ad;
			if(i == tmp-1 || ln[i].x == ln[i+1].x) continue;

			int j = 0;
			while(j < tpy)
			{
				while(j < tpy && yv[j] == 0) ++j;
				l = j;
				while(j < tpy && yv[j]) ++j;
				r = j;
				//printf("%f %f\n",yp[r],yp[l]);
				if(r > l) ans += (yp[r]-yp[l])*(ln[i+1].x-ln[i].x);
			}
		//	printf("%f\n",ans);
		}

		printf("Test case #%d\n",z++);
		printf("Total explored area: %.2f\n\n",ans);
	}

	return 0;
}







你可能感兴趣的:(【POJ 1151】 Atlantis(离散化+扫描线))