【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 ...
随机推荐
- CDH离线安装
1. 安装准备 系统:Centos 6 Cloudera Manager分配如下: 安装版本:CDH-5.8.0 所需安装文件 CDH相关 CDH-5.8.0-1.cdh5.8.0.p0.42-el6 ...
- css 3 制作水波状进度条
效果图如下 : 代码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- VS2010 快捷键设置,快速编码1
全屏:Shift+Alt+Enter注释选定内容:Ctrl+E+C/Crtr+E+U代码格式化:ctrl+E+F VS2008 使用小技巧——快捷键1. 怎样调整代码排版的格式?选择:编辑—>高 ...
- 从头认识java-18.2 主要的线程机制(7)join
这一章节我们来讨论一下join. 1.什么是join? 阻塞当前线程,让.join的线程先做完.然后再继续当前线程 以下是api的解释: A.join,在API中的解释是,阻塞当前线程B,直到A运行完 ...
- lseek函数与文件空洞
在UNIX/LINUX系统中,文件位移量可以大于文件的当前长度,这种情况下向文件中写入数据就会产生文件空洞(hole),这些没写入数据的文件空洞部分默认会被0填满.虽然这些文件空洞并没有实际的数据,但 ...
- input文本框与图片的对齐
<input name="demo" style="width:100px;vertical-align:top" /> <img src=& ...
- Mysql利用match...against进行全文检索
在电商项目中,最核心的功能之一就是搜索功能,搜索做的好,整个电商平台就是个优秀的平台.一般搜索功能都使用搜索引擎如Lucene.solr.elasticsearch等,虽然这功能比较强大,但是对于一些 ...
- MyEclipse安装EGit插件方法
摘要: 先了解自己的MyEclipse是基于哪个版本的Eclipse优化的,然后到EGit网站找对应的EGit版本,下载压缩包,解压安装. 在网上看到有人说并不是任意版本的EGit插件都能适配你的My ...
- 笔记:写Java程序,并输出
建一个文件名为 demo.java的文件 //写框架文件 public class Demo{ //写入口文件 public static void main(String[] args){ Syst ...
- swift - UISlider 的用法
swift的UISlider的用法和oc基本没有区别 1.创建 class SecondViewController: UIViewController { var slider = UISlider ...