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(扫描线)的更多相关文章

  1. 线段树+扫描线【bzoj1645】[USACO07OPEN]城市的地平线City Horizon

    Description 约翰带着奶牛去都市观光.在落日的余晖里,他们看到了一幢接一幢的摩天高楼的轮廓在地平线 上形成美丽的图案.以地平线为 X 轴,每幢高楼的轮廓是一个位于地平线上的矩形,彼此间可能有 ...

  2. [题目] luogu P2061 [USACO07OPEN]城市的地平线City Horizon

    算法 线段树 + 离散化 思路 对\((x,y,h)\)的左右端点\(x,y\)进行离散化,离散化前的原值记为\(val[i]\),对每个矩形按高度\(h\)从小到大排序. 设离散化后的端点有\(M\ ...

  3. 洛谷 P2061 [USACO07OPEN]城市的地平线City Horizon

    简化版的矩形面积并,不用线段树,不用离散化,代码意外的简单 扫描线,这里的基本思路就是把要求的图形竖着切几刀分成许多矩形,求面积并.(切法就是每出现一条与y轴平行的线段都切一刀) 对于每一个切出来的矩 ...

  4. 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 ...

  5. POJ 3277 City Horizon(扫描线+线段树)

    题目链接 类似求面积并..2Y.. #include <cstdio> #include <cstring> #include <string> #include ...

  6. 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树

    [BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...

  7. 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 ...

  8. 1645: [Usaco2007 Open]City Horizon 城市地平线

    1645: [Usaco2007 Open]City Horizon 城市地平线 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 315  Solved: ...

  9. BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线

    BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线 Description N个矩形块,交求面积并. Input * Line 1: A single i ...

随机推荐

  1. hdu2896 病毒肆虐【AC自动机】

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. MapRedece(多表关联)

    多表关联: 准备数据 ******************************************** 工厂表: Factory Addressed BeijingRedStar 1 Shen ...

  3. codeforces 355C - Vasya and Robot

    因为在允许的情况下,必然是左右手交替进行,这样不会增加多余的无谓的能量. 然后根据不同的分界点,肯定会产生左手或右手重复使用的情况,这是就要加上Qr/Ql * 次数. 一开始的想法,很直接,枚举每个分 ...

  4. HDU - 5651 xiaoxin juju needs help 逆元模板

    http://acm.hdu.edu.cn/showproblem.php?pid=5651 题意:生成回文串.输出所有回文串的可能数. 题解:mod除法会损失高位,用逆元来代替除法,模板如下 ac代 ...

  5. 使用QT设计师-信号和槽signal-slot(第一弹)

    自定义信号和槽的步骤: 1.定义信号---signal1 = pyqtSignal() 2.定义槽信号---def setSlot(self): 3.连接信号和槽函数---signal1.connec ...

  6. Java List <T> T[] toArray(T[] a) implementation

    Like the toArray() method, this method acts as bridge between array-based and collection-based APIs. ...

  7. mysql 权限管理 针对某个库 某张表 授权 tables_priv表

    精确到表级别 针对db1的t3表 授予select权限 mysql> grant select on db1.t3 to 'mike'@'localhost'; Query OK, rows a ...

  8. vue:Group XSwitch Actionsheet,Toast控件使用

    <template> <div> <div class="vux-demo"> <img class="logo" s ...

  9. Windows操作系统上各种服务使用的端口号, 以及它们使用的协议的列表

    Windows操作系统上各种服务使用的端口号, 以及它们使用的协议的列表 列表如下 Port Protocol Network Service System Service System Servic ...

  10. adb server version (31) doesn't match this client (36)

    运行adb 命令的时候报错: C:\Users\Administrator>adb devices List of devices attachedadb server version (31) ...