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. C++字符串指针与字符数组的区别

    今天发现这样一个问题 #include <iostream> using namespace std; int main() { ]; strcpy_s(ch1,");//编译通 ...

  2. 【HDU1879】继续畅通工程(MST基础题)

    真心大水题...不多说. #include <iostream> #include <cstring> #include <cstdlib> #include &l ...

  3. nyoj 234 吃土豆

    描述 Bean-eating * grid. Now you want to eat the beans and collect the qualities, but everyone must ob ...

  4. JQuery DOM 有关代码练习

    //累加你选择的个数 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <hea ...

  5. Unity UI和引用的管理中心

    我们来谈谈Unity的UI, 通常会写一些UI页面,当A页面需要去操作B页面的时候. 至少要获取B页面的引用吧! 一般新人都会在组件的写一个public GameObject UIB页面的属性, 然后 ...

  6. Javascript 中的变量

    var a; console.log("The value of a is " + a); // The value of a is undefined console.log(& ...

  7. [每日一题] OCP1z0-047 :2013-07-27 外部表――不能被DML和建索引

    首先看官方文档上的解释: Managing External Tables Oracle Database allows you read-only access to data in externa ...

  8. SVG Loading

    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="64&qu ...

  9. js如何关闭当前页,而不弹出提示框

    //关闭当前页面,并且打开新页面,(不提示) function closeWinAndOpen(url) { //利用随机数处理WinName var sWinName = "LR" ...

  10. 2014年1月9日 Oracle常见授权与权限回收[转]

    1.GRANT 赋于权限 常用的系统权限集合有以下三个: CONNECT(基本的连接), RESOURCE(程序开发), DBA(数据库管理) 常用的数据对象权限有以下五个: ALL ON 数据对象名 ...