poj3277 City Horizon

本文介绍了一种使用离散化加线段树的数据结构解决计算多个矩形建筑形成的天际线总面积问题的方法。该方法首先对建筑边界进行排序,然后通过线段树更新高度并查询总面积。

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

H. City Horizon

2000ms
2000ms
65536KB
64-bit integer IO format:  %lld      Java class name:  Main
Font Size:   

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

一个JB题,坑了我2个小时。。。

就因为把sort(w+1,w+1+n)写成了sort(w,w+n,cmp)

离散化+线段树



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
const int N=80001;
struct acm
{
    int lt;
    int rt;
    long long h;
}node[N*4];
struct wp
{
    int s;
    int e;
    long long height;
}w[N];
int p[N];
long long ans;
void build(int l,int r,int id)
{
    node[id].rt=r;
    node[id].lt=l;
    node[id].h=0;
    if(r-l==1)
        return;
    int mid=(l+r)>>1;
    build(l,mid,id<<1);
    build(mid,r,id<<1|1);
}
void push_down(int id){
    if(node[id].h){
        node[id<<1].h=node[id<<1|1].h=node[id].h;
        node[id].h=0;
    }
}
void update(int l,int r,long long hh,int id)
{
    if(node[id].rt==r&&node[id].lt==l)
    {
        node[id].h=hh;
        return;
    }
    if(node[id].lt==node[id].rt-1)
        return;
       push_down(id);
    int mid=(node[id].lt+node[id].rt)>>1;
    if(r<=mid)
        update(l,r,hh,id<<1);
    else if(l>=mid)
        update(l,r,hh,id<<1|1);
    else
    {
        update(l,mid,hh,id<<1);
        update(mid,r,hh,id<<1|1);
    }
}


void query(int id)
{
    if(node[id].h>0)
    {
        //cout<<p[node[id].rt-1]<<" "<<p[node[id].lt-1]<<" "<<node[id].h<<endl;
        ans+=(p[node[id].rt-1]-p[node[id].lt-1])*node[id].h;
        return;
    }
    if(node[id].lt+1==node[id].rt)
        return;
    query(id<<1);
    query(id<<1|1);
}


int cmp( wp a, wp b)
{
    return a.height<b.height;
}


int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        build(1,N,1);
        int cnt=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%lld",&w[i].s,&w[i].e,&w[i].height);
            p[cnt++]=w[i].s;
            p[cnt++]=w[i].e;
        }
        sort(p,p+cnt);
        sort(w+1,w+n+1,cmp);
        cnt=unique(p,p+cnt)-p;


        for(int i=1;i<=n;i++)
        {
            int ll=lower_bound(p,p+cnt,w[i].s)-p+1;
            int rr=lower_bound(p,p+cnt,w[i].e)-p+1;
            update(ll,rr,w[i].height,1);
        }
        ans=0;
        query(1);
        printf("%lld\n",ans);
    }
    return 0;
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值