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个用空格 ...
随机推荐
- openstack 手动 部署安装调试
Virtual Interface creation failed
- Hadoop2.4.1 64-Bit QJM HA and YARN HA + Zookeeper-3.4.6 + Hbase-0.98.8-hadoop2-bin HA Install
Hadoop2.4.1 64-Bit QJM HA and YARN HA Install + Zookeeper-3.4.6 + Hbase-0.98.8-hadoop2-bin HA(Hadoop ...
- javascript中的变量作用域
在网上看了一道js面试题 <script type="text/javascript"> var tt = 'aa'; function test() { alert( ...
- python socket实例练习
Web Server是基于Socket编程,又称之为网络编程,socket是网络编程接口,socket可以建立网络连接,读数据,写数据.socket模块定义了一些常量参数,用来指定socket的的地址 ...
- 在Build时使用NuGet自动下载缺失的包
现在使用.net编程,不可避免的总会使用到NuGet去下载引用第三方的dll,而且vs2012也直接默认就会安装上NuGet了.NuGet的确也给我们带来了一些方便,但是也带来了一些问题,比如:一般程 ...
- Sublime Text2 按shift键选择不了的问题
记录下来,免得以后忘了: 今天在Sublime Text 2装了一个ThinkPHP插件之后.发现按shift键+鼠标左键选择不了内容了.原因是ThinkPHP里的热键与系统的有冲突了,须要设置例如以 ...
- somethings about QSplitter
m_splitter = new QSplitter(Qt::Horizontal); m_splitter->addWidget(this->m_leftWidget); m ...
- nginx反向代理nginx,RealServer日志打印真实ip
title: nginx反向代理nginx,RealServer日志打印真实ip date: 2016-05-11 19:15:37 tags: --- nginx反向代理nginx,RealServ ...
- mybatis之特殊查询
在mybatis查询的过程中,某个字段是经过计算得到的,这时,在设计数据表的时候,就不 必在增加此对应的字段 那么,在查询的时候,页面有需要展示这个字段时,怎么办呢? 举个例子: 在查询微信团商品时, ...
- 服务 通话录音 TelephonyManager
MainActivity public class MainActivity extends ListActivity { private BatteryChangedReceiver rec ...