【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;
}

时间: 2024-09-30 15:20:08

【POJ 1151】 Atlantis(离散化+扫描线)的相关文章

poj 1151 Atlantis (离散化 + 扫描线 + 线段树)

题目链接题意:给定n个矩形,求面积并,分别给矩形左上角的坐标和右上角的坐标. 分析: 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <cstring> 5 #include <cstdlib> 6 #include <algorithm> 7 #define LL __int64 8 #define lson l, mid, 2*rt

POJ 1151 Atlantis 扫描线+线段树

点击打开链接 Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17252   Accepted: 6567 Description There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of pa

poj 1151 Atlantis 二分查找+离散化

Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17464   Accepted: 6654 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

POJ 1151 Atlantis(扫描线)

题目原链接:http://poj.org/problem?id=1151 题目中文翻译: POJ 1151 Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25769   Accepted: 9477 Description 有几个古希腊文本包含传说中的亚特兰蒂斯岛的描述. 其中一些文本甚至包括岛屿部分地图. 但不幸的是,这些地图描述了亚特兰蒂斯的不同区域. 您的朋友Bill必须知道地图的总面积. 你(不

POJ&#183;1151 Atlantis&#183;线段树求矩形面积并

题目在这:http://poj.org/problem?id=1151 Atlantis Time Limit: 1000MS   Memory Limit: 10000K 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 is

POJ 1151 Atlantis( 线段树 + 扫描线 )

一维离散化, 扫描线扫另一维, 用线段树维护 POJ建议交C++...G++貌似double要用%f ? 反正同一份代码C++AC,G++WA ------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 109; struct Line { double p,

poj 1151 线段树+扫描线

poj 1151 http://poj.org/problem?id=1151 大神orz 这是弱渣扫面线第一题,感觉区域赛有比较多的用的这个思想,所以赶在区域赛前熟悉一下扫描线和线段树(学习经验就是看着代码,然后一步一步测试,差不多就明白了 /************************************************************** Problem:poj 1151 User: youmi Language: C++ Result: Accepted Time

poj 1151 Atlantis (线段树+扫描线+离散化)

Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18061   Accepted: 6873 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

POJ 1151 Atlantis(线段树 + 扫描线)

转载请注明原文:http://www.cnblogs.com/burning-flame/p/5934653.html 题目链接:http://poj.org/problem?id=1151 题意: 给你 n 个矩形的对角线坐标,求 n 个矩形并集的面积. 做法: 扫描线 + 线段树. 因为作线段树的题遇到了扫描线,只是粗浅的了解了一下. 就像字面上的:线性扫描一遍,对于每个单元,因为某些事件的发生会有一些变化. 举个例子: 现有长度为 n 的数组a,同时有 n 个区间覆盖[li, ri],对于

POJ 1151 Atlantis 线段树+离散化

题目链接:http://poj.org/problem?id=1151  http://acm.hdu.edu.cn/showproblem.php?pid=1542 题目大意:给你几个矩形的坐标,求他们的面积的并. 解题思路:可以参考http://www.cnblogs.com/kuangbin/archive/2011/08/16/2140544.html,实际上就是一个方向直接算出,另一个方向使用线段树维护已经覆盖的底边的长度.具体操作不算复杂:假想一条扫描下从下往上开始进行扫描,初始时候