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

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

Hint

The first building overlaps with the fourth building for an area of 1 square unit, so the total area is just 3*1 + 1*4 + 2*2 + 2*3 - 1 = 16.
 /*做法:因为所有矩形的都在一个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的更多相关文章

  1. POJ 3277 City Horizon(叶子节点为[a,a+1)的线段树+离散化)

    网上还有用unique函数和lowerbound函数离散的方法,可以百度搜下题解就有. 这里给出介绍unique函数的链接:http://www.cnblogs.com/zhangshu/archiv ...

  2. poj 3277 City Horizon (线段树 扫描线 矩形面积并)

    题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不 ...

  3. [POJ] 3277 .City Horizon(离散+线段树)

    来自这两篇博客的总结 http://blog.csdn.net/SunnyYoona/article/details/43938355 http://m.blog.csdn.net/blog/mr_z ...

  4. POJ 3277 City Horizon(扫描线+线段树)

    题目链接 类似求面积并..2Y.. #include <cstdio> #include <cstring> #include <string> #include ...

  5. POJ 3277 City Horizon

    标题效果: 每间房子的长度给出阴影(在间隔代表)而高度,求阴影总面积. 解题思路:矩形面积并. 以下是代码: #include <set> #include <map> #in ...

  6. 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树

    [BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...

  7. 【POJ】2528 Mayor's posters ——离散化+线段树

    Mayor's posters Time Limit: 1000MS    Memory Limit: 65536K   Description The citizens of Bytetown, A ...

  8. poj/OpenJ_Bailian - 2528 离散化+线段树

    传送门:http://bailian.openjudge.cn/practice/2528?lang=en_US //http://poj.org/problem?id=2528 题意: 给你n长海报 ...

  9. 南阳理工 题目9:posters(离散化+线段树)

    posters 时间限制:1000 ms  |  内存限制:65535 KB 难度:6   描述 The citizens of Bytetown, AB, could not stand that ...

随机推荐

  1. x3d 规范 在线镜像版

    国内访问web网站不稳定,在此部署一个国内的在线版本,供有需要的同学查阅. 注:已失效 x3d规范文档: https://code.csdn.net/x3dcn/x3d-specification-d ...

  2. KMP---Count the string

    题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110060#problem/A Description It is well k ...

  3. NYOJ:题目529 flip

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=529 由于此题槽点太多,所以没忍住...吐槽Time: 看到这题通过率出奇的高然后愉快的进 ...

  4. servlet中的转发和重定向问题

    重定向和请求转发在学习servlet的时候很容易混淆,故在此特意记录. 1. 重定向---------sendRedirect()方法 Servlet响应请求有两种方式,一个是重定向,返回一个页面给客 ...

  5. 以Web Host的方式来寄宿Web API

    一.新建一个Common的类库项目并新建一个测试用的Contact实体类 namespace Common { public class Contact { public string Id { ge ...

  6. FPSCalc——简单FPS观测类

    利用Unity做的手游项目很多时候要保证流畅度,流畅度最直观的表现就是帧率FPS.Unity编辑器模式下的帧率观测几乎没有意义,所以还是自己实现的好. 这里给一个前人写的类,我几乎原封不动,该类只有一 ...

  7. 趣味题:恺撒Caesar密码(c++实现)

    描述:Julius Caesar 生活在充满危险和阴谋的年代.为了生存,他首次发明了密码,用于军队的消息传递.假设你是Caesar 军团中的一名军官,需要把Caesar 发送的消息破译出来.并提供给你 ...

  8. xampp 访问出现New XAMPP security concept 解决办法

    最近通过手机访问本地服务器时出现以下问题: Access forbidden! New XAMPP security concept: Access to the requested director ...

  9. Android v4、v7、v13 的区别

    Android Support v4:  这个包是为了照顾1.6及更高版本而设计的,这个包是使用最广泛的,eclipse新建工程时,都默认带有了. Android Support v7:  这个包是为 ...

  10. view.performClick()触发点击事件

    1.主要作用 自动触发控件的点击事件 2.界面的布局文件  activity_main.xml <RelativeLayout xmlns:android="http://schema ...