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

4
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 城市地平线_扫描线的更多相关文章

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

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

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

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

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

  4. 【BZOJ】1645: [Usaco2007 Open]City Horizon 城市地平线(线段树+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1645 这题的方法很奇妙啊...一开始我打了一个“离散”后的线段树.............果然爆了. ...

  5. BZOJ 1645: [Usaco2007 Open]City Horizon 城市地平线 扫描线 + 线段树 + 离散化

    Code: #include<cstdio> #include<algorithm> #include<string> #define maxn 1030000 # ...

  6. bzoj 1645: [Usaco2007 Open]City Horizon 城市地平线【线段树+hash】

    bzoj题面什么鬼啊-- 题目大意:有一个初始值均为0的数列,n次操作,每次将数列(ai,bi-1)这个区间中的数与ci取max,问n次后元素和 离散化,然后建立线段树,每次修改在区间上打max标记即 ...

  7. [BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 线段树

    链接 题意:N个矩形块,交求面积并. 题解 显然对于每个 \(x\),只要求出这个 \(x\) 上面最高的矩形的高度,即最大值 将矩形宽度离散化一下,高度从小到大排序,线段树区间set,然后求和即可 ...

  8. 【BZOJ】1628 && 1683: [Usaco2007 Demo]City skyline 城市地平线(单调栈)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1628 http://www.lydsy.com/JudgeOnline/problem.php?id ...

  9. bzoj1683[Usaco2005 Nov]City skyline 城市地平线

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1683 Input 第1行:2个用空格隔开的整数N和W. 第2到N+1行:每行包括2个用空格 ...

随机推荐

  1. 2018年,请不要再使用OLE生成EXCEL文件

    输出EXCEL文件是ABAP开发工作中的常见需求,为了学习相关技术,我翻译过一篇文章:使用OLE2对象创建EXCEL文件,并且一度乐在其中. 最近几个月,经过与若干EXCEL打印程序的艰苦斗争,以及对 ...

  2. Gulp基础知识

    首先,我们需要了解Gulp能做些什么? 编译 sass                                        sass是什么?(使CSS可以用编程的方式写,加快我们开发的速度) ...

  3. 【个人学习笔记】走近H5

    一.HTML5概述 1.HTML5新特性 兼容性(ie9+).合理性.效率.安全性.分离.简化.通用性.无插件 2.HTML5构成 主要包括下面这些功能:Canvas(2D和3D).Channel消息 ...

  4. two sum II

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  5. AS3中的mouseEnabled与mouseChild

    InteractiveObject类的一个属性,InteractiveObject类是用户可以使用鼠标和键盘与之交互的所有显示对象的抽象基类.我们不能直接实例化InteractiveObject类.m ...

  6. Netstat状态分类

    用netstat -an命令查看!再stat下面有一些英文,简单说一下这些英文具体都代表什么: LISTEN:(Listening for a connection.)侦听来自远方的TCP端口的连接请 ...

  7. Java 中遇到null 和为空的情况,使用Optional来解决。

    Java 中遇到null 和为空的情况,使用Optional来解决 示例代码: package crazy; import java.util.Optional; class Company { pr ...

  8. Linux服务器安全审计工具与流程完全指南

    http://Linux.chinaitlab.com/server/860516.html 当今许多linux服务器都不是刚刚部署完毕的新机器,有专业的Linux系统管理员进行定期维护,IT技术人员 ...

  9. CSS样式渐变代码,兼容IE8

    background: -webkit-linear-gradient(top,#ffffff,#f5f5f5); background: -moz-linear-gradient(top,#ffff ...

  10. .NET开发微信小程序-生成二维码

    1.生成小程序二维码功能 直接请求相应的链接.传递相应的参数 以生成商铺的付款码为例: var shopsId = e.ShopsId //付款码的参数 var codeModel = new fun ...