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 城市地平线 扫描线 + 线段树 + 离散化的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

  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. 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex

    题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...

随机推荐

  1. Spring MVC-处理程序映射(Handler Mapping)-简单的Url处理程序映射(Simple Url Handler Mapping)示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_simpleurlhandlermapping.htm 说明:示例基于Spring ...

  2. 多个结果显示成一个group_concat函数

    需求:获取班级.课程中文名.老师 扩展:一个班级一门课程,老师可能多个,想把多个教师显示成在一个结果里 解决方案:加个group by 参考资料:https://www.cnblogs.com/zhu ...

  3. Android - Fragment BackStack 清空

    Fragment BackStack 清空 int backStackCount = getFragmentManager().getBackStackEntryCount(); for(int i ...

  4. 杂项:JavaScript

    ylbtech-杂项:JavaScript JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广 ...

  5. PCB Genesis脚本C#使用WPF窗体实现方法

    用C#写脚本做UI界面基本上都是用WinForm界面,如果想制作很漂亮动态的界面用WPF界面挺不错的选择, 这里介绍如何使用控制台程序调用WPF窗口 一.方法一 在控制台程序中,通过Main方法启动W ...

  6. ecshop类的解析2 json_encode和json_decode的具体实现

    在看ecshop源码时,看到json这个类,研究了一下,它是为了兼容低版本php而做出的类,是对php的数据和json转换时使用的. encode和decode函数是对json的操作,对应json_e ...

  7. selenium3 + Python - 处理浏览器弹窗(转载)

    作者:Real_Tino 转载链接:https://blog.csdn.net/real_tino/article/details/59068827 我们在浏览网页时经常会碰到各种花样的弹窗,在做UI ...

  8. Django day16 Auth组件

    一:Auth组件 -django内置的用户认证系统,可以快速的实现,登录,注销,修改密码... -怎么用? (1)先创建超级用户: -python3 manage.py createsuperuser ...

  9. Java NIO Buffer说明

    Buffer 有3个重要的参数:位置(position).容量(capactiy).上限(limit) 位置(position): 写:当前缓冲区的位置,将从position的下一个位置写数据. 读: ...

  10. php中curl的详细解说 【转载】

    这几天在帮一些同学处理问题的时候,突然发现这些同学是使用file_get_contents()函数来采集页面内容的,貌似都没有curl的概念亦或是对这种工具特别不敏感, 本文我来给大家详细介绍下cUR ...