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个用空格 ...
随机推荐
- C++笔记十七:C语言中 “冒牌货”const和const符号表
在.c文件中有程序: int main() { int const a = 10; a=20; printf("a=%d\n",a); return 0; } 编译就知道C语言 ...
- Construct Binary Tree from Preorder and Inorder Traversal(根据前序中序构建二叉树)
根据前序中序构建二叉树. 1 / \ 2 3 / \ / \ 4 5 6 7对于上图的树来说, index: 0 1 2 3 4 5 6 先序遍历为: 6 3 7为了清晰表示,我给节点上了颜色,红色是 ...
- 树的广度优先遍历和深度优先遍历(递归非递归、Java实现)
在编程生活中,我们总会遇见树性结构,这几天刚好需要对树形结构操作,就记录下自己的操作方式以及过程.现在假设有一颗这样树,(是不是二叉树都没关系,原理都是一样的) 1.广度优先遍历 英文缩写为BFS即B ...
- C#程序自动更新软件版本号
最近因为服务器程序管理多,所以在查看服务器程序的时候,只能通过EXE的编译时间来判断服务器程序版本时间,费神伤身啊 现在想了一个方式,在目录下新增一个version文件,里面写上年月日,并且只是在程序 ...
- 与班尼特·胡迪一起拿奖学金(HZNU-2273)
与班尼特·胡迪一起拿奖学金 AC Time Limit: 2 s Memory Limit: 256 MB Description 班尼特·胡迪这学期的体测终于上80分了,当期末考试的 ...
- MariaDB/MySQL备份和恢复(一):mysqldump工具用法详述
本文目录:1.备份分类2.备份内容和备份工具3.mysqldump用法详述 3.1 语法选项 3.1.1 连接选项 3.1.2 筛选选项 3.1.3 DDL选项 3.1.4 字符集选项 3.1.5 复 ...
- PHP 中的 __FILE__ 和__DIR__常量
__DIR__ :当前内容写在哪个文件就显示这个文件目录 __FILE__ : 当前内容写在哪个文件就显示这个文件目录+文件名 比如文件 b.php 包含如下内容: <?php $basedir ...
- Laravel 框架 基础(一)
Laravel 框架 laravel 5.2 在 5.1 基础上继续改进和优化,添加了许多新的功能特性:多认证驱动支持.隐式模型绑定.简化 Eloquent 全局作用域.可选择的认证脚手架.中间件组. ...
- spring 整合 shiro框架
shiro是用来干嘛的?从它的官网上(http://shiro.apache.org/)基本可以了解到,她主要提供以下功能: (1)Authentication(认证) (2)Authorizatio ...
- python_方法说明
方法用来描述对象所具有的行为,例如,列表对象的追加元素.插入元素.删除原宿.排序,字符串对象的分隔.连接.排版.替换.烤箱的温度设置.烘烤,等等 在类中定义的方法可以粗略分为四大类:公有方法.私有方法 ...