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个用空格 ...
随机推荐
- Solr 新增、更新、删除索引
solr-admin新增索引 [索引中无则新增,有则更新] 1.在doc标签和field标签中增加权重(boost),增加权重后,可以在搜索的时候做权重过滤. <add> <doc ...
- leetcode刷题指南
转载自:http://blog.csdn.net/lnho2015/article/details/50962989 以下是我个人做题过程中的一些体会: 1. LeetCode的题库越来越大,截止到目 ...
- 修改was数据源
本机的RAD运行的工程可以通过修改jpa中的persistence中的jni修改数据源: 对于通过was控制台部署的ear需要在was控制台:资源--jdbc 修改数据源
- Layx——网页弹窗最佳选择.
开源地址:https://gitee.com/monksoul/LayX 文档地址:http://baisoft.gotoip11.com/layx/doc/ 大家好,我叫百小僧,是一名C#开发工程师 ...
- Java学习不走弯路教程(7.Eclipse环境搭建)
7.Eclipse环境搭建 在前几章,我们熟悉了DOS环境下编译和运行Java程序,对于大规模的程序编写,开发工具是必不可少的.Java的开发工具比较常用的是Eclipse.在接下来的教程中,我们将基 ...
- 【CloverETL培训】题目
具体要求: 导入: 1.在CRM中,创建相应物理表,存储Follow/Binding记录.openid作为逻辑主键 2.Follow/Binding导入相互不影响,一个失败另外一个继续执行 3.Fol ...
- ruby簡單的代碼行統計工具
看代码 # encoding: utf-8 class CodeLineStat attr_reader :code_lines def initialize @code_lines = 0 end ...
- springboot中使用分页,文件上传,jquery的具体步骤(持续更新)
分页: pom.xml 加依赖 <dependency> <groupId>com.github.pagehelper</groupId> <arti ...
- erlang的脚本执行---escript
1.概述: 作为程序员对于脚本语言应该很熟悉了,脚本语言的优点很多,如快速开发.容易编写.实时开发和执行, 我们常用的脚本有Javascript.shell.python等,我们的erlang语言也有 ...
- XGBoost算法--学习笔记
学习背景 最近想要学习和实现一下XGBoost算法,原因是最近对项目有些想法,准备做个回归预测.作为当下比较火的回归预测算法,准备直接套用试试效果. 一.基础知识 (1)泰勒公式 泰勒公式是一个用函数 ...