uva1232 - SKYLINE 线段树

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.

\epsfbox{p4108a.eps}

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.

\epsfbox{p4108b.eps}

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

  按顺序建房子,给出房子的左右坐标和高度,每个房子的覆盖度为它建的时候覆盖区间最高(相等也算)的区间长度。求总覆盖度。

  用懒惰标记,设minv[o],maxv[o],setv[o]分别为经过以o节点为根的树内的操作o区间范围内的最小值,最大值和置值。查询时候就算[L,R]在[ql,qr]区间内也必须要height>=maxv[o]或者L==R的时候才能把区间置值然后return,因为如果全部覆盖了才能set,否则就要把set往下推一层,然后继续递归,维护节点信息。

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cstring>
#include<queue>
#include<map>
#include<set>
#include<stack>
#define INF 0x3f3f3f3f
#define MAXN 100010
#define MAXM 35
#define MAXNODE 4*MAXN
#define eps 1e-9
#define pi 4*atan(1.0)
#define pii pair<int,int>
using namespace std;
int T,N,ql,qr,height,ans,minv[MAXNODE],maxv[MAXNODE],setv[MAXNODE],l[MAXN],r[MAXN],h[MAXN];
void maintain(int o,int L,int R){
    int lc=(o<<1),rc=(o<<1|1);
    if(L<R){
        minv[o]=min(minv[lc],minv[rc]);
        maxv[o]=max(maxv[lc],maxv[rc]);
    }
    if(setv[o]!=-1) minv[o]=maxv[o]=setv[o];
}
void pushdown(int o){
    if(setv[o]!=-1){
        int lc=(o<<1),rc=(o<<1|1);
        setv[lc]=setv[rc]=setv[o];
        setv[o]=-1;
    }
}
void update(int o,int L,int R){
    if(height<minv[o]) return;
    if(ql<=L&&qr>=R){
        if(height>=maxv[o]){
            setv[o]=maxv[o]=minv[o]=height;
            ans+=R-L+1;
            return;
        }
        if(L==R) return;
    }
    pushdown(o);
    int mid=(L+R)/2,lc=(o<<1),rc=(o<<1|1);
    if(ql<=mid) update(lc,L,mid);
    else maintain(lc,L,mid);
    if(qr>mid) update(rc,mid+1,R);
    else maintain(rc,mid+1,R);
    maintain(o,L,R);
}
int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%d",&T)!=EOF,T){
        while(T--){
            memset(setv,-1,sizeof(setv));
            memset(minv,0,sizeof(minv));
            memset(maxv,0,sizeof(maxv));
            setv[1]=0;
            scanf("%d",&N);
            int MINL=INF,MAXR=-1;
            for(int i=0;i<N;i++){
                scanf("%d%d%d",&l[i],&r[i],&h[i]);
                MINL=min(MINL,l[i]);
                MAXR=max(MAXR,r[i]);
            }
            ans=0;
            for(int i=0;i<N;i++){
                ql=l[i]+1;
                qr=r[i];
                height=h[i];
                update(1,MINL,MAXR);
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值