BZOJ 1645: [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 the city horizon and observe the beautiful silhouettes formed by the rectangular buildings. The entire horizon is represented by a number line with N (1 <= N <= 40,000) buildings. Building i's silhouette has a base that spans locations A_i through B_i along the horizon (1 <= A_i < B_i <= 1,000,000,000) and has height H_i (1 <= H_i <= 1,000,000,000). Determine the area, in square units, of the aggregate silhouette formed by all N buildings.
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
#include<cstdio>
#include<algorithm>
#include<string>
#define maxn 1030000
#define inf 300000
#define ll long long
using namespace std;
void setIO(string s)
{
string in=s+".in";
freopen(in.c_str(),"r",stdin);
}
ll Arr[maxn];
namespace tr
{
#define mid ((l+r)>>1)
#define lson t[x].l
#define rson t[x].r
struct Node
{
int l,r,sum;
ll len;
}t[maxn<<2];
int tot;
int newnode(){ return ++tot; }
void pushup(int x,int l,int r)
{
if(t[x].sum)
{
t[x].len=Arr[r]-Arr[l-1];
}
else
{
t[x].len=t[lson].len+t[rson].len;
}
}
// 应为 > L (左面是开的)
void Update(int &x,int l,int r,int L,int R,int v)
{
if(!x) x = newnode();
if(l>=L&&r<=R)
{
t[x].sum+=v;
pushup(x,l,r);
return;
}
if(L<=mid) Update(lson,l,mid,L,R,v);
if(R>mid) Update(rson,mid+1,r,L,R,v);
pushup(x,l,r);
}
void re()
{
for(int i=0;i<=tot;++i) t[tot].l=t[tot].r=t[tot].sum=t[tot].len=0;
tot=0;
}
};
struct Edge
{
ll l,r,h;
int L,R;
int flag;
}edges[maxn];
bool cmp(Edge a,Edge b)
{
return a.h==b.h?a.flag>b.flag:a.h<b.h;
}
int i,j,n,root,ed,cc,dd;
ll ans=0,a,b,c,d;
int main()
{
// setIO("input");
scanf("%d",&n);
ed=root=cc=0;
ans=0;
for(i=1;i<=n;++i)
{
scanf("%lld%lld%lld",&a,&c,&d), b = 0;
edges[++ed].l=a,edges[ed].r=c,edges[ed].h=b,edges[ed].flag=1;
edges[++ed].l=a,edges[ed].r=c,edges[ed].h=d,edges[ed].flag=-1;
Arr[++cc]=a, Arr[++cc]=c;
}
sort(edges+1,edges+1+ed,cmp);
sort(Arr+1,Arr+1+cc);
dd=unique(Arr+1,Arr+cc+1)-(Arr+1);
for(i=1;i<=ed;++i)
{
edges[i].L=lower_bound(Arr+1,Arr+1+dd,edges[i].l)-Arr;
edges[i].R=lower_bound(Arr+1,Arr+1+dd,edges[i].r)-Arr;
}
for(i=1;i<ed;++i)
{
tr::Update(root,0,inf,edges[i].L+1,edges[i].R,edges[i].flag);
ans+=(ll)(tr::t[root].len)*(edges[i+1].h-edges[i].h);
}
printf("%lld\n",ans);
tr::re();
return 0;
}
BZOJ 1645: [Usaco2007 Open]City Horizon 城市地平线 扫描线 + 线段树 + 离散化的更多相关文章
- bzoj 1645: [Usaco2007 Open]City Horizon 城市地平线【线段树+hash】
bzoj题面什么鬼啊-- 题目大意:有一个初始值均为0的数列,n次操作,每次将数列(ai,bi-1)这个区间中的数与ci取max,问n次后元素和 离散化,然后建立线段树,每次修改在区间上打max标记即 ...
- 【BZOJ】1645: [Usaco2007 Open]City Horizon 城市地平线(线段树+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1645 这题的方法很奇妙啊...一开始我打了一个“离散”后的线段树.............果然爆了. ...
- 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 ...
- 【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 ...
- [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 ...
- 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex
题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...
随机推荐
- 用sqlldr导入csv文件
1.新建文件test.ctl,内容如下 load dataCHARACTERSET 'UTF16' \*指定编码格式,很重要*\ infile 'vodall.csv' append into tab ...
- F2BPM 开发Api与RESTfull应用服务Api 层次关系及示例
目前越来越多的企业架构解决方案更加趋向于基于http协议“微服务”访问跨系统调用,而不使用统传的WebService调用,即通过RESTfull方式进行交互,更加轻量整合调用更加方便.本文档中所有F2 ...
- 史上最全opencv源代码解读,opencv源代码具体解读文件夹
本博原创,如有转载请注明本博网址http://blog.csdn.net/ding977921830/article/details/46799043. opencv源代码主要是基于adaboost算 ...
- Unreal Engine 4 Camera Lag(摄影机延迟)
以官方的Third Person Template为样例,Character蓝图中的USpringArmComponent就实现了摄影机和场景碰撞和交互等大部分的功能了. 要实现摄影机延时,仅仅须要改 ...
- hdu1542 线段树+扫描线+离散化
仅仅想说题目给的欲实际不服 还是这类型的水题吧 建议看之前我写的那个 #include<stdio.h> #include<string.h> #include&l ...
- SharePoint 2013 Silverlight中使用Netclient对象模型
1.创建Silverlight时,选择Silverlight 4.不要选择版本号5,试了非常久版本号5都调用不了,自己也不知道什么原因.谷歌也没找到答案.后来尝试版本号4,能够调用. 至于Host t ...
- bootstrap简单form表单样式-form-horizontal
jsp代码: <div id="content" style="background-color: white;"> <form class= ...
- centos7 二次封装定制
- Android中使用Gson解析JSON数据
Android中使用Gson解析JSON数据 在Android中可以使用Gson解析JSON数据 首先,从 code.google.com/p/google-gson/downloads/list ...
- Webpack 2.0 的文档
Webpack 2.0 的文档 https://webpack.js.org/get-started/