扫描线模板题

Atlantis

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. 

InputThe 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.OutputFor 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 
题意:给出矩形的左下和右上两个端点的横纵坐标,求出所有矩形的面积,覆盖部分算一次。

以Y轴建立线段树 线段树上的点为需要用到的点(类似离散化)线段树的叶子结点为一段线段

t表示当前结点是否为叶子结点 f表示当前结点的区间是否被全部覆盖

#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAXN = 300;
double num[MAXN];
struct in{
	double y1,y2,h,v;
	bool operator < (const in &a) const{ return h == a.h ? v > a.v : h < a.h;}
}start[MAXN];
struct Segment_Tree{
	struct info{
		double l,r,sum;
		int t,f;
	}node[MAXN << 2];
	void Build(int n,int l,int r)
	{
		node[n].l = num[l],node[n].r = num[r];
		if(r - l == 1){
			node[n].t = 1;
			return;
		}
		int mid = (l+r) >> 1,lt = n << 1,rt = n << 1|1;
		Build(lt,l,mid);Build(rt,mid,r);
		node[n].t = 0;
		return;
	}
	void pushup(int n)
	{
		if(node[n].f > 0){
			node[n].sum = node[n].r - node[n].l;
		}
		else if(node[n].t == 1){
			node[n].sum = 0;
		}
		else{
			node[n].sum = node[n << 1].sum + node[n << 1|1].sum;
		}
	}
	void modify(int n,double l,double r,int k)
	{
		if(node[n].l >= l && node[n].r <= r){
			node[n].f += k;
			pushup(n);
			return;
		}
		int lt = n << 1,rt = n << 1|1;
		if(node[lt].r > l) modify(lt,l,r,k);
		if(node[rt].l < r) modify(rt,l,r,k);
		pushup(n);
		return;
	}
}ST;
int main()
{
	int n,T = 0;
	while(scanf("%d",&n) && n){
		T++;
		double x1,y1,x2,y2;
		for(int i = 1;i <= n; i++){
			scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
			int l = (i << 1) - 1,r = i << 1;
			num[l] = y1;
			num[r] = y2;
			start[l].y1 = y1,start[l].y2 = y2,start[l].h = x1,start[l].v = 1;
			start[r].y1 = y1,start[r].y2 = y2,start[r].h = x2,start[r].v = -1;
		}
		n <<= 1;
		sort(start+1,start+n+1);
		sort(num+1,num+n+1);
		int cnt = unique(num+1,num+n+1) - num - 1;
		ST.Build(1,1,cnt);
		double ans = 0;
		for(int i = 1;i <= n; i++){
			ans += (start[i].h-start[i-1].h)*ST.node[1].sum;
			ST.modify(1,start[i].y1,start[i].y2,start[i].v);
		}
		printf("Test case #%d\nTotal explored area: %.2lf\n\n",T,ans);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值