3月20日 city-horizon 线段树

题目描述:
City Horizon
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 19890 Accepted: 5499
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: Ai, Bi, 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.
这题很明显的求和,一开始的想法是树状数组(1m是一个元素),不过肯定溢出啊,而且一个一个更新也够tle几十次了。这里就应想到线段更新的线段树,这里数据比较大,要进行离散化,建树的时候用1、2、3这种下标,操作的时候要转换成具体的距离,又涉及到排序和去重,学习了一个unique函数。
一个问题:为什么开数组是N<<3(改成我们熟悉的2就会re),因为每次输入相当于2个点,而每个点又相当于传统线段树的一个点,所以要<<3。另外,两个线段是紧靠的,不需要mid+1。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define MA 40003
#define ll long long
struct node {
    int l, r, h;
}t[MA<<3];
int ho[MA<<1],hei[MA],lb[MA],rb[MA];
void build(int rt, int l, int r)//离散化之前的 
{
    t[rt].l = l;
    t[rt].r = r;
    t[rt].h = 0;
    if (l + 1 == r)return;
    int mid = (l + r) >> 1;
    build(rt << 1, l, mid);
    build(rt << 1 | 1, mid, r);
}
void update(int rt, int l, int r, int h)//注意离散化 
{
    if (ho[t[rt].l] == l&&ho[t[rt].r] == r)
    {
        if (t[rt].h < h)
        {
            t[rt].h = h;
        }
        return;
    }
    int mid = ho[(t[rt].l+t[rt].r)>>1];
    if (r <= mid)//注意等于号
    {
        update(rt << 1, l, r, h);
    }
    else if (l >= mid)
    {
        update(rt << 1 | 1, l, r, h);
    }
    else
    {
        update(rt << 1, l, mid, h);
        update(rt << 1 | 1, mid, r, h);
    }
}
ll ask(int rt,int h)
{
    if (h > t[rt].h)t[rt].h = h;
    if (t[rt].l + 1 == t[rt].r)return (ll)(ho[t[rt].r] - ho[t[rt].l])*t[rt].h;
    ll a = ask(rt << 1, t[rt].h);
    ll b = ask(rt << 1 | 1, t[rt].h);
    return a + b;
}
int main()
{
    int n,cnt=0;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        scanf("%d%d%d", &lb[i], &rb[i], &hei[i]);
        ho[++cnt] = lb[i];
        ho[++cnt] = rb[i];
    }
//  for (int i = 0; i < cnt; i++)
    //  printf("%d ", ho[i]);
//  printf("\n");
    std::sort(ho+1, ho + cnt+1);
    //for (int i = 0; i < cnt; i++)
        //printf("%d ", ho[i]);
    //printf("\n");
    //int *p = std::unique(ho, ho + cnt);
//  printf("%d\n",*p);
    int ct = std::unique(ho+1, ho + cnt+1) - ho-1;
    //printf("%d %d\n", cnt,ct);
    //for (int i = 0; i < ct; i++)
//      printf("%d ", ho[i]);
    build(1, 1, ct);
    //printf("%d\n", ct);
//  printf("\n%d %d %d %d\n", t[1].l, t[1].r,ho[t[1].l],ho[t[1].r]);
    for (int i = 0; i < n; i++)
    {
        update(1, lb[i], rb[i], hei[i]);
    }
    printf("%lld\n", ask(1, 0));
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_viceversa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值