1645: [Usaco2007 Open]City Horizon 城市地平线
1645: [Usaco2007 Open]City Horizon 城市地平线
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 315 Solved: 157
[Submit][Status]
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 A_i through B_i along the
horizon (1 <= A_i < B_i <= 1,000,000,000) and has height H_i (1
<= H_i <= 1,000,000,000). Determine the area, in square units, of
the aggregate silhouette formed by all N buildings.
N个矩形块,交求面积并.
Input
* Line 1: A single integer: N
* Lines 2..N+1: Input line i+1 describes building i with three space-separated integers: A_i, B_i, and H_i
Output
* Line 1: The total area, in square units, of the silhouettes formed by all N buildings
Sample Input
2 5 1
9 10 4
6 8 2
4 6 3
Sample Output
OUTPUT DETAILS:
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.
HINT
Source
全部的图形被分成了2*n-1个矩形,所以只要用线段树维护每一个矩形的高即可,取max
代码:(copy)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
#define inf 10000000000
using namespace std;
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n;
int x[],y[],val[],disc[];
struct seg{int l,r,mx,tag;}t[];
int find(int x)
{
int l=,r=*n;
while(l<=r)
{
int mid=(l+r)>>;
if(disc[mid]<x)l=mid+;
else if(disc[mid]==x)return mid;
else r=mid-;
}
}
void pushdown(int k)
{
if(t[k].l==t[k].r)return;
int tag=t[k].tag;t[k].tag=;
if(tag)
{
t[k<<].tag=max(t[k<<].tag,tag);
t[k<<|].tag=max(t[k<<|].tag,tag);
t[k<<].mx=max(t[k<<].mx,tag);
t[k<<|].mx=max(t[k<<|].mx,tag);
}
}
void build(int k,int l,int r)
{
t[k].l=l;t[k].r=r;
if(l==r)return;
int mid=(l+r)>>;
build(k<<,l,mid);build(k<<|,mid+,r);
}
void update(int k,int x,int y,int val)
{
pushdown(k);
int l=t[k].l,r=t[k].r;
if(l==x&&y==r)
{
t[k].tag=val;t[k].mx=max(t[k].mx,val);
return;
}
int mid=(l+r)>>;
if(y<=mid)update(k<<,x,y,val);
else if(x>mid)update(k<<|,x,y,val);
else
{
update(k<<,x,mid,val);update(k<<|,mid+,y,val);
}
}
int query(int k,int x)
{
pushdown(k);
int l=t[k].l,r=t[k].r;
if(l==r)return t[k].mx;
int mid=(l+r)>>;
if(x<=mid)return query(k<<,x);
else return query(k<<|,x);
}
int main()
{
n=read();build(,,n<<);
for(int i=;i<=n;i++)
{
x[i]=read(),y[i]=read(),val[i]=read();
disc[(i<<)-]=x[i];disc[i<<]=y[i];
}
sort(disc+,disc+(n<<)+);
for(int i=;i<=n;i++)
x[i]=find(x[i]),y[i]=find(y[i]);
for(int i=;i<=n;i++)
{
update(,x[i],y[i]-,val[i]);
}
ll ans=;
for(int i=;i<*n;i++)
{
ans+=(ll)query(,i)*(disc[i+]-disc[i]);
}
printf("%lld",ans);
return ;
}
1645: [Usaco2007 Open]City Horizon 城市地平线的更多相关文章
- 【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 # ...
- bzoj 1645: [Usaco2007 Open]City Horizon 城市地平线【线段树+hash】
bzoj题面什么鬼啊-- 题目大意:有一个初始值均为0的数列,n次操作,每次将数列(ai,bi-1)这个区间中的数与ci取max,问n次后元素和 离散化,然后建立线段树,每次修改在区间上打max标记即 ...
- BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线
BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线 Description N个矩形块,交求面积并. Input * Line 1: A single i ...
- 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树
[BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...
- bzoj1645 [Usaco2007 Open]City Horizon 城市地平线
Description Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at ...
- [BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 线段树
链接 题意:N个矩形块,交求面积并. 题解 显然对于每个 \(x\),只要求出这个 \(x\) 上面最高的矩形的高度,即最大值 将矩形宽度离散化一下,高度从小到大排序,线段树区间set,然后求和即可 ...
- 【BZOJ】1628 && 1683: [Usaco2007 Demo]City skyline 城市地平线(单调栈)
http://www.lydsy.com/JudgeOnline/problem.php?id=1628 http://www.lydsy.com/JudgeOnline/problem.php?id ...
- bzoj1683[Usaco2005 Nov]City skyline 城市地平线
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1683 Input 第1行:2个用空格隔开的整数N和W. 第2到N+1行:每行包括2个用空格 ...
随机推荐
- mysql 索引相关知识
由where 1 =1 引发的思考 最近工作上被说了 说代码中不能用 where 1=1,当时觉得是应该可以用的,但是找不到什么理据, 而且mysql 语句优化这方面确实很薄弱 感觉自己mysql ...
- IT项目外包有哪些注意事项
2015年两会上,李克强总理在政府工作报告中首次提出“互联网+”计划,各种互联网创业项目也如雨后春笋般破土而出.由于很多创业团队并没有自己的技术团队,所以软件外包成为很多创业项目普遍采取的一种方案.在 ...
- js中的数据类型及其转换
Js中的数据类型 Js中的数据类型一共有六种,即number,string,boolean,underfine,null,object. 一,number Number数据类型指的是数字,可以为整型, ...
- SEO学习之路
一.入门建站篇 1.Wamp集成环境安装 2.Wamp集成环境配置多站点 3.DedeCMS安装及目录结构 4.DedeCMS源码安装 5.DedeCMS官方手册 未完待续...
- facebook分享遇到的错误解决方法
*** Terminating app due to uncaught exception 'InvalidOperationException', reason: ''App ID not foun ...
- Qt的Graphics-View框架和OpenGL结合详解
Qt的Graphics-View框架和OpenGL结合详解 演示程序下载地址:这里 程序源代码下载地址:这里 这是一篇纯技术文,介绍了这一个月来我抽时间研究的成果. Qt中有一个非常炫的例子:Boxe ...
- c#中从string数组转换到int数组
以前一直有一个数组之间转换的东西,可是忘记了,今天也是找了好久也没有解决,最后用这种方法解决了,分享给大家. " }; int[] output = Array.ConvertAll< ...
- springmvc 基础
在最简单的springmvc应用程序中,控制器是唯一需要在java web部署描述文件(web.xml)中配置的servlete(springmvc的控制器是Dispatcher Servlet).每 ...
- 福昕阅读器drm加密解密总结
drm是数字版权保护的一种方式,前一段时间在做四川文轩数字图书馆项目的时候用到了相关的知识,感觉这东西对于一些在线阅读和视频播放还是有很大用处的. 对于其工作原理我也很好奇,先摘抄度娘的内容如下,当然 ...
- (二)《Java编程思想》——t h i s 关键字
this 关键字(注意只能在方法内部使用)可为已调用了其方法的那个对象生成相应的句柄.可象对待其他任何对象句柄一样对待这个句柄. package chapter4; //: Leaf.java // ...