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. ReferenceError: password is not defined

    报错提示位置at c:\Users\Administrator\WebstormProjects\blogtest\routes\index.js:19:16 原因是我这个password没有定义,p ...

  2. JSP文件上传,好烦啊、、

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  3. GraphicsMagick安装&make命令使用

    0.0本过程为GraphicsMagick Linux版安装,通过典型的make编译安装. 未了支持png和jpg格式,首先请安装依赖.执行 yum install -y libpng-devel y ...

  4. GetClassName 取得的类名有

    今天上午稍微跟踪了一下自己的项目里面的各个空间,得知GetClassName可以取到以下类名:Static\Edit\Button\ComboBox\msctls_trackbar32\SysTabC ...

  5. Ubuntu16.04 + cuda8.0 + GTX1080安装教程

    1. 安装Ubuntu16.04 不考虑双系统,直接安装 Ubuntu16.04,从 ubuntu官方 下载64位版本: ubuntu-16.04-desktop-amd64.iso . 在MAC下制 ...

  6. 安卓6.0之前的系统 判断app是否有录音权限

    public static synchronized boolean isVoicePermission() { AudioRecord record = null; try { record = n ...

  7. 【NumPy学习指南】day5 改变数组的维度

    我们已经学习了怎样使用reshape函数,现在来学习一下怎样将数组展平. (1) ravel 我们可以用ravel函数完成展平的操作: In: b Out: array([[[ 0, 1, 2, 3] ...

  8. Jenkins结合ant传递参数

    需求: 使用Jenkins的「参数化构建过程」,由用户手动输入参数.通过ant脚本接收这个参数,并输出(当然,中间也可以进行复杂的处理,这里为了说明问题,仅做简单的输出). 1.基础环境 Jenkin ...

  9. UVA - 1252 Twenty Questions (状压dp)

    状压dp,用s表示已经询问过的特征,a表示W具有的特征. 当满足条件的物体只有一个的时候就不用再猜测了.对于满足条件的物体个数可以预处理出来 转移的时候应该枚举询问的k,因为实际上要猜的物品是不确定的 ...

  10. NSCopying协议和copy方法

    不是所有的对象都支持 copy需要继承NSCopying 协议(实现 copyWithZone: 方法)同样,需要继承NSMutableCopying 协议才可以使用mutableCopy(实现 mu ...