首页 > 其他 > 详细

POJ 3277 City Horizon

时间:2014-04-01 00:45:47      阅读:679      评论:0      收藏:0      [点我收藏+]
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);
}


POJ 3277 City Horizon,布布扣,bubuko.com

POJ 3277 City Horizon

原文:http://blog.csdn.net/yongxingao/article/details/22694175

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!