HDU 1542 Atlantis 线段树扫面线求面积并

Problem 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 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 

Source

Mid-Central European Regional Contest 2000

Recommend

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
using namespace std;
#define N 605

struct node{
int le,ri,cover;
double val;
}f[N];

struct stud{
double x1,x2,h;
int type;
}e[N];

map<double,int>mp;

double X[N];

int cmp(stud a,stud b)
{
	return a.h<b.h;
}

void build(int pos,int le,int ri)
{
	f[pos].le=le;
	f[pos].ri=ri;
	f[pos].val=0;
	f[pos].cover=0;
	if(le==ri) return ;

	int mid=MID(le,ri);

	build(L(pos),le,mid);
	build(R(pos),mid+1,ri);
}

void pushup(int pos)
{
    if(f[pos].cover)
	{
		f[pos].val=X[f[pos].ri]-X[f[pos].le-1];
		return ;
	}

	if(f[pos].le==f[pos].ri)
		f[pos].val=0;
	else
		f[pos].val=f[L(pos)].val+f[R(pos)].val;

}

void update(int pos,int le,int ri,int type)
{

     if(f[pos].le>=le&&f[pos].ri<=ri)
	 {
	 	 f[pos].cover+=type;
	 	 pushup(pos);
	 	 return ;
	 }

	 int mid=MID(f[pos].le,f[pos].ri);

//	 if(le<=mid)
//		update(L(pos),le,ri,type);
//
//	 if(ri>mid)
//		update(R(pos),le,ri,type);

     if(mid>=ri)
		update(L(pos),le,ri,type);
	 else
		if(mid<le)
		   update(R(pos),le,ri,type);
	 else
	 {
	 	update(L(pos),le,mid,type);
	 	update(R(pos),mid+1,ri,type);

	 }

	 pushup(pos);
}

