poj 3277 City Horizon 线段树加上离散化

思路:线段树加上离散化
就是将各个坐标值排序,那么40000个buildings,就只有80000个点,这就是离散化的思想。
每次更新,就取更大的高度。可以将各个结构体按高度进行排序。

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

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
__int64 y[81000<<2];
struct node
{
    __int64 l,r;
    __int64 s,yl,yr;
    int flag;
}tree[81000<<2];
struct w
{
   __int64 x,yl,yr;
   int flag;
}p[81000<<2],tem;
int cmp(struct w a,struct w b)
{
    return a.x<b.x;
}
void build(__int64 left,__int64 right,__int64 root)
{
    tree[root].l=left;
    tree[root].r=right;
    tree[root].s=tree[root].flag=0;
    tree[root].yl=y[left];
    tree[root].yr=y[right];
    if(left+1==right)
        return;
    int mid=(left+right)>>1;
    build(left,mid,root<<1);
    build(mid,right,root<<1|1);

}
void len(__int64 t)
{
    if(tree[t].flag>0)
    {
        tree[t].s=tree[t].yr-tree[t].yl;
        return ;
    }
    if(tree[t].l+1==tree[t].r)
        tree[t].s=0;
    else
        tree[t].s=tree[t<<1].s+tree[t<<1|1].s;
}
void update(__int64 t,w e)
{
    if(e.yl==tree[t].yl&&e.yr==tree[t].yr)
    {
        tree[t].flag+=e.flag;
        len(t);
        return;
    }
    if(e.yr<=tree[t<<1].yr)
        update(t<<1,e);
    else if(e.yl>=tree[t<<1|1].yl)
        update(t<<1|1,e);
    else
    {
        w tmp=e;
        tmp.yr=tree[t<<1].yr;
        update(t<<1,tmp);
        tmp=e;
        tmp.yl=tree[t<<1|1].yl;
        update(t<<1|1,tmp);
    }
     len(t);
}
int main()
{
    int t,i,cnt;
    __int64 x1,x2,y2;
    while(~scanf("%I64d",&t))
    {
        cnt=1;
        for(i=1;i<=t;i++)
        {
            int y1=0;
            scanf("%I64d %I64d %I64d",&x1,&x2,&y2);
            p[cnt].x=x1;
            p[cnt].yl=y1;
            p[cnt].yr=y2;
            p[cnt].flag=1;
            y[cnt++]=y1;
            p[cnt].x=x2;
            p[cnt].yl=y1;
            p[cnt].yr=y2;
            p[cnt].flag=-1;
            y[cnt++]=y2;
        }
        sort(y+1,y+cnt);
        sort(p+1,p+cnt,cmp);
        build(1,cnt-1,1);
        update(1,p[1]);
        __int64 sum=0;
        for(int i=2;i<cnt;i++)
        {
            sum+=tree[1].s*(p[i].x-p[i-1].x);
            update(1,p[i]);
        }
        printf("%I64d\n",sum);
    }
    return 0;
}

这道题目和poj1151是一样的,我在1151的前提上改了一下,但是感觉让我自己写还是写不出来,弱爆了!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值