UVA1232,LA4108,Skyline,线段树

本文深入解析了建筑物覆盖度的概念及其计算方法,通过实例展示了如何利用线段树高效解决覆盖度求和问题。从理论到实践,为读者提供了全面的技术指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The skyline of Singapore as viewed from the Marina Promenade (shown on the left) is one of the iconic scenes of Singapore. Country X would also like to create an iconic skyline, and it has put up a call for proposals. Each submitted proposal is a description of a proposed skyline and one of the metrics that country X will use to evaluate a proposed skyline is the amount of overlap in the proposed sky-line.

As the assistant to the chair of the skyline evaluation committee, you have been tasked with determining the amount of overlap in each proposal. Each proposal is a sequence of buildings, $ \langle$b1, b2,..., bn$ \rangle$, where a building is specified by its left and right endpoint and its height. The buildings are specified in back to front order, in other words a building which appears later in the sequence appears in front of a building which appears earlier in the sequence.

The skyline formed by the first k buildings is the union of the rectangles of the first k buildings (see Figure 4). The overlap of a building, bi, is defined as the total horizontal length of the parts of bi, whose height is greater than or equal to the skyline behind it. This is equivalent to the total horizontal length of parts of the skyline behind bi which has a height that is less than or equal to hi, where hi is the height of building bi. You may assume that initially the skyline has height zero everywhere.

Input

The input consists of a line containing the number c of datasets, followed by c datasets, followed by a line containing the number `0'.

The first line of each dataset consists of a single positive integer, n (0 < n < 100000), which is the number of buildings in the proposal. The following n lines of each dataset each contains a description of a single building. The i-th line is a description of building bi. Each building bi is described by three positive integers, separated by spaces, namely, li, ri and hi, where li and rj (0 < li < ri$ \le$100000) represents the left and right end point of the building and hi represents the height of the building.

Output

The output consists of one line for each dataset. The c-th line contains one single integer, representing the amount of overlap in the proposal for dataset c. You may assume that the amount of overlap for each dataset is at most 2000000.


Note:In this test case, the overlap of building b1, b2 and b3 are 6, 4 and 4 respectively. Figure 4 shows how to compute the overlap of building b3. The grey area represents the skyline formed by b1 and b2 and the black rectangle represents b3. As shown in the figure, the length of the skyline covered by b3 is from position 3 to position 5 and from position 11 to position 13, therefore the overlap of b3 is 4.

Sample Input

1 
3 
5 11 3 
1 10 1 
3 13 2 
0

Sample Output

14

题意:建筑物在多长的部分是最高的成为该建筑物的覆盖度。求所有建筑物的覆盖度之和。

思路:[l,r]的建筑物可以用[l+1,r]来表示。为什么呢?自己在草稿纸上画画图~~

           线段树维护set,minh,maxh。即区间的最小值,区间的最大值。。

           当要进行区间置值,只要区间的最大值小于等于懒惰标记。则可以直接置值
           当要进行区间置值,如果区间的最小值大于懒惰标记。可忽略、

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 100080
#define lson id<<1,l,mid
#define rson id<<1|1,mid+1,r
//线段树要维护区间最高的建筑,还要有懒惰标记。
//当要进行区间置值,只要区间的最大值小于等于懒惰标记。则可以直接置值
//当要进行区间置值,如果区间的最小值大于懒惰标记。可忽略、
struct ST
{
	int l,r,set,maxh,minh;
}st[maxn<<2];
int ans;
inline int max(int a,int b)
{
	return a>b?a:b;
}

inline int min(int a,int b)
{
	return a>b?b:a;
}

void PushDown(int id)
{
	if(st[id].set != -1)
	{
		st[id<<1].set = st[id<<1|1].set = st[id].set;
		st[id<<1].maxh = st[id<<1|1].maxh = st[id].set;
		st[id<<1].minh = st[id<<1|1].minh = st[id].set;
		st[id].set = -1;
	}
}

void PushUp(int id)
{
	st[id].maxh = max(st[id<<1].maxh,st[id<<1|1].maxh);
	st[id].minh = min(st[id<<1].minh,st[id<<1|1].minh);
}

void buildtree(int id,int l,int r)
{
	st[id].l = l,st[id].r = r;
	st[id].set = st[id].maxh = st[id].minh = 0;
	if(l == r)	return;
	int mid = (l+r) >> 1;
	buildtree(lson);
	buildtree(rson);
}

void Update(int id,int l,int r,int key)
{
	if(st[id].minh > key)	return;
	if(st[id].l == l && st[id].r == r)
	{
		if(st[id].maxh <= key)
		{
			st[id].set = st[id].maxh = st[id].minh = key;
			ans += r - l + 1;
			return;
		}
		if(l == r)	return;
	}
	PushDown(id);
	if(st[id<<1].r >= r)
		Update(id<<1,l,r,key);
	else if(st[id<<1|1].l <= l)
		Update(id<<1|1,l,r,key);
	else 
	{
			Update(id<<1,l,st[id<<1].r,key);
			Update(id<<1|1,st[id<<1|1].l,r,key);
	}
	PushUp(id);
}

int main()
{
	//freopen("in.txt","r",stdin);
	int t;
	while(scanf("%d",&t)!=EOF && t)
	{
		while(t--)
		{
			int n;
			scanf("%d",&n);
			ans = 0;
			buildtree(1,1,100010);
			for(int i = 1;i <= n;i++)
			{
					int l,r,key;	scanf("%d%d%d",&l,&r,&key);
					Update(1,l+1,r,key);
			}
			printf("%d\n",ans);
		}
	}
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值