SKYLINE uva+线段树+区间的修改+lazy标记

Time Limit:3000MS Memory Limit:Unknown 64bit IO Format:%lld & %llu

[]  [Go Back]  [Status]  

Description

Download as PDF

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
解决方案:这题一开始,我就感觉吧,直接用setv[i]数组就可以了吧,后来才发现没那个简单。所以,必须要一个hightv[i]数组来维护每个区间的最高楼,还要一个flag[i]数组来标记每个区间下的最高高度是否覆盖整个区间。还有一个
#include<iostream>
#include<cstdio>
#include<cstring>
#define MMAX 2000000
#define N 100000
using namespace std;
int setv[4*(N+10)],hightv[4*(N+10)];///懒惰标记,及最高楼的维护
bool flag[4*(N+10)];///标记该节点下的孩子节点是否都是同一高度
int cover;
int l[N+10],r[N+10],h[N+10],n,x;
void push_down(int rt)
{
    int lc=rt*2,rc=rt*2+1;
    if(setv[rt]>=0)
    {
        if(setv[rt]>=hightv[rc])
        {
            hightv[rc]=setv[rt];
            flag[rc]=true;
        }
        if(setv[rt]>=hightv[lc])
        {
            hightv[lc]=setv[rt];
            flag[lc]=true;
        }
    }
    setv[lc]=max(setv[lc],setv[rt]);
    setv[rc]=max(setv[rc],setv[rt]);
    setv[rt]=-1;

}///标记往下走,必用维护数组hightv,必要时可减少查询时间
void push_up(int rt)
{
    int lc=rt*2,rc=rt*2+1;
    hightv[rt]=max(hightv[lc],hightv[rc]);
    flag[rt]=(hightv[lc]==hightv[rc])&&flag[lc]&&flag[rc];

}///往上更新信息,同时更新flag数组,flag标记该节点下的所有孩子是否同一高度
void update(int rt,int L,int R)
{

    if(l[x]<=L&&r[x]>=R&&hightv[rt]<=h[x])
    {


        setv[rt]=h[x];
        hightv[rt]=h[x];
        cover+=(R-L+1);
        flag[rt]=true;

    }
    else if(l[x]<=L&&r[x]>=R&&hightv[rt]>h[x]&&flag[rt]) return ;///若该区间的所有楼比h[x]大,不用查下去了。
    else
    {
        push_down(rt);
        int M=(L+R)/2;
        if(l[x]<=M)update(rt*2,L,M);
        if(r[x]>M) update(rt*2+1,M+1,R);
        push_up(rt);
    }



}///本题不用查询,可变更新边记录
int main()
{
    int c;
    scanf("%d",&c);
    while(~scanf("%d",&n)&&n)
    {
        cover=0;
        memset(setv,-1,sizeof(setv));
        memset(hightv,0,sizeof(hightv));
        memset(flag,true,sizeof(flag));
        setv[1]=0;
        for(x=0; x<n; x++)
        {
            scanf("%d%d%d",&l[x],&r[x],&h[x]);
            r[x]--;
            update(1,1,N);

        }
        printf("%d\n",cover);
    }


    return 0;
}

是,要求的是区间,所以看到别人是把右边的r-1,更新l到r-1区间。
code:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值