bzoj1645 / P2061 [USACO07OPEN]城市的地平线City Horizon(扫描线)
P2061 [USACO07OPEN]城市的地平线City Horizon
扫描线
扫描线简化版
流程(本题为例):
把一个矩形用两条线段(底端点的坐标,向上长度,添加$or$删除)表示,按横坐标排序
$upd:$本题的底端点坐标简化为$(x,0)$
蓝后对纵坐标建一棵线段树(本题需要对高度进行离散化)。
每次对线段树进行覆盖$or$删除区间操作,顺便统计一下$k=$有多少点被覆盖到
而两次(线段)操作之间的长度为$r=x_{i}-x_{i-1}$
于是两条线段之间被覆盖的面积即为$k*r$
(某退役选手又一次省出了宝贵的1.5h)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 40010
struct line{
int l,h,f;// l:横坐标 h:向上高度 f:添加/删除
line(){}
line(int A,int B,int C):
l(A),h(B),f(C){}
bool operator < (const line &tmp) const{
return l<tmp.l;
}
}a[N<<];
int x[N],n,cnt,tn,sum[N<<],tag[N<<],res;
long long ans;
#define lc o<<1
#define rc o<<1|1
#define mid l+((r-l)>>1)
void upd(int o,int l,int r,line e){
if(l>=r) return; //左端点在本题中简化成0,下同
if(x[r]<=e.h) tag[o]+=e.f;//覆盖层数增加/减少
else{
upd(lc,l,mid,e);
if(e.h>x[mid]) upd(rc,mid,r,e);//注意mid~mid+1的区间不可被忽略
}sum[o]= tag[o]? x[r]-x[l]:sum[lc]+sum[rc];//是否被完全覆盖
}
int main(){
scanf("%d",&n); int q1,q2,q3;
for(int i=;i<=n;++i){
scanf("%d%d%d",&q1,&q2,&q3);
a[++cnt]=line(q1,q3,);
a[++cnt]=line(q2,q3,-);//一个矩形用两条线段表示
x[i+]=q3;//存横坐标用于离散化
}sort(a+,a+cnt+);//(线段)操作按横坐标排序
sort(x+,x+n+);x[]=-;//注意要加上坐标(0,0)
for(int i=;i<=n+;++i)
if(x[i]!=x[i-]) x[++tn]=x[i];//离散化
upd(,,tn,a[]);
for(int i=;i<=cnt;++i){
ans+=1ll*sum[]*(a[i].l-a[i-].l);//累计两条线段间的面积
upd(,,tn,a[i]);
}printf("%lld",ans);
return ;
}
bzoj1645 / P2061 [USACO07OPEN]城市的地平线City Horizon(扫描线)的更多相关文章
- 线段树+扫描线【bzoj1645】[USACO07OPEN]城市的地平线City Horizon
Description 约翰带着奶牛去都市观光.在落日的余晖里,他们看到了一幢接一幢的摩天高楼的轮廓在地平线 上形成美丽的图案.以地平线为 X 轴,每幢高楼的轮廓是一个位于地平线上的矩形,彼此间可能有 ...
- [题目] luogu P2061 [USACO07OPEN]城市的地平线City Horizon
算法 线段树 + 离散化 思路 对\((x,y,h)\)的左右端点\(x,y\)进行离散化,离散化前的原值记为\(val[i]\),对每个矩形按高度\(h\)从小到大排序. 设离散化后的端点有\(M\ ...
- 洛谷 P2061 [USACO07OPEN]城市的地平线City Horizon
简化版的矩形面积并,不用线段树,不用离散化,代码意外的简单 扫描线,这里的基本思路就是把要求的图形竖着切几刀分成许多矩形,求面积并.(切法就是每出现一条与y轴平行的线段都切一刀) 对于每一个切出来的矩 ...
- Luogu_2061_[USACO07OPEN]城市的地平线City Horizon
题目描述 Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the cit ...
- POJ 3277 City Horizon(扫描线+线段树)
题目链接 类似求面积并..2Y.. #include <cstdio> #include <cstring> #include <string> #include ...
- 【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 ...
- 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 ...
随机推荐
- 计蒜客 31452 - Supreme Number - [简单数学][2018ICPC沈阳网络预赛K题]
题目链接:https://nanti.jisuanke.com/t/31452 A prime number (or a prime) is a natural number greater than ...
- vue - vue-cli脚手架项目中组件的使用
在webpack-simple模板中,包括webpck模板.一个.vue文件就是一个组件. 为什么会这样呢?因为webpack干活了!webpack的将我们所有的资源文件进行打包.同时webpack还 ...
- table 的td 自适应事项
media的html结构,如果用display:table-cell实现, table { width: 100%; border-collapse: collapse; border: $bor; ...
- Memory consumption of popular Java data types
http://java-performance.info/memory-consumption-of-java-data-types-2/ 如何在Java中分配超过-Xmx限制的内存 http://i ...
- mysql 表基本增删查改
对表的操作是在某个数据库下才能进行的,所以要先选择数据库 "use 数据库名;" 1.创建数据表 1)"create table 表名 (字段1 类型 [约束], ...
- SQL Server中灾难时备份结尾日志(Tail of log)的两种方法
转自:http://www.cnblogs.com/CareySon/archive/2012/02/23/2365006.html SQL Server中灾难时备份结尾日志(Tail of log) ...
- chown 命令
root@localhost ~]# .txt # 同时修改文件属主和属组 !chown 执行上一个chown命令
- 深入理解Fabric环境搭建的详细过程(转)
前面的准备工作我就不用多说了,也就是各种软件和开发环境的安装,安装好以后,我们git clone下来最新的代码,并切换到v1.0.0,并且下载好我们需要使用的docker镜像,也就是到步骤6,接下来我 ...
- PAT 1066 Root of AVL Tree[AVL树][难]
1066 Root of AVL Tree (25)(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, ...
- [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...