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

City Horizon
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15255   Accepted: 4111

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

【题解】:
    这题是二分离散线段树,因为1 ≤Ai<Bi≤ 1,000,000,000,1 ≤Hi≤ 1,000,000,000显然直接建树是不可能的,因为N<40000,数据量不大可以先离散化再建树
    先将输入的Ai Bi Hi 保存在结构体node中,将Ai,Bi保存到一个index数组里面,排序,去重,这样每个Ai,Bi对应一个index数组的位置;
    结构体node按照高度Hi排序,排序之后就不用考虑覆不覆盖的问题了,统一覆盖就行了;
    
    【注】:被坑了,还是自己不够严谨的问题,居然数据类型以为只要用来求和的sum是__int64就可以了,可是在进行乘法的时候height*(r-l)溢出了,果断height__int64过了
    坑惨了,有图有真相:
    


【code】:

 /**
status:Accepted memory:7424K
time:407MS language:C++
code length:2904B author:cj
oj: poj submittime:2013-08-06 12:36:08
*/ #include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h> using namespace std; #define N 40010
#define lson p<<1
#define rson p<<1|1 struct Nod
{
int from,to,height;
}node[N]; struct TNod
{
int l,r;
__int64 height; //我以为只要sum值设为__int64就行了,height值也应该是__int64的,因为后面要进行乘法运算
__int64 sum;
}tnode[N<<]; bool cmp(Nod a,Nod b) //按高度排序
{
return a.height<b.height;
} int temp[N<<],index[N<<],brush[N<<]; int findPos(int a,int k) //二分查找离散
{
int l,r,mid=-;
l=;
r=k;
while(l<=r)
{
mid = (l+r)>>;
if(a<index[mid]) r=mid-;
else if(a>index[mid]) l=mid+;
else return mid;
}
return mid;
} void building(int l,int r,int p) //建树
{
tnode[p].l = l;
tnode[p].r = r;
tnode[p].height = ;
tnode[p].sum = ;
if(l+==r) return;
int mid = (l+r)>>;
building(l,mid,lson);
building(mid,r,rson);
} void update(int l,int r,int p,int h)
{
if(tnode[p].l==l&&r==tnode[p].r)
{
tnode[p].height = h;
tnode[p].sum = (index[r]-index[l])*tnode[p].height; //这里的height是64位int,相乘得64为int
return;
}
if(tnode[p].height>)  //向下更新
{
tnode[lson].sum = (index[tnode[lson].r] - index[tnode[lson].l])*tnode[p].height;
tnode[rson].sum = (index[tnode[rson].r] - index[tnode[rson].l])*tnode[p].height;
tnode[lson].height = tnode[rson].height = tnode[p].height;
tnode[p].height = ;
}
int mid = (tnode[p].l+tnode[p].r)>>;
if(r<=mid) update(l,r,lson,h);
else if(l>=mid) update(l,r,rson,h);
else
{
update(l,mid,lson,h);
update(mid,r,rson,h);
}
if(tnode[lson].height&&tnode[rson].height&&tnode[lson].height==tnode[rson].height) //向上更新
{
tnode[p].height = tnode[lson].height;
}
tnode[p].sum = tnode[lson].sum + tnode[rson].sum; //向上更新
} int main()
{
int n;
scanf("%d",&n);
int i,cnt=;
for(i=;i<n;i++)
{
scanf("%d%d%d",&node[i].from,&node[i].to,&node[i].height);
temp[cnt++]=node[i].from;
temp[cnt++]=node[i].to;
}
sort(temp,temp+cnt); //排序
int j=;
index[]=temp[];
for(i=;i<cnt;i++) //去掉重复值
{
if(index[j]!=temp[i])
{
index[++j]=temp[i];
}
}
int k = j;
building(,k,);
sort(node,node+n,cmp); //按照高度排序
for(i=;i<n;i++)
{
int from = findPos(node[i].from,k);
int to = findPos(node[i].to,k);
update(from,to,,node[i].height);
}
printf("%I64d\n",tnode[].sum); //直接输出,头结点的sum值
return ;
}

