离散化+线段树 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 ...
随机推荐
- 泛函编程(21)-泛函数据类型-Monoid
Monoid是数学范畴理论(category theory)中的一个特殊范畴(category).不过我并没有打算花时间从范畴理论的角度去介绍Monoid,而是希望从一个程序员的角度去分析Monoid ...
- 控制台(Console)报错:java.io.IOException: Broken pipe
控制台(Console)输出: java.io.IOException: Broken pipe at sun.nio.ch.FileDispatcherImpl.write0(Native Meth ...
- Linux学习笔记(整理记录)
1.安装 (1):安装网址:http://www.jb51.net/os/78318.html 2.鸟哥的Linux命令学习 (1):显示系统目前所支持的语言:echo $LANG (2):修改语言成 ...
- 六个字符,带你领略JavaScript (js的艺术编写)
正文从这开始- JavaScript是一门神奇且奇妙的编程语言,我们有时候用它来写一些看似疯狂的代码,但这些代码依然可被执行且运行结果十分有趣.JavaScript 试图帮助我们将一些数据类型转化为我 ...
- 使用SWFUpload无刷新上传图片
使用SWFUpload组件无刷新上传图片 在做项目时,需要用到一个图片的无刷新上传,之前听说过SWFUpload,于是想要通过SWFUpload来进行图片的无刷新上传,由于我的项目属于是ASP.NET ...
- sublimetext Emmet插件(Zen coding)
1.省略div,插件会默认元素为div .container <div class="container"></div> 含糊标签名称,比如不需要指定li, ...
- C# Web Api 上传文件
一. 使用默认方法上传文件: 1.Action: /// <summary> /// 上传文件 使用上传后的默认文件名称 /// 默认名称是BodyPart_XXXXXX,BodyPart ...
- ABAP中Conversion Routine示例
在SAP的Domain定义中,Output Length下面有个Convers. routine的标识,这是SAP用来进行输入输出转换的.我们知道,屏幕上的I/O字段都是字符串形式的,而数 ...
- 安卓开发_深入学习ViewPager控件
一.概述 ViewPager是android扩展包v4包(android.support.v4.view.ViewPager)中的类,这个类可以让用户左右切换当前的view. ViewPager特点: ...
- Spring(八)SSH整合简述
一.Spring与Struts2的整合 1.1.整合步骤 1.2.配置web.xml 1.3.配置spring配置文件applicationContext.xml 1.4.配置struts配置文件 1 ...