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个用空格 ...
随机推荐
- 高级私人定制西服品牌:XUAN PRIVE 为定制而生_乐活_onlylady女人志
高级私人定制西服品牌:XUAN PRIVE 为定制而生_乐活_onlylady女人志 高级私人定制西服品牌:XUAN PRIVE 为定制而生
- UVa 1366 - Martian Mining (dp)
本文出自 http://blog.csdn.net/shuangde800 题目链接: 点击打开链接 题目大意 给出n*m网格中每个格子的A矿和B矿数量,A矿必须由右向左运输,B矿必须由下向上运输 ...
- MyBatis的事务处理
先来假设这样一个问题:如果数据库里面有一个用户表和一个作家表,那么当要添加一条数据到作家表中时,作家表的id必须是用户表中的其中一个id,因为作家一定也要是一个用户.这时就涉及到事务处理. 在上一篇博 ...
- synchronized 与 Lock 的那点事
最近在做一个监控系统,该系统主要包括对数据实时分析和存储两个部分,由于并发量比较高,所以不可避免的使用到了一些并发的知识.为了实现这些要求,后台使用一个队列作为缓存,对于请求只管往缓存里写数据.同时启 ...
- 对C++默认构造函数的理解
在文章开始之前,首先指出对于c++新手的两个常见的误解: 一.任何class如果没有定义default constructor,就会被合成出一个来. 二.编译器合成出来的default constru ...
- Android Clipboard(复制/剪贴板)
Android提供的剪贴板框架,复制和粘贴不同类型的数据.数据可以是文本,图像,二进制流数据或其它复杂的数据类型. Android提供ClipboardManager.ClipData.Item和Cl ...
- Hadoop在Windows环境下的部署[转]
http://blog.csdn.net/ruby97/article/details/7423088 经过一整天的折腾,参考了网上很多资料,我机器上的Hadoop似乎是配置成功了.下面分享一下详细的 ...
- AJAX的概念介绍
AJAX学习 1.XMLHttpRequest对象创建 var request= new XMLHttpRequest(); 兼容ie6.ie5 var request; if(windoe.XMLH ...
- RPC 实现
PC,即 Remote Procedure Call(远程过程调用),说得通俗一点就是:调用远程计算机上的服务,就像调用本地服务一样. RPC 可基于 HTTP 或 TCP 协议,Web Servic ...
- passwd的使用
名称:passwd 使用权限:所有使用者 使用方式:passwd [-k] [-l] [-u [-f]] [-d] [-S] [username] 说明:用来更改使用者的密码 参数: -k keep ...