【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 ...
随机推荐
- shell两个数字的运算,一共三个变量
#!/bin/bash #两个数运算的简单脚本 + ,一共三个参数 echo $# #对获取的参数以此判断是否包含[a-zA-Z]的东西,如果包含就退出.因为数字相加不是数字就是加减乘除 for i_ ...
- CMM已经落伍了,敏捷才是王道
首先强调一下,敏捷和有没有文档一点关系都没有.我只是对于CMM的那些文档感觉有些浪费. 看看那些文档,看看那些流程.想想那些伟大的软件作品,哪个是用CMM开发出来的? 作为测试工程师,程序员的你在CM ...
- android 编译 app
有些编写的app需要放到android的源码中进行编译.放置的路径packages/apps/ 编译方法,参考 http://blog.csdn.net/luoshengyang/article/de ...
- Windows上建立、取消共享文件夹
建立共享文件夹 1.创建一个文件夹test 2.右键属性,点击共享 4.在另外一台机器上访问该共享文件 取消共享文件夹 右键属性,点击高级共享
- udp编程中,一次能发送多少个bytes为好?
在进行UDP编程的时候,我们最容易想到的问题就是,一次发送多少bytes好? 当然,这个没有唯一答案,相对于不同的系统,不同的要求,其得到的答案是不一样的,我这里仅对 像ICQ一类的发送聊天消息 ...
- Linux 获取登录者IP
在linux中有时须要获得登录者的IP,这里有两种方法.先使用who am i 获取登录IP,然后截取字符串: 1.awk截取,sed替换 who am i | awk '{print $5}' | ...
- perl 字符串比较操作符
perl 中数字和字符串的比较操作符是不一样的 : 其中 == 用于比较数字是否相等:eq 用于比较字符串是否相等: 今天找程序里的bug,结果就是这个操作符用错,哎,赶紧记一下!
- sp_configure命令开启组件Agent XPs,数据库计划(Maintenance Plan)
新建“计划(Maintenance Plan)”时,记得执行计划需把SQL的“代理服务(SQL Server Agent)”也开启 出现对话框:“SQL Server 阻止了对组件 'Agent XP ...
- Python学习笔记(一)——基本知识点
主要记录学习Python的历程和用于复习.查阅之用. 知识点: 数据类型(列表.元组.字典.集合) 帮助文档 函数(默认参数.可变参数.关键字参数.参数组合) 数据类型: 列表:list ...
- Unity5.4新版AssetBundle资源打包
(1)新版本 唯一打包API Buildpipeline.BuildAssetBundle (2)在资源的Inpector界面最下方可设置该资源的assetbundleName, 每个assetbun ...