离散化+线段树 POJ 3277 City Horizon
POJ 3277 City Horizon
Time Limit: 2000MS |
Memory Limit: 65536K |
|
Total Submissions: 18466 |
Accepted: 5077 |
Description
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
Hint
/*做法:因为所有矩形的都在一个X轴上,就把X方向是做线段树,每一个进行区间覆盖,最后查询到单点,计算总的面积,因为以X轴的坐标建立线段树,空间过大,可以离散化,用不超过40000个点表示坐标*/
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 40010
typedef long long ll;
ll zl[N],yl[N],hig[N],seg[N<<];
int n,len,cnt=;
struct Tree{
ll hi;
int l,r;
}tree[N<<];
void build_tree(int l,int r,int k)
{
tree[k].r=r;
tree[k].l=l;
tree[k].hi=;
if(l+==r) return;
int mid=(l+r)>>;
build_tree(l,mid,k<<);
build_tree(mid,r,k<<|);
}
void bing(int l,int r,ll z,ll y,int i,int k)
{
if(seg[l]>=z&&seg[r]<=y)
{
if(hig[i]>tree[k].hi)
tree[k].hi=hig[i];
return ;
}
int mid=(l+r)>>;//进行区间覆盖的时候,要看清楚条件,去覆盖哪段区间
if(y<=seg[mid])
bing(l,mid,z,y,i,k<<);
else if(z>=seg[mid])
bing(mid,r,z,y,i,k<<|);
else {
bing(l,mid,z,y,i,k<<);
bing(mid,r,z,y,i,k<<|);
}
}
ll Solve(int h,int k,int l,int r)
{
if(tree[k].hi<h)
tree[k].hi=h;/*相当于懒惰标记,是否父亲区间的h改变,但是孩子区间的高度没有变*/
if(l+==r)/*求面积并,必须查询到单点,因为小区间的高度>=大区间的高度*/
return (ll)(seg[r]-seg[l])*tree[k].hi;/*因为最后是这种形式,所以必须是前闭后开区间,全闭区间是没法做的。*/
int mid=(l+r)>>;
ll zzz=Solve(tree[k].hi,k<<,l,mid);
ll yyy=Solve(tree[k].hi,k<<|,mid,r);
return zzz+yyy;
}
int main()
{
while(scanf("%d",&n)==)
{
seg[]=;
for(int i=;i<=n;++i)
{
//cin>>zl[i]>>yl[i]>>hig[i];
scanf("%d%d%d",&zl[i],&yl[i],&hig[i]);
seg[++seg[]]=zl[i];
seg[++seg[]]=yl[i];
}
sort(seg+,seg+seg[]+);
cnt=unique(seg+,seg+seg[]+)-seg-;/*去重,用最少的节点*/
memset(tree,,sizeof());
build_tree(,cnt,);/*不是build_tree(1,cnt+1,1);因为这样会存在[cnt,cnt+1),这个区间是不存在的,因为没有cnt+1这个点,再去求面积的话,会出来负数*/
for(int i=;i<=n;++i)
bing(,cnt,zl[i],yl[i],i,);
cout<<Solve(,,,cnt);
}
return ;
}
离散化+线段树 POJ 3277 City Horizon的更多相关文章
- POJ 3277 City Horizon(叶子节点为[a,a+1)的线段树+离散化)
网上还有用unique函数和lowerbound函数离散的方法,可以百度搜下题解就有. 这里给出介绍unique函数的链接:http://www.cnblogs.com/zhangshu/archiv ...
- poj 3277 City Horizon (线段树 扫描线 矩形面积并)
题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不 ...
- [POJ] 3277 .City Horizon(离散+线段树)
来自这两篇博客的总结 http://blog.csdn.net/SunnyYoona/article/details/43938355 http://m.blog.csdn.net/blog/mr_z ...
- POJ 3277 City Horizon(扫描线+线段树)
题目链接 类似求面积并..2Y.. #include <cstdio> #include <cstring> #include <string> #include ...
- POJ 3277 City Horizon
标题效果: 每间房子的长度给出阴影(在间隔代表)而高度,求阴影总面积. 解题思路:矩形面积并. 以下是代码: #include <set> #include <map> #in ...
- 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树
[BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...
- 【POJ】2528 Mayor's posters ——离散化+线段树
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Description The citizens of Bytetown, A ...
- poj/OpenJ_Bailian - 2528 离散化+线段树
传送门:http://bailian.openjudge.cn/practice/2528?lang=en_US //http://poj.org/problem?id=2528 题意: 给你n长海报 ...
- 南阳理工 题目9:posters(离散化+线段树)
posters 时间限制:1000 ms | 内存限制:65535 KB 难度:6 描述 The citizens of Bytetown, AB, could not stand that ...
随机推荐
- php中的字符串常用函数(四) ord() 获得字符的ascii码 chr()获取ascii码对应的字符
ord('a');//=>97 返回小写a 的ascii码值97 chr(97);//=>a 返回ascii码表上的97对应的 小写a
- [moka同学笔记]Yii2.0 dropDownList的使用(二)
方法一: <?php $psObjs = Poststatus::find()->all(); $allStatus = ArrayHelper::map($psObjs,'id','na ...
- activiti 工作流
1. 工作流的概念 工作流(Workflow),就是“业务过程的部分或整体在计算机应用环境下的自动化”,它主要解决的是“使在多个参与者之间按照某种预定义的规则传递文档.信息或任务的过程自动进行,从而实 ...
- [TypeScript] TypeScript对象转JSON字符串范例
[TypeScript] TypeScript对象转JSON字符串范例 Playground http://tinyurl.com/njbrnrv Samples class DataTable { ...
- andriod arcgis加载影像TIF
private static final String TAG = "MainActivity"; private MapView mapView = null; @Overrid ...
- 得到设备是何种iPhone设备 + 怎么获得启动页面图片
一.前言 今天做一个功能,需要动态的获得启动页,然后根据不同设备去使用不用的启动页图片. 二.正文 常规来说,我们直接判断是何种设备,然后通过name去获得图片选择性加载即可.但是实际上遇到的两个问题 ...
- CoreAnimation动画(CALayer动画)
#pragma mark - CABasicAnimation动画 - (IBAction)basicAnimation:(UIButton *)sender { // 1.创建动画对象 CABasi ...
- spring和mybatis整合配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- linq扩展之动态排序
前两天看QQ群里面,一位朋友问的问题,说在linq中怎么实现动态排序呢,自己想了半天,没有头绪,网上找了下相关的资料,看了下,收益挺多,记录下来. 之前我们没有如果不知道动态排序的方法的话,我们可能会 ...
- 推些C语言与算法书籍
c语言系统学习与进阶: 1. C primer plus C primer plus 作为一本被人推崇备至的 c 入门经典,C primer plus 绝非浪得虚名.应该 算得上 C 教材里最好的入门 ...