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 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
2 5 1
9 10 4
6 8 2
4 6 3
Sample Output
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 城市地平线的更多相关文章
- [BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 线段树
链接 题意:N个矩形块,交求面积并. 题解 显然对于每个 \(x\),只要求出这个 \(x\) 上面最高的矩形的高度,即最大值 将矩形宽度离散化一下,高度从小到大排序,线段树区间set,然后求和即可 ...
- 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树
[BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...
- 1645: [Usaco2007 Open]City Horizon 城市地平线
1645: [Usaco2007 Open]City Horizon 城市地平线 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 315 Solved: ...
- BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线
BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线 Description N个矩形块,交求面积并. Input * Line 1: A single i ...
- 【BZOJ】1645: [Usaco2007 Open]City Horizon 城市地平线(线段树+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1645 这题的方法很奇妙啊...一开始我打了一个“离散”后的线段树.............果然爆了. ...
- BZOJ 1645: [Usaco2007 Open]City Horizon 城市地平线 扫描线 + 线段树 + 离散化
Code: #include<cstdio> #include<algorithm> #include<string> #define maxn 1030000 # ...
- bzoj 1645: [Usaco2007 Open]City Horizon 城市地平线【线段树+hash】
bzoj题面什么鬼啊-- 题目大意:有一个初始值均为0的数列,n次操作,每次将数列(ai,bi-1)这个区间中的数与ci取max,问n次后元素和 离散化,然后建立线段树,每次修改在区间上打max标记即 ...
- 【BZOJ】1628 && 1683: [Usaco2007 Demo]City skyline 城市地平线(单调栈)
http://www.lydsy.com/JudgeOnline/problem.php?id=1628 http://www.lydsy.com/JudgeOnline/problem.php?id ...
- bzoj1683[Usaco2005 Nov]City skyline 城市地平线
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1683 Input 第1行:2个用空格隔开的整数N和W. 第2到N+1行:每行包括2个用空格 ...
随机推荐
- MLC固态硬盘,与入量是3000次P/E
固态硬盘是什么,固态硬盘寿命有多长 SSD泛指使用闪存芯片组成的SSD固态硬盘,是使用FLASH闪存颗粒作为存储单元,不再使用传统的机械存储方法,使用模拟的方式虚拟出传统 硬盘存取方式和扇区等,也可以 ...
- 【转】Device Tree(二):基本概念
原文网址:http://www.wowotech.net/linux_kenrel/dt_basic_concept.html 一.前言 一些背景知识(例如:为何要引入Device Tree,这个机制 ...
- EBS收单方/收货方
select rt.name, hcas.org_id from ar.hz_cust_acct_sites_all hcas, ar.hz_cust_site_uses_all hcsu, ra_t ...
- Asp.net MVC 3 防止 Cross-Site Request Forgery (CSRF)原理及扩展 安全 注入
原理:http://blog.csdn.net/cpytiger/article/details/8781457 原文地址:http://www.cnblogs.com/wintersun/archi ...
- [置顶] vi、akw和sed总结
- [转]Hulu 2013北京地区校招笔试题
填空题: 1.中序遍历二叉树,结果为ABCDEFGH,后序遍历结果为ABEDCHGF,逆序遍历结果为? 2.对字符串HELL0_HULU中的字符进行二进制编码,使得字符串的编码长度尽可能短,最短长度为 ...
- SAX解析和生成XML文档
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本人声明.否则将追究法律责任. 作者: 永恒の_☆ 地址: http://blog.csdn.net/chenghui031 ...
- iOS8 Core Image In Swift:更复杂的滤镜
iOS8 Core Image In Swift:自动改善图像以及内置滤镜的使用 iOS8 Core Image In Swift:更复杂的滤镜 iOS8 Core Image In Swift:人脸 ...
- hibernate 用hql做中文排序
用Hibernate+MySQL的童鞋是不是非常苦恼为什么MySQL不支持中文排序呢?没办法.仅仅有等utf8_unicode_cn 出来了.假设用hibernate即想实现跨库,又想不改代码怎样实现 ...
- 8.0 BOM对象
主要的掌握的知识结构图 1 Window 2 控制窗口.框架.弹出窗口 3 利用location对象中的页面信息 4 使用 navigator 对象了解浏览器 1.1 BOM的核心对象是window, ...