POJ 3277 City Horizon (离散化线段树)

链接:http://poj.org/problem?id=3277

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.

思路:
1>. 建立线段树,求出每一额线段的高度,进而求出面积;
2>. Ai 和 Bi的范围都很大,所以不能直接建立线段树,用离散化的方法建树;

代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define RST(N)memset(N, 0, sizeof(N))
using namespace std;

typedef __int64 INT;

typedef struct Building_
{
    INT x1, x2, height;
}Building;

struct Trie
{
    INT left, right, hi;
}trie[1000000];

Building N[50000];
INT Node[100000];   //表示离散化后的端点个数;

bool cmp(Building a, Building b) { return a.height < b.height; }

void Init(int n)   //初始化;
{
    RST(N), RST(trie), RST(Node);
    for(int i=1; i<=n; i++) {
        scanf("%I64d %I64d %I64d", &N[i].x1, &N[i].x2, &N[i].height);
        Node[i*2-1] = N[i].x1, Node[i*2] = N[i].x2;
    }
    sort(N+1, N+n+1, cmp);   //端点排序;
    sort(Node+1, Node+2*n+1);  
    int j = 1;
    for(int i=1; i<=2*n; i++) {   //离散化;
        if(Node[i] != Node[j]) Node[++j] = Node[i];
    }
    Node[0] = j;
    return ;
}

void build(INT L, INT R, INT p)
{
    trie[p].left = Node[L];
    trie[p].right = Node[R];
    if(L + 1 == R) return ;
    int mid = (L + R)/2;
    build(L, mid, p*2);
    build(mid, R, p*2+1);
    return ;
}

void update(INT L, INT R, INT value, INT p)
{
    if(L <= trie[p].left && trie[p].right <= R) {  //当前线段被包含与插入的线段;
        trie[p].hi = value;
        return ;
    }
    if(trie[p].hi > 0) trie[p*2].hi = trie[p*2+1].hi = trie[p].hi;
    trie[p].hi = -1;
    if(R > trie[p*2].right) update(L, R, value, p*2+1);
    if(L < trie[p*2].right) update(L, R, value, p*2);
    return ;
}

INT Query(INT p)
{
    if(trie[p].hi > 0) return trie[p].hi*(trie[p].right-trie[p].left);
    if(trie[p].hi == 0) return 0;
    return (Query(p*2) + Query(p*2+1));
}

int main()
{
    int n;
    scanf("%d", &n);
    Init(n);
    build(1, Node[0], 1);
    for(int i=1; i<=n; i++) update(N[i].x1, N[i].x2, N[i].height, 1);
    printf("%I64d\n", Query(1));
}

做这道题的时候,有一个问题我一直都没弄明白,,为什么用__int64能过,用unsigned long long不能过。。。求大牛解释;


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值