ACM: 线段树 poj 3277

City Horizon

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: Ai, Bi, 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.
 
题意: 给出你矩形的[A,B]长度区间和高度H, 求全部的矩形面具和, 覆盖部分只算一次.
 
解题思路:
      1. 经典线段树求矩形面积. 高度离散化即可. (值得多次回味的题目)
 
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX 400005
struct Line
{
  __int64 x, h;
  int flag;
}line[MAX*2];
struct node
{
  int l, r;
  __int64 h, height;
  int flag;
}pt[MAX*4];
int n;
__int64 y[MAX];
int A, B, H;
inline __int64 max(__int64 a, __int64 b)
{
  return a > b ? a : b;
}
bool cmp(Line a, Line b)
{
  if(a.x == b.x) return a.h < b.h;
  else return a.x < b.x;
}
void buildTree(int l, int r, int pos)
{
  pt[pos].l = l, pt[pos].r = r;
  pt[pos].h = pt[pos].flag = 0;
  if(l == r)
  {
    pt[pos].height = y[l];
    return ;
  }
  int mid = (l+r)/2;
  buildTree(l, mid, pos*2);
  buildTree(mid+1, r, pos*2+1);
  pt[pos].height = max(pt[pos*2].height, pt[pos*2+1].height);
}
void insert(int num, int pos)
{
  if(pt[pos].l == pt[pos].r)
  {
    pt[pos].flag += line[num].flag;
    if(pt[pos].flag > 0) pt[pos].h = pt[pos].height;
    else pt[pos].h = 0;
    return ;
  }
  if(line[num].h <= pt[pos*2].height)
    insert(num, pos*2);
  else
    insert(num, pos*2+1);
  pt[pos].h = max(pt[pos*2].h, pt[pos*2+1].h);
}
int main()
{
  int i;
//  freopen("input.txt","r",stdin);
  while(scanf("%d",&n) != EOF)
  {
    for(i = 0; i < n; ++i)
    {
      scanf("%d %d %d",&A, &B, &H);
      line[i*2].x = A;
      line[i*2].h = H;
      line[i*2].flag = 1;
      line[i*2+1].x = B;
      line[i*2+1].h = H;
      line[i*2+1].flag = -1;
      y[i] = H;
    }
    sort(y, y+n);
    sort(line, line+2*n, cmp);
    buildTree(0, n-1, 1);
   
    insert(0, 1);
    __int64 result = 0;
    for(i = 1; i < n*2; ++i)
    {
      result += (line[i].x-line[i-1].x)*pt[1].h;
      insert(i, 1);
    }
    printf("%I64d\n",result);
  }
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值