【POJ】1151 Atlantis(线段树)

http://poj.org/problem?id=1151

经典矩形面积并吧.....很简单我就不说了...

有个很神的地方,我脑残没想到:

将线段变成点啊QAQ这样方便计算了啊

还有个很坑的地方,为毛每一次我精确地计算过空间可就是wa....一改大就ac...我无力了..

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }

const int N=1005;
#define lc x<<1
#define rc x<<1|1
#define lson l, mid, lc
#define rson mid+1, r, rc
int tot, idn;
double idy[N*2];
struct T { double sum; int num; }t[N*8+10];
void pushup(int x, int l, int r) { if(t[x].num>0) t[x].sum=idy[r+1]-idy[l]; else t[x].sum=t[lc].sum+t[rc].sum; }
void update(int L, int R, int k, int l=1, int r=tot, int x=1) {
	if(L<=l && r<=R) { t[x].num+=k; pushup(x, l, r); return; }
	int mid=(l+r)>>1;
	if(L<=mid) update(L, R, k, lson);
	if(mid<R) update(L, R, k, rson);
	pushup(x, l, r);
}
int n, ln;

struct dat { double x, y[2]; int flag; }line[N*2];
bool cmp(const dat &a, const dat &b) { return a.x<b.x; }
void cln() { tot=tot*4+1; rep(i, tot) t[i].sum=0, t[i].num=0; tot=0; }

int main() {
	int test=0;
	while(read(n), n) {
		ln=idn=tot=0;
		for1(i, 1, n) {
			static double x[2], y[2];
			rep(k, 2) scanf("%lf%lf", &x[k], &y[k]), idy[++idn]=y[k];
			++ln; line[ln].x=x[0]; rep(k, 2) line[ln].y[k]=y[k]; line[ln].flag=1;
			++ln; line[ln].x=x[1]; rep(k, 2) line[ln].y[k]=y[k]; line[ln].flag=-1;
		}
		sort(idy+1, idy+1+idn);
		idn=unique(idy+1, idy+1+idn)-idy-1;
		tot=idn-1;

		sort(line+1, line+1+ln, cmp);
		line[0].x=line[1].x;
		double ans=0;
		for1(i, 1, ln) {
			ans+=t[1].sum*(line[i].x-line[i-1].x);
			int left=lower_bound(idy+1, idy+1+idn, line[i].y[0])-idy,
				rigt=lower_bound(idy+1, idy+1+idn, line[i].y[1])-idy-1;
			update(left, rigt, line[i].flag);
		}

		printf("Test case #%d\n", ++test);
		printf("Total explored area: %.2f\n", ans);
		puts("");
		cln();
	}
	return 0;
}

  



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

时间: 2024-07-29 22:48:22

【POJ】1151 Atlantis(线段树)的相关文章

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 线段树+离散化

题目链接: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,实际上就是一个方向直接算出,另一个方向使用线段树维护已经覆盖的底边的长度.具体操作不算复杂:假想一条扫描下从下往上开始进行扫描,初始时候

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 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 (线段树+扫描线)

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

POJ 1151 扫描线 线段树

题意:给定平面直角坐标系中的N个矩形,求它们的面积并. 题解:建立一个四元组(x,y1,y2,k).(假设y1<y2)用来储存每一条线,将每一条线按x坐标排序.记录所有的y坐标以后排序离散化.离散化之后线段树的第i个叶子节点储存的是y[i+1]-y[i]. 这里的线段树用的是一个不用下传延迟标记的做法(仅限这一类题).线段树的每一个节点维护length(这个节点的子节点覆盖的长度)和cnt(这个节点代表的线段[l,r]所覆盖的次数). 任意一个区间都可以被线段树划分成O(logn)个子区间,我们

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 (离散化 + 扫描线 + 线段树)

题目链接题意:给定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(扫描线)

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

POJ 2481 Cows (线段树)

Cows 题目:http://poj.org/problem?id=2481 题意:有N头牛,每只牛有一个值[S,E],如果对于牛i和牛j来说,它们的值满足下面的条件则证明牛i比牛j强壮:Si <=Sjand Ej <= Ei and Ei - Si > Ej - Sj.现在已知每一头牛的测验值,要求输出每头牛有几头牛比其强壮. 思路:将牛按照S从小到大排序,S相同按照E从大到小排序,这就保证了排在后面的牛一定不比前面的牛强壮.再按照E值(离散化后)建立一颗线段树(这里最值只有1e5,所