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. asp.net--mvc--异步编程

    Using Asynchronous Methods in ASP.NET MVC 4 asp.net mvc中的异步只能增加系统的性能,原来需要500个线程的,现在需要50个就够了,对一些常规的程序 ...

  2. Linux排查java程序占用cpu过高的线程代码

    分几步骤: 1.通过top,查出占用CPU过高的java进程 ,比如: pid :6666 2.通过ps -mp 6666 -o THREAD,tid,time| sort -n -k1 -r 查看此 ...

  3. open cursor too much error

    今天遇到一个错误ORA-01000: maximum open cursors exceeded. 客户想增加 DB 的open_cursor这个参数. 但是我看了下,她的程序要打开几千个cursor ...

  4. Spark 颠覆 MapReduce 保持的排序记录

    在过去几年,Apache Spark的採用以惊人的速度添加着,通常被作为MapReduce后继,能够支撑数千节点规模的集群部署. 在内存中数 据处理上,Apache Spark比MapReduce更加 ...

  5. [Tool] Open Multiple Terminal Tabs on npm Start with ttab and npm-run-all

    Often times when developing web applications, you need to open multiple tabs to run different script ...

  6. GPS-Graph Processing System 改动源代码经验总结 (四)

    HamaWhite原创,转载请注明出处.欢迎大家增加Giraph 技术交流群: 228591158 本文目的:在改动GPS源代码后,具体描写叙述怎样编译和分发到各Worker节点上. 以下以Graph ...

  7. 迭代器和iter()函数

    1.先来个样例,有个初步的印象: myTuple=(123,'xyz',45.67) i=iter(myTuple) i.next() 123 i.next() 'xyz' i.next() 45.6 ...

  8. SQL Server 2012内部原理及故障排除(专栏)

    PROFESSIONAL SQL SERVER® 2012 INTERNALS AND TROUBLESHOOTING一书(可从这里下载).认为内容非常不错.自己也想对SQL Server 2012有 ...

  9. hdu1533 费用流模板

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  10. C++_homework_StackSort

    顾名思义(?)类似于单调栈?维护一个单调递减的栈.一旦准备入栈的元素大于栈顶元素,栈一直弹出直到准备入栈的元素小于等于栈顶元素,弹出的元素压入另一个tmp栈中. #include <iostre ...