[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 ...
随机推荐
- Firefox中使用pac
https://campus.barracuda.com/product/websecurityservice/article/WSS/ConfigProxyWithPACFile/ https:// ...
- Lesson 2 Building your first web page: Part 3
Time to build your first HTML page by hand I could go on with more theory and send half of you to sl ...
- orm 通用方法——QueryModelById 主键查询
方法定义: /** * 描述:根据主键查询 * 作者:Tianqi * 日期:2014-09-15 * param:model 对象实例,包含主键 * return:对象 * */ func Quer ...
- Linux下配置Squid基础教程
Linux下配置Squid基础教程 本视频高清下载地址:http://down.51cto.com/data/437529 本文出自 "李晨光原创技术博客" 博客,请务必保留此出处 ...
- 手把手教你如何在Ubuntu系统中安装Pycharm
前几天带大家一起安装了Ubuntu14.04系统,没来得及上车的伙伴可以戳这篇文章:手把手教你在VMware虚拟机中安装Ubuntu14.04系统.今天小编带大家一起在Ubuntu14.04中安装Py ...
- 2017-2018年红头发新版Cisco认证网络工程师(CCNA-R&S)全新讲解分享
网名"红头发",多年授课经验,业内资深思科认证讲师,其所写的CISCO认证原创技术文章风靡各大网站与培训机构.精通CISCO各类路由交换产品,熟悉JUNIPER M/T系列路由产品 ...
- jQuery获取区间随机数
1.自定义函数 function getRandom(min,max){ //x上限,y下限 var x = max; var y = min; if(x<y){ ...
- K-近邻算法学习
# -- coding: utf-8 -- from numpy import * import operator def createDataSet(): group = array([[1.0,1 ...
- 一个Web报表项目的性能分析和优化实践(六):设置MySQL的最大连接数(max_connections)
在上一篇文章中"一个Web报表项目的性能分析和优化实践(二):MySQL数据库连接不够用(TooManyConnections)问题的一次分析和解决案例"提到,项目中新增几个数据库 ...
- Spark 性能相关參数配置具体解释-任务调度篇
作者:刘旭晖 Raymond 转载请注明出处 Email:colorant at 163.com BLOG:http://blog.csdn.net/colorant/ 随着Spark的逐渐成熟完好, ...