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. 我的NopCommerce之旅(1): 系统综述

    一.概述 NopCommerce是一个开源的购物网站,它的特点是Pluggable modular/layered architecture(可插拔模块分层架构) 二.功能特色介绍 1.适配手机端 2 ...

  2. sql server技巧

    --查出数据最新的存储过程select name,modify_date from sys.procedures where modify_date>'2017-05-26 17:21:09.3 ...

  3. CF1025C Plasticine zebra

    思路: 不要被骗了,这个操作实际上tm是在循环移位. 实现: #include <bits/stdc++.h> using namespace std; int main() { stri ...

  4. 海康威视采集卡结合opencv使用(两种方法)-转

    (注:第一种方法是我的原创 ^_^. 第二种方法是从网上学习的.) 第一种方法:利用 板卡的API:  GetJpegImage 得到 Jpeg 格式的图像数据,然后用opencv里的一个函数进行解码 ...

  5. jmeter中登录和提交收银出现的错误

    登录出现的错误 登录界面如图所示: 为了防止登录跳转的问题response code 302的问题,要设置 2.提交收银界面 当系统设置必须传送jison格式时,要在HTTP Header Manag ...

  6. Grace Huang 2017/1/12

    原文 Huang doesn't think of acting as pretending to be someone else.Rather,she considers it an opportu ...

  7. 洛谷 P1926 小书童——刷题大军

    题目背景 数学是火,点亮物理的灯:物理是灯,照亮化学的路:化学是路,通向生物的坑:生物是坑,埋葬学理的人. 文言是火,点亮历史宫灯:历史是灯,照亮社会之路:社会是路,通向哲学大坑:哲学是坑,埋葬文科生 ...

  8. leetcode 127 单词接龙

    给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度.转换需遵循如下规则: 每次转换只能改变一个字母. 转换过程中的中 ...

  9. Bellman-Ford与SPFA

    一.Bellman-Ford Bellman-Ford 算法是一种用于计算带权有向图中单源最短路径(当然也可以是无向图).与Dijkstra相比的优点是,也适合存在负权的图. 若存在最短路(不含负环时 ...

  10. python基础一 day10(1)

    要背的: