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. SourceGrid之Grid绑定数据

    private void BindData() { //为绑定的按钮选线增加单击事件 SourceGrid.Cells.Controllers.CustomEvents clickEvent = ne ...

  2. hihocoder1766 字符串问题

    思路: 不断贪心增加即可. 实现: #include <iostream> #include <cstring> using namespace std; ][]; int m ...

  3. Monkey安装和使用介绍

    安装步骤1)安装sdk环境在系统环境变量中配置 ANDROID_HOMED:\sdk PATH%ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools;%A ...

  4. 把apk文件拖到re-sign.jar运行打开的界面找不到指定文件

    下载一个zipalign.exe放到tools目录下面就可以了 点击下载

  5. Docker和K8S

    干货满满!10分钟看懂Docker和K8S [摘自:https://my.oschina.net/jamesview/blog/2994112]   本文来源微信号:鲜枣课堂 2010年,几个搞IT的 ...

  6. css3中animation属性animation-timing-function知识点以及其属性值steps()

    在animation中最重要的其实就是时间函数(animation-timing-function)这个属性,他决定了你的动画将以什么样的速度执行,所以最关键的属性值也就是cubic-bezier(n ...

  7. Noip2011提高组 聪明的质监员

    题目传送门 讲真,既然质监员这么聪明,为什么要让我们帮他设计程序? 所以还是叫ZZ的质检员吧 其实,我最想说的,不是这个题,而是这个\(\Sigma\)(一见 \(\Sigma\) 就懵逼系列) 这个 ...

  8. Luogu P1782 旅行商的背包

    题目传送门 卡常背包果然名不虚传 算法主体就是两种背包分开跑,先跑多重背包,再跑奇货 不知道为什么,这题二进制拆分好像要比单调队列优化快一些 然后这题毒瘤的地方就出来了: 如果一件物品的体积\(\ti ...

  9. 正则表达式匹配:根据key获取value

    需求 url请求html字符串,dytk值写在js里,可以看成是key-value的格式,需要提取dytk值. 解决方法 正则匹配 private string get_dytk(string htm ...

  10. Lucene入门基础教程

    http://www.linuxidc.com/Linux/2014-06/102856.htm