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个用空格 ...
随机推荐
- AJAX实例入门
一.开门见山 这些时间,瞎子也看得见,AJAX正大踏步的朝我们走来.不管我们是拥护也好,反对也罢,还是视而不见,AJAX像一阵潮流,席转了我们所有的人. 关于AJAX的定义也好,大话也好,早有人在网上 ...
- 【转】ubuntu12.04下安装chrome浏览器
原文网址:http://blog.163.com/zhou_411424/blog/static/197362156201331931313549 下载google chrome deb包 32位:h ...
- 制作安装包工具NSIS
NSIS 下载地址: http://nsis.sourceforge.net/Download 编辑工具:NIS Edit 下载地址: http://soft.hao123.com/soft/appi ...
- Populating Next Right Pointers in Each Node II 解答
Question Follow up for problem "Populating Next Right Pointers in Each Node". What if the ...
- 第六百二十一天 how can I 坚持
好歹都是一些集成的框架,也懒得去深入研究了,微信小程序,今天体验了一个,虽然内心非常相信张小龙,但是还是不太看好,但是感觉确实需要一种方式去简化应用程序的开发,今天把没用的app都删了,太耽误时间了, ...
- HDU 4362 Dragon Ball 线段树
#include <cstdio> #include <cstring> #include <cmath> #include <queue> #incl ...
- linux用户创建删除以及文件权限查看修改
一. 1.查看用户 命令如下:whoami 2.创建用户 创建用户命令:sudo adduser hello 超级用户是 root 删除用户名命令:sudo deluser hello --remov ...
- JS中for循环里面的闭包问题的原因及解决办法
我们先看一个正常的for循环,普通函数里面有一个for循环,for循环结束后最终返回结果数组 function box(){ var arr = []; for(var i=0;i<5;i++) ...
- node 安装express
环境:win7 64位旗舰版 1 安装nodejs 2 安装npm 3 安装express 重点介绍安装express,前提是你已经安装nodejs和npm了. 1 安装express npm ins ...
- Html5移动端页面布局通用模板暨移动端问题总结
最近的移动端项目终于告一段落了,其中遇到了不少问题,在此和大家总结分享. 首先,说一下结构.一般的手机页面大致可以分为五块:head, content, foot,solidbar,dialog. 针 ...