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. HDU 5433

    每次BC都好心酸... BFS+queue..状态可以设为p_val[x][y][k],加上斗志的值. #include <iostream> #include <cstdio> ...

  2. 权限问题导致无法删除ftp文件

    首先吐槽一下,使用新版编辑器,发了两遍愣是time out,果断放弃 这个文章也是一件小事,大致说一下: 有一个java操作ftp文件的程序,运行删除时,总是返回false,也没有报错.開始考虑是没有 ...

  3. Python帮助函数调试函数 用于获取对象的属性及属性值

    Python帮助函数调试函数 用于获取对象的属性及属性值 刚接触Python,上篇 <Python入门>第一个Python Web程序--简单的Web服务器 中调试非常不方便,不知道对象详 ...

  4. jenkins集成钉钉

    1.创建通知人群组,添加机器人 2.2.获取自定义机器人webhook 3.jenkins 安装"Dingding[钉钉] Plugin"插件: 4.插件安装完成后,创建/修改任务 ...

  5. android 自己定义ViewGroup实现可记载并呈现选择的ListView

    转载请注明出处:王亟亟的大牛之路 之前也做过一些用TextView之类的记录ListView选项的东西.可是总认为好难看.发现个不错的实现就贴给大家. 项目文件夹 执行效果: 自己定义视图: @Tar ...

  6. CSS3动画遮罩文字特效

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  7. 解析Qt元对象系统(四) 属性系统(确实比较方便)

    官方解释 我们在Qt源码中可以看到一个QObject的子类经常会用到一些Q_开头的宏,例如QMainWindow类开始部分代码是这样的: Q_PROPERTY(QSize iconSize READ ...

  8. How to build CppCMS 1.x.x

    How to build CppCMS 1.x.x Requirements Mandatory Requirements Recommended Dependencies Suggested Dep ...

  9. 解析HTML文件

    #!/usr/bin/env python3 # -*- coding: UTF-8 -*- from bs4 import BeautifulSoup import operator import ...

  10. 数据结构C++,线性表的实现

    #include <iostream>#include <sstream>#include <fstream>#include <cmath>#incl ...