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个用空格 ...
随机推荐
- react中需要用到【深度复制】的问题
首先,说一下我所遇到的问题,我所做的项目是用的基于react的antd框架. 一张表格,里面的数据是从后台获取直接渲染,我点击修改按钮,在modal弹框中修改数据,但是没有点击确定,点击取消,发现页面 ...
- 别跟我谈EF抵抗并发,敢问你到底会不会用EntityFramework
前言 一直以来写的博文都是比较温婉型的博文,今天这篇博文算是一篇批判性博文,有问题欢迎探讨,如标题,你到底会不会用EntityFramework啊. 你到底会不会用EntityFramework啊 面 ...
- 排序算法入门之堆排序(Java实现)
堆排序 在学习了二叉堆(优先队列)以后,我们来看看堆排序.堆排序总的运行时间为O(NlonN). 堆的概念 堆是以数组作为存储结构. 可以看出,它们满足以下规律: 设当前元素在数组中以R[i]表示,那 ...
- 使用 focus() 和 blur()
<html> <head> <style type="text/css"> a:active {color:green} </style& ...
- Activiti初学问题,求解
<userTask id="writeReportTask" name="Write monthly financial report" > < ...
- JAVA 平台
由Java虚拟机和Java核心类所构成.它为纯Java程序提供了统一的编程接口,而不管下层操作系统是什么. 目录 1Java术语 2移动平台 3桌面应用平台 4企业级平台 5JRE的成分 1J ...
- FFPLAY的原理(四)
意外情况 你们将会注意到我们有一个全局变量quit,我们用它来保证还没有设置程序退出的信号(SDL会自动处理TERM类似的信号).否则,这个线程将不停地运 行直到我们使用kill -9来结束程序.FF ...
- TabBarController和其他view无法建立Relationship segue的原因
拖拽怎么也没有那个出现,最后看sourcecode发现是那个那个viewcrontroler的XML 元素不是TabBarController.在Sourcecode里面改了一下,解决了这个问题. 总 ...
- Centos7查看IP
查看IP ip addr : lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN qlen link/loopback : ...
- 测试驱动开发 TDD
一.详解TDD 1.1.TDD概念 :Test Drived Develop 测试驱动开发是敏捷开发中的一项核心实践和技术,也是一种方法论.TDD的原理是在开发功能代码之前,编写单元测试用例代码,测试 ...