POJ 3277 用堆求矩形轮廓面积

本文探讨了如何计算城市天际线上由多个建筑物形成的总面积,通过将建筑物离散化并使用堆数据结构来解决该问题。利用算法处理每条边,计算轮廓面积,实现高效解决方案。
City Horizon
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 14291 Accepted: 3867

Description

Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and observe the beautiful silhouettes formed by the rectangular buildings.

The entire horizon is represented by a number line with N (1 ≤ N ≤ 40,000) buildings. Building i's silhouette has a base that spans locations Ai through Bi along the horizon (1 ≤ Ai < Bi ≤ 1,000,000,000) and has height Hi (1 ≤ Hi ≤ 1,000,000,000). Determine the area, in square units, of the aggregate silhouette formed by all N buildings.

Input

Line 1: A single integer: N 
Lines 2..N+1: Input line i+1 describes building i with three space-separated integers: AiBi, and Hi

Output

Line 1: The total area, in square units, of the silhouettes formed by all N buildings

Sample Input

4
2 5 1
9 10 4
6 8 2
4 6 3

Sample Output

16

轮廓可以看成一个一个矩形。   
将建筑离散化成两条边(入边和出边)。   
将所有边排序。    
逐条边进行处理。
用堆维护当前未计算完的建筑。堆中的建筑按照建筑的高度排序。 对顶元素对应当前最高的建筑。

建筑的两条边对应建筑的进堆和出堆。 (堆必须支持删除)

当入边的高度比 对顶建筑还要高,说明轮廓中前面的矩形到头了。

当出边对应的建筑就是当前最高的建筑(堆顶的建筑),同样说明轮廓中当前的矩形到头了。


当然这个题目还可以用 线段树来做。   


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

#define N 40400

struct _building{
    long long l,r,h;
}B[N];

struct _wall{
    int b; 
    bool left;
    void set(int b,bool left)
    {
        this->b = b;
        this->left = left;
    }
    bool operator < (const _wall& other)const
    {   
        long long x = left ? B[b].l:B[b].r;
        long long y = other.left ? B[other.b].l:B[other.b].r;
        if( x==y )  return left < other.left;   // right first
        else        return x < y;
    }
}wall[N*2];

struct node{
    int b;
    bool operator < (const node& other)const
    {    return B[b].h < B[other.b].h;  }
    bool operator > (const node& other)const
    {    return B[b].h > B[other.b].h;  }
};

struct Heap{
    void swap(int x,int y)
    {
        Hash[ array[x].b ] = y;
        Hash[ array[y].b ] = x;
        node t = array[ x ] ;
        array[ x ] = array[ y ];
        array[ y ] = t;
    }    
    void up_adjust(int idx)
    {
        int father = idx/2;
        while(father && array[father] < array[idx]){
            swap( father,idx );
            father /= 2;   idx /= 2;
        }
    }
    void insert(int b)
    {
        array[++size].b = b; 
        Hash[b] = size;
        up_adjust(size);
    }
    void down_adjust(int idx)
    {
        while(idx<=size){
            int mx = idx;
            if( idx*2 <= size && array[idx*2] > array[mx] )  mx = idx*2;
            if( idx*2+1 <= size && array[idx*2+1] > array[mx] )  mx = idx*2+1;
            if( mx == idx )  break;
            swap( idx, mx );
            idx = mx;
        }
    }
    void remove(int b)
    {
        int idx = Hash[b];  
        if( idx==size ){
            size -- ;
        }else if( array[size] < array[idx] ){
            array[idx] = array[size--];
            Hash[ array[idx].b ] = idx;
            down_adjust(idx);
        }else{
            array[idx] = array[size--];
            Hash[ array[idx].b ] = idx;
            up_adjust(idx);
        }
        Hash[b] = 0; 
    }
    bool empty(){return size==0;}
    int top(){  return array[1].b; }        
    void show()
    {
        for(int i=1;i<=size;i++){
            cout<<"<"<<array[i].b<<","<<B[array[i].b].h<<"> ";
            if( (i &(i+1)) == 0) cout<<endl;
        }cout<<endl<<endl;
    }

    int Hash[N];    
    int size;
    node array[N];
}hp;

int main()
{
    int n;  scanf("%d",&n);
    
    for(int i=0;i<n;i++){
        //cout<<"Building "<<i<<":";
        scanf("%lld%lld%lld",&B[i].l,&B[i].r,&B[i].h);
        wall[i*2].set(i,0);
        wall[i*2+1].set(i,1);
    }
    sort(wall,wall+n*2);
    
    long long area = 0;
    long long pre = 0;  // pre wall
    int b;    // current building
    for(int i=0;i<n*2;i++){        
        b = wall[i].b;
        //if( wall[i].left ) cout<<"Buiding "<<b<<" left: "<<B[b].l<<" Height:"<<B[b].h<<endl;
        //else              cout<<"Buiding "<<b<<" right :"<<B[b].r<<" Height:"<<B[b].h<<endl;
        if( wall[i].left ){  // ¿ªÊ¼±ß 
            if( hp.empty() )  pre = B[b].l;
            else if( B[hp.top()].h < B[b].h ){
                area += B[hp.top()].h * (B[b].l - pre) ;
                pre = B[b].l;
            }
            hp.insert( b );
        }else{               //½áÊø±ß 
            if( b == hp.top() ){
                area += B[b].h * (B[b].r - pre);
                pre = B[b].r;
            }
            hp.remove( b );
        }
        //cout<<"area :"<<area<<endl;
        //cout<<"pre  :"<<pre<<endl<<endl;
    }
    cout<<area<<endl;
    
    //system("pause");
    return 0;
}




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值