POJ 3277 City Horizon(扫描线)

City Horizon
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 17061 Accepted: 4653

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




   题意:有n做建筑在一个坐标轴上(想象在坐标轴上),三个数分别表示为建筑在坐标轴上的起点   终点    建筑的高度,然后求所有建筑面积的和(可能会出现两个建筑有一部分重合,这样的话重合的部分算一次就可以了)

   思路:该题的做法和poj1389的思路差不多,不同的是1389是从上到下扫描,而本题是从左到右进行扫描。两个题几乎是一样的。




#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define N  40011

using namespace std;

struct node
{
    long long int lf,rf;
    long long int cnt,ans;
    long long int l,r;
}q[N<<3];

struct tt
{
    long long int x1;
    long long int x2;
    long long int y;
    long long int flag;
}p[N<<3];

int n;
int k;
long long int pnum[N<<2];

bool cmp(tt a,tt b)
{
    if(a.y == b.y)
    {
        return a.x1 < b.x1;
    }
    return a.y<b.y;
}

void build(long long int l,long long int r,long long int rt)
{
    q[rt].l = l;
    q[rt].r = r;
    q[rt].ans = 0;
    q[rt].cnt = 0;
    q[rt].lf = pnum[l];
    q[rt].rf = pnum[r];
    if(l + 1 == r)
    {
        return ;
    }
    int mid = (l+r)>>1;
    build(l,mid,rt<<1);
    build(mid,r,rt<<1|1);
}

void updata(long long int rt)
{
    if(q[rt].ans > 0)
    {
        q[rt].cnt = q[rt].rf - q[rt].lf;
        return ;
    }
    else
    {
        q[rt].cnt = 0;
    }
    if(q[rt].l + 1  == q[rt].r)
    {
        return ;
    }
    else
    {
        q[rt].cnt = q[rt<<1].cnt + q[rt<<1|1].cnt;
    }
}

void insert(long long int rt,tt dot)
{
    if(q[rt].lf == dot.x1 && q[rt].rf == dot.x2)
    {
        q[rt].ans += dot.flag;
        updata(rt);
        return ;
    }
    if(q[rt<<1].rf>=dot.x2)
    {
        insert(rt<<1,dot);
    }
    else if(q[rt<<1|1].lf<=dot.x1)
    {
        insert(rt<<1|1,dot);
    }
    else
    {
        struct tt pt = dot;
        pt.x2 = q[rt<<1].rf;
        insert(rt<<1,pt);
        pt = dot;
        pt.x1 = q[rt<<1|1].lf;
        insert(rt<<1|1,pt);
    }
    updata(rt);
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        long long int x1,x2,y;
        k = 1;
        for(int i=0;i<n;i++)
        {
            scanf("%lld%lld%lld",&x1,&x2,&y);
            p[k].x1 = x1;
            p[k].x2 = x2;
            p[k].y = 0;
            p[k].flag = 1;
            pnum[k] = x1;
            k++;
            p[k].x1 = x1;
            p[k].x2 = x2;
            p[k].y = y;
            p[k].flag = -1;
            pnum[k] = x2;
            k++;
        }
        k--;
        sort(p+1,p+k+1,cmp);
        sort(pnum+1,pnum+1+k);
        build(1,k,1);
        insert(1,p[1]);
        long long int sum = 0;
        for(int i=2;i<=k;i++)
        {
            sum += q[1].cnt * (p[i].y - p[i-1].y);
            insert(1,p[i]);
        }
        printf("%lld\n",sum);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

叶孤心丶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值