POJ 3277 City Horizon

本文介绍了一种使用线段树数据结构解决计算多个矩形建筑组成的天际线总面积问题的方法。通过输入建筑物的位置和高度信息,算法能够高效地计算出不重叠区域的总面积。

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


City Horizon
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 15769 Accepted: 4276

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

Hint

The first building overlaps with the fourth building for an area of 1 square unit, so the total area is just 3*1 + 1*4 + 2*2 + 2*3 - 1 = 16.

Source


面积并 线段树

#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#define N 41000
using namespace std;
int y[N*2];
struct num
{
    int x;
    int y1,y2,flag;
}a[N*2];
struct Num
{
    int l,r,tl,tr,sum,tlen;
}b[N*8];
bool cmp(num p1,num p2)
{
    return p1.x<p2.x;
}
int main()
{
    //freopen("data.txt","r",stdin);
    void build(int k,int l,int r);
    void update(int k,num p);
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            int x1,x2;
            int y2;
            scanf("%d %d %d",&x1,&x2,&y2);
            y[i*2-1] = 0;
            y[i*2] = y2;
            a[i*2-1].x = x1;
            a[i*2-1].y1 = 0;
            a[i*2-1].y2 = y2;
            a[i*2-1].flag = 1;
            a[i*2].x = x2;
            a[i*2].y1 = 0;
            a[i*2].y2 = y2;
            a[i*2].flag = -1;
        }
        n  = n * 2;
        sort(y+1,y+n+1);
        sort(a+1,a+n+1,cmp);
        build(1,1,n);
        update(1,a[1]);
        __int64 s=0;
        for(int i=2;i<=n;i++)
        {
            s+=(__int64)(a[i].x-a[i-1].x)*(__int64)(b[1].tlen);
            update(1,a[i]);
        }
        printf("%I64d\n",s);
    }
    return 0;
}
void build(int k,int l,int r)
{
    b[k].l =l; b[k].r = r;
    b[k].tl = y[l];
    b[k].tr = y[r];
    b[k].sum = b[k].tlen=0;
    if(l+1==r)
    {
        return ;
    }
    int mid = (l+r)>>1;
    build(k<<1,l,mid);
    build(k<<1|1,mid,r);
}
void cal(int k)
{
    if(b[k].sum>0)
    {
        b[k].tlen = b[k].tr - b[k].tl;
        return ;
    }
    if(b[k].l+1==b[k].r)
    {
        b[k].tlen = 0;
    }else
    {
        b[k].tlen = b[k<<1].tlen+b[k<<1|1].tlen;
    }
}
void update(int k,num p)
{
    if(b[k].tl==p.y1&&b[k].tr==p.y2)
    {
        b[k].sum+=p.flag;
        cal(k);
        return ;
    }
    if(b[k<<1].tr>=p.y2)
    {
        update(k<<1,p);
    }else if(b[k<<1|1].tl<=p.y1)
    {
        update(k<<1|1,p);
    }else
    {
        num p1 = p;
        p1.y2 = b[k<<1].tr;
        update(k<<1,p1);
        p1 = p;
        p1.y1 = b[k<<1|1].tl;
        update(k<<1|1,p1);
    }
    cal(k);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值