[BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 线段树
题意:N个矩形块,交求面积并.
题解
显然对于每个 \(x\),只要求出这个 \(x\) 上面最高的矩形的高度,即最大值
将矩形宽度离散化一下,高度从小到大排序,线段树区间set,然后求和即可
注意它给的矩形区间是 \([L,R)\) 这样在线段树里直接维护 \([L,R)\) 这样的区间即可
#include<bits/stdc++.h>
#define REP(i,a,b) for(int i(a);i<=(b);++i)
using namespace std;
typedef long long ll;
inline int read(){char c;int w;
while(!isdigit(c=getchar()));w=c&15;
while(isdigit(c=getchar()))w=w*10+(c&15);return w;
}
inline char smax(int&x,const int&y){return x<y?x=y,1:0;}
inline char smin(int&x,const int&y){return x>y?x=y,1:0;}
const int N=40005;
struct data{int l,r,h;}a[N];
inline bool cmp(data a,data b){return a.h<b.h;}
int n,b[N<<1],m,tag[N<<3];
ll sum[N<<3];
#define ls o<<1
#define rs o<<1|1
inline void update(int o,int l,int r,int x,int y,int z){
assert(l>=1&&r<=m);
if(x<=l&&r<=y){tag[o]=z,sum[o]=1ll*z*(b[r]-b[l]);return;}
int mid=l+r>>1;
if(tag[o]){
tag[ls]=tag[rs]=tag[o];
sum[ls]=1ll*tag[o]*(b[mid]-b[l]);
sum[rs]=1ll*tag[o]*(b[r]-b[mid]);
tag[o]=0;
}
if(x<mid)update(ls,l,mid,x,y,z);
if(y>mid)update(rs,mid,r,x,y,z);
sum[o]=sum[ls]+sum[rs];
}
signed main(){
n=read();
REP(i,1,n)a[i]=(data){read(),read(),read()},b[++m]=a[i].l,b[++m]=a[i].r;
sort(a+1,a+1+n,cmp);sort(b+1,b+1+m);m=unique(b+1,b+1+m)-b-1;
REP(i,1,n){
a[i].l=lower_bound(b+1,b+1+m,a[i].l)-b;
a[i].r=lower_bound(b+1,b+1+m,a[i].r)-b;
update(1,1,m,a[i].l,a[i].r,a[i].h);
}
printf("%lld\n",sum[1]);
return 0;
}
[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 ...
- 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树
[BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...
- 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】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标记即 ...
- [POJ] 3277 .City Horizon(离散+线段树)
来自这两篇博客的总结 http://blog.csdn.net/SunnyYoona/article/details/43938355 http://m.blog.csdn.net/blog/mr_z ...
- 【BZOJ】1628 && 1683: [Usaco2007 Demo]City skyline 城市地平线(单调栈)
http://www.lydsy.com/JudgeOnline/problem.php?id=1628 http://www.lydsy.com/JudgeOnline/problem.php?id ...
随机推荐
- COGS——T 1215. [Tyvj Aug11] 冗余电网
http://www.cogs.pro/cogs/problem/problem.php?pid=1215 ★ 输入文件:ugrid.in 输出文件:ugrid.out 简单对比时间限制: ...
- cxf 实例解读
1.sample 实例之一---java_first_pojo 服务端发布服务的方法: 1 HelloWorldImpl helloworldImpl = new HelloWorldImpl(); ...
- POJ 1035-Spell checker(字符串)
题目地址:POJ 1035 题意:输入一部字典.输入若干单词. 若某个单词能在字典中找到,则输出corret.若某个单词能通过 变换 或 删除 或 加入一个字符后.在字典中找得到.则输出这些单词.输出 ...
- 赵雅智_运用Bitmap和Canvas实现图片显示,缩小,旋转,水印
上一篇已经介绍了Android种Bitmap和Canvas的使用,以下我们来写一个详细实例 http://blog.csdn.net/zhaoyazhi2129/article/details/321 ...
- 【图像配准】基于互信息的图像配准算法:MI、EMI、ECC算法
简单介绍: 基于互信息的图像配准算法以其较高的配准精度和广泛的适用性而成为图像配准领域研究的热点之中的一个.而基于互信息的医学图像配准方法被觉得是最好的配准方法之中的一个.基于此.本文将介绍简单的基于 ...
- cocos2d-x 3.1.1学习笔记[23]寻找主循环 mainloop
文章出自于 http://blog.csdn.net/zhouyunxuan cocos2d到底是怎样把场景展示给我们的,我一直非常好奇. 凭个人猜想,引擎内部的结构类似于这样 while(true ...
- spring IOC --- 控制反转(依赖注入)----简单的实例
IoC(Inversion of Control)控制反转,对象创建责任的反转,在spring中BeanFacotory是IoC容器的核心接口,负责实例化,定位,配置应用程序中的对象及建立这些对象间的 ...
- 在navicat中如何新建连接数据库
前几天给大家分享了如何安装Navicat,没有来得及上车的小伙伴可以戳这篇文章:手把手教你安装Navicat——靠谱的Navicat安装教程.今天给大家分享一下Navicat的简单使用教程,具体的教程 ...
- 原型,构造函数,实例,__proto__
再说说__proto__,这个孩子性格慢向,所以即使在现代浏览器广为支持得今天也不建议使用,性能特别慢,而且影响所有来自该[[prototype]]的对象.只是拿出来了解了解: 1.它是个啥? 原型对 ...
- tensorflow学习之路---Session、Variable(变量)和placeholder
---恢复内容开始--- 1.Session '''Session.run():首先里面的参数是一个API(函数的接口)的返回值或者是指定参数的值:功能:得知运算结果有两种访问方式:直接建立或者运用w ...