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. Windows中使用cmd实现自定义Service的安装与卸载

    在项目中,有些时候我们需要自定义一些Service来定时处理一些业务逻辑,这时候就涉及到如何安装与卸载service的问题了,具体如何安装呢?在此整理一些解决方案供大家参考: 方案一: 1.运行--〉 ...

  2. 写代码的心得,怎么减少编程中的 bug?

    遭遇 bug 的时候,理性的程序员会说:这个 bug 能复现吗? 自负型:这不可能,在我这是好好的. 经验型:不应该,以前怎么没问题? 幻想型:可能是数据有问题. 无辜型:我好几个星期都没碰这块代码了 ...

  3. JMS - QueueBrowser

    QueueBrowser 是一个专用对象,提供提前浏览 Queue 上的排队消息的功能,而实际上并没有真正消费这些消息.这是点对点消息传送模型的独有特性.从 QueueBrowser 获得消息是该队列 ...

  4. Linux 命令 - ftp: 网络文件传输工具

    命令格式 ftp [-pinegvd] [host] 命令参数 -A 传输文件模式为主动模式. -p 传输文件模式为被动模式. -i 关闭交互模式. -n 关闭自动登录功能. -e 不记录历史命令. ...

  5. Git CMD - rm: Remove files from the working tree and from the index

    命令格式 git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] <file>…​ 命令参 ...

  6. SQL Server2008新特性Filesteam的使用

    Filesteam是SQL Server2008的新特性,它结合了SQL Server和NTFS文件系统,为Blob类型的数据提供了比较高效的存储和访问方案.我们最近的一个项目就是采用SQL Serv ...

  7. WebResponse 取出全国省市区的邮编

    WebResponse用法(根据省市区地址查询其邮编): class Program { static string url { get; set; } static void Main(string ...

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

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

  9. 常用到的Tomcat的修改方法

    1.修改端口号 打开tomcat的service.xml文件: 找到<Connector connectionTimeout="20000" port="8080& ...

  10. DQL_数据查询语言

    2014年11月21日 21:43:53 DQL      基础查询--  注意要点:1.用户友善的标题                                                 ...