xtu数据结构 H. City Horizon
H. City Horizon
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
Lines 2..N+1: Input line i+1 describes building i with three space-separated integers: Ai, Bi, and Hi
Output
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的更多相关文章
- [POJ3277]City Horizon
[POJ3277]City Horizon 试题描述 Farmer John has taken his cows on a trip to the city! As the sun sets, th ...
- 离散化+线段树 POJ 3277 City Horizon
POJ 3277 City Horizon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18466 Accepted: 507 ...
- poj City Horizon (线段树+二分离散)
http://poj.org/problem?id=3277 City Horizon Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线
BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线 Description N个矩形块,交求面积并. Input * Line 1: A single i ...
- bzoj1645 / P2061 [USACO07OPEN]城市的地平线City Horizon(扫描线)
P2061 [USACO07OPEN]城市的地平线City Horizon 扫描线 扫描线简化版 流程(本题为例): 把一个矩形用两条线段(底端点的坐标,向上长度,添加$or$删除)表示,按横坐标排序 ...
- 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树
[BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...
- 【BZOJ】1645: [Usaco2007 Open]City Horizon 城市地平线(线段树+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1645 这题的方法很奇妙啊...一开始我打了一个“离散”后的线段树.............果然爆了. ...
- BZOJ 1645: [Usaco2007 Open]City Horizon 城市地平线 扫描线 + 线段树 + 离散化
Code: #include<cstdio> #include<algorithm> #include<string> #define maxn 1030000 # ...
- 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 ...
随机推荐
- h5旋转效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 断言assert用法
本文转自:http://blog.jobbole.com/76285/ 这个问题是如何在一些场景下使用断言表达式,通常会有人误用它,所以我决定写一篇文章来说明何时使用断言,什么时候不用. 为那些还不清 ...
- 【CSS】纯css实现立体摆放图片效果
1. 元素的 width/height/padding/margin 的百分比基准 设置 一个元素 width/height/padding/margin 的百分比的时候,大家可知道基准是什么? 举 ...
- [Rational Rose 2007]解决启动报”解决无法启动此程序因为丢失suite objects.dll“的问题
问题根源1:不是丢失suite objects.dll文件,而是环境变量配置错误或无配置 假如安装目录如:C:\Program Files\Rational 需要配置环境变量的路径为:C:\Progr ...
- C#遍历文件夹下全部文件
public static List<string> GetFile(string path, List<string> FileList, string RelativePa ...
- POJ 3133 Manhattan Wiring (插头DP,轮廓线,经典)
题意:给一个n*m的矩阵,每个格子中有1个数,可能是0或2或3,出现2的格子数为2个,出现3的格子数为2个,要求将两个2相连,两个3相连,求不交叉的最短路(起终点只算0.5长,其他算1). 思路: 这 ...
- codevs 3054 高精度练习-文件操作
时间限制: 1 s 空间限制: 64000 KB 题目等级 : 钻石 Diamond 题目描述 Description 输入一组数据,将每个数据加1后输出 输入描述 Input Descripti ...
- ubuntu14.04 red5
jdk tar -xzvf jdk-8u151-linux-x64.tar.gz nano /etc/profile export HADOOP_HOME=/usr/local/src/hadoope ...
- Codeforces Round #273 (Div. 2)-B. Random Teams
http://codeforces.com/contest/478/problem/B B. Random Teams time limit per test 1 second memory limi ...
- tp5 -- 腾讯云cos简单使用
因项目需要,本来是需要对接阿里云oss,但因客户错误将云存储买成腾讯云cos,因此简单做了个对象上传使用 首先下载cos的sdk: 三种方式在文档上面都有介绍 SDK 安装有三种方式:Composer ...