【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树
【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 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.
题解:一看就是离散化+线段树的题,可以先将h离散,再按a,b排序,用线段树维护扫描线来做,不过这样比较麻烦。
因为本题中所有的矩形底边都在同一直线上,所以我们可以把这些矩形整体看成一棵线段树,线段树的区间就是每个a,b,线段树的权值就是h,然后按h排序,从小到大一个一个加到线段树里就行了。
注意:a和b离散化后是2*40000的空间,所以线段树要开8*40000!2*4==8!!!
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lson x<<1
#define rson x<<1|1
using namespace std;
const int maxn=40010;
typedef long long ll;
struct edges
{
ll pos,org;
}s[maxn<<1];
struct house
{
ll sa,sb,sh;
}p[maxn];
int n,m;
ll ts[maxn<<3],tag[maxn<<3],ref[maxn<<1];
bool cmp1(edges a,edges b)
{
return a.pos<b.pos;
}
bool cmp2(house a,house b)
{
return a.sh<b.sh;
}
void pushdown(int l,int r,int x)
{
if(tag[x])
{
int mid=l+r>>1;
ts[lson]=(ref[mid]-ref[l])*tag[x],ts[rson]=(ref[r]-ref[mid])*tag[x];
tag[lson]=tag[rson]=tag[x];
tag[x]=0;
}
}
void pushup(int x)
{
ts[x]=ts[lson]+ts[rson];
}
void updata(int l,int r,int x,int a,int b,ll v)
{
if(a<=l&&r<=b)
{
ts[x]=(ref[r]-ref[l])*v;
tag[x]=v;
return ;
}
pushdown(l,r,x);
int mid=l+r>>1;
if(b<=mid) updata(l,mid,lson,a,b,v);
else if(a>=mid) updata(mid,r,rson,a,b,v);
else updata(l,mid,lson,a,b,v),updata(mid,r,rson,a,b,v);
pushup(x);
}
int main()
{
scanf("%d",&n);
int i;
for(i=1;i<=n;i++)
{
scanf("%lld%lld%lld",&p[i].sa,&p[i].sb,&p[i].sh);
s[i*2-1].pos=p[i].sa,s[i*2].pos=p[i].sb;
s[i*2-1].org=s[i*2].org=i;
}
sort(s+1,s+2*n+1,cmp1);
for(i=1;i<=2*n;i++)
{
if(s[i].pos>ref[m]) ref[++m]=s[i].pos;
if(s[i].pos==p[s[i].org].sa) p[s[i].org].sa=m;
else p[s[i].org].sb=m;
}
sort(p+1,p+n+1,cmp2);
for(i=1;i<=n;i++)
updata(1,m,1,p[i].sa,p[i].sb,p[i].sh);
printf("%lld",ts[1]);
return 0;
}
【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树的更多相关文章
- BZOJ 1645: [Usaco2007 Open]City Horizon 城市地平线 扫描线 + 线段树 + 离散化
Code: #include<cstdio> #include<algorithm> #include<string> #define maxn 1030000 # ...
- 【BZOJ】1645: [Usaco2007 Open]City Horizon 城市地平线(线段树+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1645 这题的方法很奇妙啊...一开始我打了一个“离散”后的线段树.............果然爆了. ...
- bzoj 1645: [Usaco2007 Open]City Horizon 城市地平线【线段树+hash】
bzoj题面什么鬼啊-- 题目大意:有一个初始值均为0的数列,n次操作,每次将数列(ai,bi-1)这个区间中的数与ci取max,问n次后元素和 离散化,然后建立线段树,每次修改在区间上打max标记即 ...
- 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,然后求和即可 ...
- 1645: [Usaco2007 Open]City Horizon 城市地平线
1645: [Usaco2007 Open]City Horizon 城市地平线 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 315 Solved: ...
- BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线
BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线 Description N个矩形块,交求面积并. Input * Line 1: A single i ...
- 【BZOJ】1628 && 1683: [Usaco2007 Demo]City skyline 城市地平线(单调栈)
http://www.lydsy.com/JudgeOnline/problem.php?id=1628 http://www.lydsy.com/JudgeOnline/problem.php?id ...
- 南阳理工 题目9:posters(离散化+线段树)
posters 时间限制:1000 ms | 内存限制:65535 KB 难度:6 描述 The citizens of Bytetown, AB, could not stand that ...
随机推荐
- 转00600异常解决方案:ORA-00600: 内部错误代码, 参数: [19004], [], [], [], [], []
<问题描述> ORACLE 10.1 OR 10.2中所有平台都存在该问题. <问题现象> 在进行多表关联复杂查询时出现 ORA-00600: 内部错误代码, 参数: [190 ...
- snmp trap编写
1.MIB库查看net-snmp的安装目录./usr/share/snmp/mibs目录下: NET-SNMP-EXAMPLES-MIB.mib本件部分内容如下: netSnmpExampleHear ...
- 第三百一十三节,Django框架,Session
第三百一十三节,Django框架,Session Django中默认支持Session,其内部提供了5种类型的Session供开发者使用: 1.数据库(默认)2.缓存3.文件4.缓存+数据库5.加密c ...
- webBrowser1.Document.Cookie取不到HttpOnly的Cookie,取Cookie不完整【转】
在做数据采集时,有些网站需要输入验证码,但各网站验证码都不同,不可能有完美的识别验证码的代码,所以我也没去研究,我所采取的方案是:在winform里通过WebBrowser调用网页先手动登录系统,然后 ...
- php -- 实现linux关机、重启功能
有时候,我们自己可以DIY一个控制面板实现linux的关机重启功能.众所周知,linux是一个基于文件的操作系统,所以要实现系统的关机重启功能必须满足以下两点 一.知道命令的绝对路径 在linux下操 ...
- 基于swoole扩展实现真正的PHP数据库连接池
转自: http://rango.swoole.com/archives/265 PHP的数据库连接池一直以来都是一个难题,很多从PHP语言转向Java的项目,大多数原因都是因为Java有更好的连接 ...
- mac下安装apc并且使用
1.到网站下载对应PHP版本apc压缩包http://git.php.net/?p=pecl/caching/apc.git;a=commit;h=08e2ce7ab5f59aea483d877e2b ...
- Java基础--生成验证码
HTML <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnco ...
- 为什么MathType窗口变灰色
mathtype是一个功能强大的数学公式编辑器,可以轻松输入各种复杂的公式和符号,与Office文档完美结合,显示效果超好,比Office自带的公式编辑器要强大很多.但我们在使用MathType编辑公 ...
- WPF 本地化(多语言)
如果你的程序需要本地化,考虑的因素诸多,例如:当文本改变后,控件的当前高度,宽度 是否合适.所在的位置是否合适.字体.布局是否合适?如果已经构建了一个真正自适应的布局,就不会有问题.用户界面应当能够调 ...