H. City Horizon

Time Limit: 2000ms
Memory Limit: 65536KB

64-bit integer IO format: %lld      Java class name: Main

 

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

解题:看了解题报告的,还在领悟离散化是什么灰机,不过这题目线段树部分其实很容易的,只是加上了离散化这一操作,Eggache啊!

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
const int maxn = ;
struct Building{
LL a,b,h;
}bb[maxn];
struct node{
int lt,rt,h;
}tree[maxn<<];
LL p[maxn<<],n,ans;
int cnt;
bool cmp(const Building &x,const Building &y){
return x.h < y.h;
}
void build(int lt,int rt,int v){
tree[v].lt = lt;
tree[v].rt = rt;
tree[v].h = ;
if(rt-lt == ) return;
int mid = (lt+rt)>>;
build(lt,mid,v<<);
build(mid,rt,v<<|);
}
void update(int lt,int rt,int val,int v){
if(tree[v].lt == lt && tree[v].rt == rt){
tree[v].h = val;
return;
}
if(tree[v].h > ){
tree[v<<].h = tree[v<<|].h = tree[v].h;
tree[v].h = ;
}
int mid = (tree[v].lt+tree[v].rt)>>;
if(rt <= mid) update(lt,rt,val,v<<);
else if(lt >= mid) update(lt,rt,val,v<<|);
else{
update(lt,mid,val,v<<);
update(mid,rt,val,v<<|);
}
}
void query(int v){
if(tree[v].h > ){
ans += (LL)tree[v].h*(p[tree[v].rt-] - p[tree[v].lt-]);
return;
}
if(tree[v].rt - tree[v].lt == ) return;
query(v<<);
query(v<<|);
}
int bsearch(int lt,int rt,int val){
while(lt <= rt){
int mid = (lt+rt)>>;
if(p[mid] == val)
return mid+;
else if(val < p[mid])
rt = mid-;
else lt = mid+;
}
return ;
}
int main(){
int i,j,r;
scanf("%lld",&n);
for(i = ; i < n; i++){
scanf("%d%d%d",&bb[i].a,&bb[i].b,&bb[i].h);
p[cnt++] = bb[i].a;
p[cnt++] = bb[i].b;
}
sort(p,p+cnt);//排序是为了使用二分查找。
sort(bb,bb+n,cmp);
build(,n<<,);
r = (n<<)-;
for(i = ; i < n; i++){
int lt = bsearch(,cnt-,bb[i].a);
int rt = bsearch(,cnt-,bb[i].b);
update(lt,rt,bb[i].h,);
}
ans = ;
query();
printf("%lld",ans);
return ;
}

xtu数据结构 H. City Horizon的更多相关文章

  1. [POJ3277]City Horizon

    [POJ3277]City Horizon 试题描述 Farmer John has taken his cows on a trip to the city! As the sun sets, th ...

  2. 离散化+线段树 POJ 3277 City Horizon

    POJ 3277 City Horizon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18466 Accepted: 507 ...

  3. poj City Horizon (线段树+二分离散)

    http://poj.org/problem?id=3277 City Horizon Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  4. BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线

    BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线 Description N个矩形块,交求面积并. Input * Line 1: A single i ...

  5. bzoj1645 / P2061 [USACO07OPEN]城市的地平线City Horizon(扫描线)

    P2061 [USACO07OPEN]城市的地平线City Horizon 扫描线 扫描线简化版 流程(本题为例): 把一个矩形用两条线段(底端点的坐标,向上长度,添加$or$删除)表示,按横坐标排序 ...

  6. 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树

    [BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...

  7. 【BZOJ】1645: [Usaco2007 Open]City Horizon 城市地平线(线段树+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1645 这题的方法很奇妙啊...一开始我打了一个“离散”后的线段树.............果然爆了. ...

  8. BZOJ 1645: [Usaco2007 Open]City Horizon 城市地平线 扫描线 + 线段树 + 离散化

    Code: #include<cstdio> #include<algorithm> #include<string> #define maxn 1030000 # ...

  9. Luogu_2061_[USACO07OPEN]城市的地平线City Horizon

    题目描述 Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the cit ...

随机推荐

  1. java 利用c3p0管理数据库连接池

    数据库连接池类,用于获取数据库连接.利用单例模式保证所有的连接都只通过一个连接池管理. package com.mousewheel.dbcon; import java.io.InputStream ...

  2. spring mvc添加静态资源访问时@Controller无效的解决

    web.xml中的url-pattern设置为/,添加mvc:resources访问静态资源时,@Controller无效的问题 web.xml: <servlet> <servle ...

  3. Xcode 升级后,cocoaPod 问题

    当我从Xcode 6.3切换到Xcode6.4的时候,因为我是mac上安装了两个不同的版本,现在把Xcode 6.3卸掉了. 现在再次运行pod install命令的时候,提示如下错误:   Upda ...

  4. PostgreSQL 的日期函数用法举例

    最近偶有开发同事咨询 PostgreSQL 日期函数,对日期处理不太熟悉,今天详细看了下手册的日期函数,整理如下,供参考. 一 取当前日期的函数 --取当前时间skytf=> select no ...

  5. shell脚本,awk数组之如何处理多个文件。

    [root@localhost | > file [root@localhost - | > file1 [root@localhost awk]# cat file [root@loca ...

  6. ratio_to_report分析函数求占比

    drop table test; create table test ( name varchar(20), kemu varchar(20), score number  ); insert int ...

  7. iOS开发遇到的坑之三--使用asi框架在xcode下正常运行,但是打包时却不能进行网络访问

    前言: 前两篇博客遇到的问题是前几天在实验室开发的时候遇到的,花了两三天时间在上面,今天突然心血来潮,想把这些”坑”写下来,所以才有了这两篇写的很丑的博客随笔 今天在开发时又遇到一个问题,那就是标题所 ...

  8. ios之键盘的自定义

    一.键盘通知 当文本View(如UITextField,UITextView,UIWebView内的输入框)进入编辑模式成为first responder时,系统会自动显示键盘.成为firstresp ...

  9. 调用 C 动态库

    调用 C 动态库 由 王巍 (@ONEVCAT) 发布于 2015/11/04 C 是程序世界的宝库,在我们面向的设备系统中,也内置了大量的 C 动态库帮助我们完成各种任务.比如涉及到压缩的话我们很可 ...

  10. TortoiseSVN文件夹及文件图标不显示解决方法---20150515

    由于自己的电脑是win7(64位)的,系统安装TortoiseSVN之后,其他的功能都能正常的使用,但是就是文件夹或文件夹的左下角就是不显示图标,这个问题前一段时间就遇到了(那个时候没找到合适的答案) ...