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

Sample Input

4

2 5 1

9 10 4

6 8 2

4 6 3

Sample Output

16



OUTPUT DETAILS:



The first building overlaps with the fourth building for an area of 1

square unit, so the total area is just 3*1 + 1*4 + 2*2 + 2*3 - 1 = 16.

离散+线段树各种搞都能过……但是我写了个最得瑟的

先搞一个快排+判重,然后再把区间修改按高度排一下……我有优越感

#include<cstdio>
#include<algorithm>
#define LL long long
#define N 50010
#define mod 1000007
using namespace std;
struct trees{
int l,r,mx;
}tree[8*N];
struct add{
int l,r,mx;
}a[N];
int n,treesize;
LL ans;
int num[2*N];
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline void pushdown(int now)
{
if (tree[now].l==tree[now].r)return;
int mx=tree[now].mx;tree[now].mx=0;
if (mx)
{
tree[now<<1].mx=mx;
tree[now<<1|1].mx=mx;
}
}
inline void buildtree(int now,int l,int r)
{
tree[now].l=l;
tree[now].r=r;
if (l==r)return;
int mid=(l+r)>>1;
buildtree(now<<1,l,mid);
buildtree(now<<1|1,mid+1,r);
}
inline void change(int now,int x,int y,int mx)
{
pushdown(now);
int l=tree[now].l,r=tree[now].r;
if (l==x&&r==y)
{
tree[now].mx=mx;
return;
}
int mid=(l+r)>>1;
if (y<=mid) change(now<<1,x,y,mx);
else if(x>mid)change(now<<1|1,x,y,mx);
else
{
change(now<<1,x,mid,mx);
change(now<<1|1,mid+1,y,mx);
}
}
inline void dfs(int now)
{
int l=tree[now].l,r=tree[now].r;
if (tree[now].mx)
{
ans+=(LL)tree[now].mx*(num[r+1]-num[l]);
return;
}
if (l==r)return;
dfs(now<<1);
dfs(now<<1|1);
}
//----------------------------------离散
struct hashing{
int num,next,rnk;
}hash[mod];
int ha[2*N],len,cnt,rating;
int head[mod];
inline void insert(int u,int v,int w)
{
hash[++cnt].num=v;
hash[cnt].rnk=w;
hash[cnt].next=head[u];
head[u]=cnt;
}
inline int find(int x)
{
int s=x%mod;
for (int i=head[s];i;i=hash[i].next)
if (hash[i].num==x)return hash[i].rnk;
}
inline bool cmp(const add &a,const add &b)
{return a.mx<b.mx||a.mx==b.mx&&a.l<b.l||a.mx==b.mx&&a.l==b.l&&a.r<b.r;}
//----------------------------------end
int main()
{
n=read();
for (int i=1;i<=n;i++)
{
a[i].l=read();
a[i].r=read();
a[i].mx=read();
ha[++len]=a[i].l;
ha[++len]=a[i].r;
}
sort(ha+1,ha+len+1);
for (int i=1;i<=len;i++)
if (ha[i]!=ha[i-1])
{
num[++rating]=ha[i];
insert(ha[i]%mod,ha[i],rating);
}
for (int i=1;i<=n;i++)
{
a[i].l=find(a[i].l);
a[i].r=find(a[i].r);
}
sort(a+1,a+n+1,cmp);
buildtree(1,1,rating-1);
for (int i=1;i<=n;i++)
change(1,a[i].l,a[i].r-1,a[i].mx);
dfs(1);
printf("%lld",ans);
}

bzoj1645 [Usaco2007 Open]City Horizon 城市地平线的更多相关文章

  1. [BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 线段树

    链接 题意:N个矩形块,交求面积并. 题解 显然对于每个 \(x\),只要求出这个 \(x\) 上面最高的矩形的高度,即最大值 将矩形宽度离散化一下,高度从小到大排序,线段树区间set,然后求和即可 ...

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

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

  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. 【BZOJ】1645: [Usaco2007 Open]City Horizon 城市地平线(线段树+特殊的技巧)

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

  6. BZOJ 1645: [Usaco2007 Open]City Horizon 城市地平线 扫描线 + 线段树 + 离散化

    Code: #include<cstdio> #include<algorithm> #include<string> #define maxn 1030000 # ...

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

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

  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. bzoj1683[Usaco2005 Nov]City skyline 城市地平线

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1683 Input 第1行:2个用空格隔开的整数N和W. 第2到N+1行:每行包括2个用空格 ...

随机推荐

  1. 利用智能手机(Android)追踪一块磁铁(三)

    更新磁铁追踪算法的源代码,Android Studio项目工程 github地址:https://github.com/amazingyyc/MagnetLocate 说明:将磁铁的位置信息封装成消息 ...

  2. 《Algorithms 4th Edition》读书笔记——3.1 符号表(Elementary Symbol Tables)-Ⅲ

    3.1.3 用例举例 在学习它的实现之前我们还是应该先看看如何使用它.相应的我们这里考察两个用例:一个用来跟踪算法在小规模输入下的行为测试用例和一个来寻找更高效的实现的性能测试用例. 3.1.3.1 ...

  3. c++中经常需要访问对象中的成员的三种方式

    可以有3种方法: 通过对象名和成员运算符访问对象中的成员; 通过指向对象的指针访问对象中的成员; 通过对象的引用变量访问对象中的成员. 一.通过对象名和成员运算符访问对象中的成员 例如在程序中可以写出 ...

  4. oracle查询优化

    1. 排序合理查询条件 Oracle自下而上分析顺序WHERE收条,从优化性能的角度.它建议,这些条件将能够过滤掉大量行书写的WHERE在条款结束, 之间的连接条件置于其它WHERE子句之前,即对易排 ...

  5. Linux经常使用命令大全

    系统信息  arch 显示机器的处理器架构(1)  uname -m 显示机器的处理器架构(2)  uname -r 显示正在使用的内核版本号  dmidecode -q 显示硬件系统部件 - (SM ...

  6. ORACLE 视图的 with check option

    ORACLE 视图的 with check option 我们来看下面的例子: create or replace view testview as select empno,ename from e ...

  7. ORACLE数据库常用查询二

    ORACLE数据库常用查询 1.查看表空间对应数据文件情况: SQL MB,AUTOEXTENSIBLE FROM DBA_DATA_FILES; TABLESPACE_NAME FILE_NAME ...

  8. C#/.NET笔试题

    1.简述 private. protected. public. internal.protected internal 访问修饰符和访问权限 private : 私有成员, 在类的内部才可以访问. ...

  9. nodejs报错 events.js:72 throw er; // Unhandled 'error' event

    var http = require('http'); var handlerRequest = function(req,res){ res.end('hello');}; var webServe ...

  10. CSS筛选器简单实例1

    1.通配符 <!--筛选器---通配符实例--> <!--支持IE7+ --> <style type="text/css"> *.all { ...