离散化+线段树 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 ...
随机推荐
- 【学习整理】NOIP涉及的数论 [updating]
扩展欧几里得 求二元一次不定式方程 的一组解. int exgcd(int a,int b,int &x,int &y) { int t; ;y=;return a;} t=exgcd ...
- js 自带的 reduce() 方法
1.方法说明 , Array的reduce()把一个函数作用在这个Array的[x1, x2, x3...]上,这个函数必须接收两个参数,reduce()把结果继续和序列的下一个元素做累积计算,其效果 ...
- mysql performance_schema/information_schema授权问题
mysql> grant all on performance_schema.* to 'testuser'@'%';ERROR 1044 (42000): Access denied for ...
- js中的排序
不靠谱的sort() 众所周知,js中的sort()排序是按字母表顺序排序的,这就导致如下现象: var a = [9,60,111,55,8,7777]; a.sort(); alert(a); / ...
- ABAP Performance Examples
*modifying a set of lines directly(批量修改内表数据) *使用"LOOP ... ASSIGNING ..."可以直接修改内表中的数据,而不需要先 ...
- CSS重置样式表
网页设计,让人最头疼的莫过于让页面兼容各大浏览器,准确些是兼容它们“默认”的CSS样式表.第一种方式 * {margin:0px; padding:0px;} 这行代码虽然简单,但却让网页解析太慢.于 ...
- javascript的三个组成部分
javascript是一种专为与网页交互而设计的脚本语言,由下列三个不同的部分组成: ECMAScript,由ECMA-262定义,提供核心语言功能; 文档对象模型(DOM),提供访问和操作网页内容的 ...
- BI笔记-SSAS部署的几种方式及部署后的SSAS刷新
SSAS的部署方式在哥本哈士奇的博客:BI笔记之--- SSAS部署的几种方式已经介绍了四种方式,在这里再介绍一种比较常用的快速部署方式. 环境约定:SQL Server 2008 R2 示例库:Ad ...
- 去除UITableView中多余的分割线或者隐藏cell间的分割线
一:去除tableView多余的分割线 首先,自定义一个方法 -(void)setExtraCellLineHidden: (UITableView *)tableView{ UIView *v ...
- Linux线程学习(一)
一.Linux进程与线程概述 进程与线程 为什么对于大多数合作性任务,多线程比多个独立的进程更优越呢?这是因为,线程共享相同的内存空间.不同的线程可以存取内存中的同一个变量.所以,程序中的所有线程都可 ...