BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线
BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线
Description
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
16
裸的扫描线,这里从左往右扫。
先将x轴向上平移一个单位,避免处理麻烦的区间长度问题。
然后每个矩形拆成两条竖线,从左往右扫。
需要在线段树上维护一些点是否存在。
每个节点记录sum和raw,当sum>0时raw=r-l+1,否则等于两个儿子的raw之和,每次修改都要pushup一下。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 80050
#define maxn 1000000000
struct Line {
int y_1,y_2,x,flg;
Line() {}
Line(int y__1,int y__2,int x_,int flg_) :
y_1(y__1),y_2(y__2),x(x_),flg(flg_) {}
bool operator < (const Line &u) const {
return x<u.x;
}
}a[N];
int t[N*40],ls[N*40],rs[N*40],cnt,n,raw[N*40],add[N*40];
void pushup(int l,int r,int p) {
if(t[p]>0) raw[p]=r-l+1;
else if(l==r) raw[p]=0;
else raw[p]=raw[ls[p]]+raw[rs[p]];
}
void update(int l,int r,int x,int y,int v,int &p) {
if(!p) p=++cnt;
if(x<=l&&y>=r) {
t[p]+=v; pushup(l,r,p);
return ;
}
int mid=(l+r)>>1;
if(x<=mid) update(l,mid,x,y,v,ls[p]);
if(y>mid) update(mid+1,r,x,y,v,rs[p]);
pushup(l,r,p);
}
int main() {
scanf("%d",&n);
int i,x_1,x_2,h,tot=0;
for(i=1;i<=n;i++) {
scanf("%d%d%d",&x_1,&x_2,&h);
a[++tot]=Line(1,h,x_1,1);
a[++tot]=Line(1,h,x_2,-1);
}
sort(a+1,a+tot+1);
int root=0;
update(1,maxn,a[1].y_1,a[1].y_2,a[1].flg,root);
long long ans=0;
for(i=2;i<=tot;i++) {
ans+=1ll*(a[i].x-a[i-1].x)*raw[1];
update(1,maxn,a[i].y_1,a[i].y_2,a[i].flg,root);
}
printf("%lld\n",ans);
}
BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线的更多相关文章
- 1645: [Usaco2007 Open]City Horizon 城市地平线
1645: [Usaco2007 Open]City Horizon 城市地平线 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 315 Solved: ...
- 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树
[BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...
- 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 ...
- 【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标记即 ...
- [BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 线段树
链接 题意:N个矩形块,交求面积并. 题解 显然对于每个 \(x\),只要求出这个 \(x\) 上面最高的矩形的高度,即最大值 将矩形宽度离散化一下,高度从小到大排序,线段树区间set,然后求和即可 ...
- 【BZOJ】1628 && 1683: [Usaco2007 Demo]City skyline 城市地平线(单调栈)
http://www.lydsy.com/JudgeOnline/problem.php?id=1628 http://www.lydsy.com/JudgeOnline/problem.php?id ...
- bzoj1683[Usaco2005 Nov]City skyline 城市地平线
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1683 Input 第1行:2个用空格隔开的整数N和W. 第2到N+1行:每行包括2个用空格 ...
随机推荐
- mysql 5.7主从安装和配置
本文主要介绍mysql 5.7主从复制,转载请注明出处 下载地址 模块 版本 下载地址 mysql 5.7 https://dev.mysql.com/downloads/mysql/ libaio( ...
- [ASP.NET MVC4高级编程] 学习记录(一)
理论: 先有GUI在发展,当用户按下某个键,某个进程会监听到这个动作,这个进程就是控制器.这就是MVC模式. 后来有了事件驱动编程,响应动作的是按钮本身,而不是控制器. 再后来webForm中,事件驱 ...
- Python-Web框架之 - 利用SQLALchemy创建与数据库MySQL的连接, 详解用Flask时会遇到的一些大坑 !
经过这个小项目算是对Django与Flask这两个web框架有了新的认识 , Django本身的轮子非常齐全 , 套路也很固定 , 新手在接触Django框架时 , 不会陷入到处找轮子的大坑 ; 那么 ...
- Python import this : The Zen of Python
>>> import thisThe Zen of Python, by Tim Peters Beautiful is better than ugly.Explicit is b ...
- Java自学开发编程路线图(文中有资源福利)
Java 语言入门 免费视频资源<毕向东Java基础教程>:http://yun.itheima.com/course/7.html JavaEE 学习大纲 所处阶段 主讲内容 技术要点 ...
- 基于Cloudera Search设计数据灾备方案
当实际项目上线到生产环境中,难以避免一些意外情况,如数据丢失.服务器停机等.对于系统的搜索服务来说,当遇到停机的情况意味着在停机这段时间内,用户都不能通过搜索的相关功能进行访问数据,停机意味着将这一段 ...
- Java基础:内存模型
1. 引言 2. Java内存模型 3. 内存间的交互操作 1. 引言 考虑到计算机组成的内容: 原始的计算机是CPU用于计算+硬盘用于存储,由于CPU的高速发展和硬盘的缓慢发展,高速的存储需要持续供 ...
- Lua读取CSV文件到table中
创建Lua函数载入CSV文件并保存到表中的函数: function GetLines(fileName) indx = 0 myLines ={} for line in io.line(string ...
- Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.sup
解决:找不到资源文件: 系统会根据分辨率来选择加载不同drawable下文件夹的资源,如果只在一个文件下放了资源文件,不同的分辨率设备的会报错.
- POI实现Excel导入导出
我们知道要创建一张excel你得知道excel由什么组成,比如说sheet也就是一个工作表格,例如一行,一个单元格,单元格格式,单元格内容格式…这些都对应着poi里面的一个类. 一个excel表格: ...