HDU 1542 Atlantis (线段树求矩阵覆盖面积)

题意:给你n个矩阵求覆盖面积。

思路:看了别人的结题报告

给定一个矩形的左下角坐标和右上角坐标分别为:(x1,y1)、(x2,y2),对这样的一个矩形,我们构造两条线段,一条定位在x1,它在y坐标的区间是[y1,y2],并且给定一个cover域值为1;另一条线段定位在x2,区间一样是[y1,y2],给定它一个cover值为-1。根据这样的方法对每个矩形都构造两个线段,最后将所有的线段根据所定位的x从左到右进行排序

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define M 100
#define inf 0x3fffffff
#define maxn 500000*2

struct seg
{
	int flag;
	double up,down,x;
}line[M*5];
int cmp(seg a,seg b)
{
	return a.x<b.x;
}
struct Tree
{
	double up,down,x;
	int cover;
	bool flag;
}tree[M*M];
double y[M*3];//对y进行分割,建树
void build(int id,int l,int r)
{
	tree[id].down=y[l];tree[id].up=y[r];
	tree[id].flag=false;tree[id].cover=0;tree[id].x=-1;
	if(l+1==r)
	{
		tree[id].flag=true;
		return ;
	}
	int mid=(l+r)/2;
	build(id*2,l,mid);
	build(id*2+1,mid,r);
}
double insert(int id,double x,double l,double r,int flag)//flag表示为左边还是右边
{
	if(tree[id].down>=r||tree[id].up<=l) return 0;
	if(tree[id].flag)
	{
		if(tree[id].cover>0)//递归到了叶子节点
		{
			double temp_x=tree[id].x;
			double ans=(x-temp_x)*(tree[id].up-tree[id].down);
			tree[id].x=x;//定位上一次的x
			tree[id].cover+=flag;
			return ans;
		}
		else
		{
			tree[id].cover+=flag;
			tree[id].x=x;
			return 0;
		}
	}
	double ans1,ans2;
	ans1=insert(id*2,x,l,r,flag);
	ans2=insert(id*2+1,x,l,r,flag);
	return ans1+ans2;
}
int main()
{
	int t,ca=1;
	while(scanf("%d",&t)&&t)
	{
		double x1,x2,y1,y2;
		int k=0;
		for(int i=0;i<t;i++)
		{
			scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
			k++;
			y[k]=y1;
			line[k].down=y1;
			line[k].up=y2;
			line[k].x=x1;
			line[k].flag=1;//1表示左边
			k++;
			y[k]=y2;
			line[k].x=x2;
			line[k].down=y1;
			line[k].up=y2;
			line[k].flag=-1;//-1表示右边
		}
		sort(y+1,y+k+1);
		sort(line+1,line+1+k,cmp);
		build(1,1,k);
		double ans=0;
		for(int i=1;i<=k;i++)
			ans+=insert(1,line[i].x,line[i].down,line[i].up,line[i].flag);
		printf("Test case #%d\nTotal explored area: %.2f\n\n",ca++,ans);
	}
	return 0;
}
/*
2
10 10 20 20
15 15 25 25.5
0
*/

HDU 1542 Atlantis (线段树求矩阵覆盖面积)

时间: 2024-10-09 18:03:35

HDU 1542 Atlantis (线段树求矩阵覆盖面积)的相关文章

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;面积并)

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

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

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

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

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

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. You