poj City Horizon (线段树+二分离散)的更多相关文章

  1. poj 3277 City Horizon (线段树 扫描线 矩形面积并)

    题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不 ...

  2. hdu 3333 Turing Tree 图灵树(线段树 + 二分离散)

    http://acm.hdu.edu.cn/showproblem.php?pid=3333 Turing Tree Time Limit: 6000/3000 MS (Java/Others)    ...

  3. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

  4. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  5. hdu4614 线段树+二分 插花

    Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N ...

  6. 洛谷P4344 脑洞治疗仪 [SHOI2015] 线段树+二分答案/分块

    !!!一道巨恶心的数据结构题,做完当场爆炸:) 首先,如果你用位运算的时候不小心<<打成>>了,你就可以像我一样陷入疯狂的死循环改半个小时 然后,如果你改出来之后忘记把陷入死循 ...

  7. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  8. luogu4422 [COCI2017-2018#1] Deda[线段树二分]

    讨论帖:线段树二分的题..我还考场切过..白学 这题我一年前的模拟赛考场还切过,现在就不会了..好菜啊. 显然直接线段树拆成$\log n$个区间,然后每个区间在进行线段树二分即可. UPD:复杂度分 ...

  9. Buy Tickets POJ - 2828 思维+线段树

    Buy Tickets POJ - 2828 思维+线段树 题意 是说有n个人买票,但是呢这n个人都会去插队,问最后的队列是什么情况.插队的输入是两个数,第一个是前面有多少人,第二个是这个人的编号,最 ...

随机推荐

  1. Linux下is not in the sudoers file解决方法

    最近在学习linux,在某个用户(xxx)下使用sudo的时候,提示以下错误:xxx is not in the sudoers file. This incident will be reporte ...

  2. (转)MSMQ(消息队列)

    原文作者:虔诚者    点此传送至原文   前段时间研究WCF接触到了MSMQ,所以认真的学习了一下,下面是我的笔记. 我理解的MSMQ MSMQ可以被看成一个数据储存装置,就如同数据库,只不过数据存 ...

  3. js获取网页高度(详细整理)

    网页可见区域宽: document.body.clientWidth 网页可见区域高: document.body.clientHeight 网页可见区域宽: document.body.offset ...

  4. VSFTPD无法上传的解决方法

    搭建好FTP之后就没有去测试了,今天去试了一下上传的时候发生错误了,无法上传,提示“553 Could not create file”错误, 上网找了一些资料,发现很多都说是权限和防火墙的问题,但是 ...

  5. 如何用代码开启“个人热点”功能?如何用代码把iOS设备设置成一个无线路由?

    不好意思,你办不到.苹果不允许.因为你没有权限.

  6. android 安装包签名问题探究

    1.首先先科普一下,android为什么需要给安装包签名: 所有的Android应用程序在发布之前都要求开发人员用一个证书进行数字签名,anroid系统不会安装没有进行签名的由于程序.    平时我们 ...

  7. ASP.NET MVC 过滤器开发与使用

    ASP.NET MVC 中给我们提供了内置的过滤器,通过过滤器,我们可以在控制器内的方法前后,添加必须的业务逻辑,如权限验证,身份验证,错误处理等. 今天,我们主要介绍3个过滤器:OutputCach ...

  8. 【转载】干货再次来袭!Linux小白最佳实践:《超容易的Linux系统管理入门书》(连载八)用命令实现批量添加用户

    Windows添加用户需要至少5个界面,而Linux一条命令就搞定了,这是不是高效人士办公第一法则呢.本文不给你一堆参数和选项,不让你见识教条主义,只给你最实用的代码. 想每天能听到小妞的语音播报,想 ...

  9. 高性能CSS(四)

    移除无匹配的样式 移除无匹配的样式,有两个好处: 第一,删除无用的样式后可以缩减样式文件的体积,加快资源下载速度: 第二,对于浏览器而言,所有的样式规则的都会被解析后索引起来,即使是当前页面无匹配的规 ...

  10. 在Centos6.5安装MySQL

    在阐述安装过程之前一定要再写一句,Centos7上安装MySQL是默认安装的MariaDB,因为在Centos7中用MariaDB代替了mysql数据库. linux安装软件很简单,有的时候只有一句话 ...