首页 > 代码库 > HDU 1542 Atlantis 线段树扫面线求面积并

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


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