POJ 3277 City Horizon (线段树)

City Horizon
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14887   Accepted: 4006

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

 
 
题意:有多栋建筑 它们有不同的高度 从一个面看进入 能看到的总面积


思路:线段树+离散化 因为它们的宽度太大,无法存储 所以得离散
PS:这道题又学到了一些新知道 1、另一种离散化的方法 2、unique 函数的使用

 unique(first,last)
参数 first, last:指出要剔除连续重复元素的迭代器区间[first,last)  右边是开区间
返回值  返回剔除元素后的新区间的最后一个元素的迭代器位置
 
 
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>

using namespace std;

#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)

const int N=40010;

struct node{
    int l,r,h;
}tree[N<<3];

int seg[N<<1],hei[N],lp[N],rp[N];

void build(int l,int r,int rt){
    tree[rt].l=l;
    tree[rt].r=r;
    tree[rt].h=0;
    if(l+1==r)
        return ;
    int mid=(l+r)>>1;
    build(l,mid,L(rt));
    build(mid,r,R(rt));
}

void update(int id,int l,int r,int rt){
    if(seg[tree[rt].l]==l && seg[tree[rt].r]==r){   //找到与原来长度相等的
        if(tree[rt].h<hei[id])  //比已存在的高 就把它覆盖
            tree[rt].h=hei[id];
        return ;
    }
    int mid=seg[(tree[rt].l+tree[rt].r)>>1];
    if(r<=mid)
        update(id,l,r,L(rt));
    else if(l>=mid)
        update(id,l,r,R(rt));
    else{
        update(id,l,mid,L(rt));
        update(id,mid,r,R(rt));
    }
}

long long Solve(int h,int rt){  //算每个子结点的面积并
    if(tree[rt].h<h)     //延迟覆盖 父结比子结点高的话
        tree[rt].h=h;
    if(tree[rt].l+1==tree[rt].r)
        return (long long)(seg[tree[rt].r]-seg[tree[rt].l])*tree[rt].h;
    long long a=Solve(tree[rt].h,L(rt));
    long long b=Solve(tree[rt].h,R(rt));
    return a+b;
}

int main(){

    //freopen("input.txt","r",stdin);

    int n;
    while(~scanf("%d",&n)){
        int cnt=0;
        for(int i=1;i<=n;i++){  //离散化操作
            scanf("%d%d%d",&lp[i],&rp[i],&hei[i]);
            seg[++cnt]=lp[i];
            seg[++cnt]=rp[i];
        }
        sort(seg+1,seg+cnt+1);
        int len=unique(seg+1,seg+cnt+1)-(seg+1);
        build(1,len,1);
        for(int i=1;i<=n;i++)
            update(i,lp[i],rp[i],1);
        long long ans=Solve(0,1);
        printf("%I64d\n",ans);
    }
    return 0;
}

 

posted @ 2013-04-24 12:33  Jack Ge  阅读(1219)  评论(0编辑  收藏  举报