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. hive计算日期差

    首先,hive本身有一个UDF,名字是datediff.我们来看一下这个日期差计算的官方描述,(下面这个是怎么出来的): hive> desc function extended datedif ...

  2. hdu_1859_最小长方形_201402282048

    最小长方形 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  3. 记一个简单的webpack.config.js

    module.exports = { entry: './basic/app.js', output: { path: './assets/', filename: '[name].bundle.js ...

  4. N天学习一个Linux命令之hostnamectl

    前言 安装了CentOS7,发现按照以前修改文件/etc/sysconfig/network HOSTNAME字段主机名的方式不生效了,查资料发现可以使用hostnamectl命令 用途 Contro ...

  5. Java Json格式的字符串转变对象

    Java Json格式的字符串转变对象: 方法还是比较多的: 学习:https://my.oschina.net/heweipo/blog/386808 其中的jsonlib说明:http://www ...

  6. nyoj 1238 最少换乘 (河南省第八届acm程序设计大赛)

    题目1238 题目信息 执行结果 本题排行 pid=1238" style="text-decoration:none; color:rgb(55,119,188)"&g ...

  7. jquery-layer弹出框

    样式1: 代码: 前台jsp: $("#add_table").bind("click",function(){ layer.open({ type: 2, t ...

  8. oc56--ARC多个对象的内存管理

    // main.m // ARC中多个对象的内存管理:ARC的内存管理就是MRC的内存管理(一个对象释放的时候,必然会把它里面的对象释放),只不过一个是Xcode加的代码,一个是我们自己加的代码: / ...

  9. Linux Framebuffer驱动框架之二软件架构(未完待续)【转】

    本文转载自:http://blog.csdn.net/gqb_driver/article/details/12918547 /************************************ ...

  10. DockPanelSuite中的DocumentStyle

    首先明确一个概念Mdi的完整词组:Multiple document interface namespace WeifenLuo.WinFormsUI.Docking { public enum Do ...