int main()
{
    int i,j;
    int n,k,ca=0;
    double x1,x2,y1,y2;

    while(scanf("%d",&n),n)
	{
		 k=0;
		 while(n--)
		 {
		 	scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);

		    if(x1>x2) swap(x1,x2);
            if(y1>y2) swap(y1,y2);

		 	X[k]=x1;
		 	e[k].x1=x1;
		 	e[k].x2=x2;
		 	e[k].h=y1;
		 	e[k++].type=1;

		 	X[k]=x2;
		 	e[k].x1=x1;
		 	e[k].x2=x2;
		 	e[k].h=y2;
		 	e[k++].type=-1;
		 }

		sort(X,X+k);
		sort(e,e+k,cmp);

		int m=1;
		for(i=1;i<k;i++)
		    if(X[i]!=X[i-1])
				X[m++]=X[i];

		for(i=0;i<m;i++)
			mp[X[i]]=i;

		m--;

		build(1,1,m);

		double ans=0;

		for(i=0;i<k-1;i++)
		{
			int le=mp[e[i].x1]+1;
			int ri=mp[e[i].x2];

			update(1,le,ri,e[i].type);
			ans+=f[1].val*(e[i+1].h-e[i].h);
		}

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

//另外一种建树方法

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
using namespace std;
#define N 605

struct stud{
  double x1,x2,h;
  int type;
}e[N];

struct node{
  int le,ri,cover;
  double val;
}f[N];

int n;

double X[N];

map<double,int>mp;

int cmp(stud a,stud b)
{
	return a.h<b.h;
}

void build(int pos,int le,int ri)
{
	f[pos].le=le;
	f[pos].ri=ri;
	f[pos].val=0;
	f[pos].cover=0;

	if(le+1==ri) return ;

	int mid=MID(le,ri);

	build(L(pos),le,mid);
	build(R(pos),mid,ri);
}

void pushup(int pos)
{
	if(f[pos].cover)
	{
		f[pos].val=X[f[pos].ri]-X[f[pos].le];
		return ;
	}

	if(f[pos].le==f[pos].ri||f[pos].le+1==f[pos].ri)
		f[pos].val=0;
	else
	    f[pos].val=f[L(pos)].val+f[R(pos)].val;

}

void update(int pos,int le,int ri,int type)
{

	if(le==ri) return ;

	if(f[pos].le>=le&&f[pos].ri<=ri)
	{
		f[pos].cover+=type;
		pushup(pos);
		return ;
	}

	int mid=MID(f[pos].le,f[pos].ri);

	if(mid>=ri)
		update(L(pos),le,ri,type);
	else
	   if(mid<le)
		 update(R(pos),le,ri,type);
	  else
	  {
	  	if(mid>le)
	  	 update(L(pos),le,mid,type);

	  	if(mid<ri)
		 update(R(pos),mid,ri,type);
	  }

	  pushup(pos);
}

int main()
{
	int i,j,ca=0;
	while(scanf("%d",&n),n)
	{
		double x1,x2,y1,y2;

		int k=0;
		while(n--)
		{
			scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
			if(x1>x2) swap(x1,x2);
			X[k]=x1;
			e[k].x1=x1;
			e[k].x2=x2;
			e[k].h=y1;
			e[k++].type=1;

			X[k]=x2;
			e[k].x1=x1;
			e[k].x2=x2;
			e[k].h=y2;
			e[k++].type=-1;
		}

		sort(X,X+k);
		sort(e,e+k,cmp);

		int m=1;
		for(i=1;i<k;i++)
			if(X[i]!=X[i-1])
			 X[m++]=X[i];

		for(i=0;i<m;i++)
			mp[X[i]]=i;

		m--;
		build(1,0,m);

		double ans=0;
		for(i=0;i<k-1;i++)
		{
			int le=mp[e[i].x1];
			int ri=mp[e[i].x2];

			update(1,le,ri,e[i].type);
			ans+=f[1].val*(e[i+1].h-e[i].h);
		}

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

	}
	return 0;
}
时间: 2024-10-12 05:53:42

HDU 1542 Atlantis 线段树扫面线求面积并的相关文章

HDU 1542 Atlantis(线段树扫描线)

http://acm.hdu.edu.cn/showproblem.php?pid=1542 Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6788    Accepted Submission(s): 2970 Problem Description There are several ancient Greek

HDU 1542 Atlantis 线段树+离散化+扫描线

题意:给出一些矩形的最上角坐标和右下角坐标,求这些矩形的面积并. NotOnlySuccess 线段树专辑中扫描线模板题,弱智的我对着大大的代码看了一下午才搞懂. 具体见思路见注释=.= #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #define lson rt<<1,l,mid #define rson rt<<1|1,mid

hdu 1542 Atlantis(线段树&amp;扫描线&amp;面积并)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6386    Accepted Submission(s): 2814 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

hdu 1542 Atlantis(线段树)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6899    Accepted Submission(s): 3022 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

hdu 1542 Atlantis (线段树+扫描线)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18559    Accepted Submission(s): 7523 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8998    Accepted Submission(s): 3856 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并

题意:给出矩形两对角点坐标,求矩形面积并. 解法:线段树+离散化. 每加入一个矩形,将两个y值加入yy数组以待离散化,将左边界cover值置为1,右边界置为2,离散后建立的线段树其实是以y值建的树,线段树维护两个值:cover和len,cover表示该线段区间目前被覆盖的线段数目,len表示当前已覆盖的线段长度(化为离散前的真值),每次加入一条线段,将其y_low,y_high之间的区间染上line[i].cover,再以tree[1].len乘以接下来的线段的x坐标减去当前x坐标,即计算了一部

hdu 1542 Atlantis 线段树求面积并,,,尼玛数据真坑人,数组千万不能开小!

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7815    Accepted Submission(s): 3420 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

hdu 1542 Atlantis(段树&amp;amp;扫描线&amp;amp;面积和)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6386    Accepted Submission(s): 2814 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i