【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

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.

题解:一看就是离散化+线段树的题,可以先将h离散,再按a,b排序,用线段树维护扫描线来做,不过这样比较麻烦。

因为本题中所有的矩形底边都在同一直线上,所以我们可以把这些矩形整体看成一棵线段树,线段树的区间就是每个a,b,线段树的权值就是h,然后按h排序,从小到大一个一个加到线段树里就行了。

注意:a和b离散化后是2*40000的空间,所以线段树要开8*40000!2*4==8!!!

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lson x<<1
#define rson x<<1|1
using namespace std;
const int maxn=40010;
typedef long long ll;
struct edges
{ll pos,org;
}s[maxn<<1];
struct house
{ll sa,sb,sh;
}p[maxn];
int n,m;
ll ts[maxn<<3],tag[maxn<<3],ref[maxn<<1];
bool cmp1(edges a,edges b)
{return a.pos<b.pos;
}
bool cmp2(house a,house b)
{return a.sh<b.sh;
}
void pushdown(int l,int r,int x)
{if(tag[x]){int mid=l+r>>1;ts[lson]=(ref[mid]-ref[l])*tag[x],ts[rson]=(ref[r]-ref[mid])*tag[x];tag[lson]=tag[rson]=tag[x];tag[x]=0;}
}
void pushup(int x)
{ts[x]=ts[lson]+ts[rson];
}
void updata(int l,int r,int x,int a,int b,ll v)
{if(a<=l&&r<=b){ts[x]=(ref[r]-ref[l])*v;tag[x]=v;return ;}pushdown(l,r,x);int mid=l+r>>1;if(b<=mid)    updata(l,mid,lson,a,b,v);else if(a>=mid)    updata(mid,r,rson,a,b,v);else    updata(l,mid,lson,a,b,v),updata(mid,r,rson,a,b,v);pushup(x);
}
int main()
{scanf("%d",&n);int i;for(i=1;i<=n;i++){scanf("%lld%lld%lld",&p[i].sa,&p[i].sb,&p[i].sh);s[i*2-1].pos=p[i].sa,s[i*2].pos=p[i].sb;s[i*2-1].org=s[i*2].org=i;}sort(s+1,s+2*n+1,cmp1);for(i=1;i<=2*n;i++){if(s[i].pos>ref[m])    ref[++m]=s[i].pos;if(s[i].pos==p[s[i].org].sa)    p[s[i].org].sa=m;else    p[s[i].org].sb=m;}sort(p+1,p+n+1,cmp2);for(i=1;i<=n;i++)updata(1,m,1,p[i].sa,p[i].sb,p[i].sh);printf("%lld",ts[1]);return 0;
}

转载于:https://www.cnblogs.com/CQzhangyu/p/6281746.html

【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树相关推荐

  1. bzoj 1645: [Usaco2007 Open]City Horizon 城市地平线(线段树扫描线)

    1645: [Usaco2007 Open]City Horizon 城市地平线 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 732  Solved: ...

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

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

  3. [Usaco2007 Open]City Horizon 城市地平线

    1645: [Usaco2007 Open]City Horizon 城市地平线 Time Limit: 5 Sec   Memory Limit: 64 MB Submit: 411   Solve ...

  4. poj/OpenJ_Bailian - 2528 离散化+线段树

    传送门:http://bailian.openjudge.cn/practice/2528?lang=en_US //http://poj.org/problem?id=2528 题意: 给你n长海报 ...

  5. [牛客网#35D 树的距离]离散化+线段树合并

    [牛客网#35D 树的距离]离散化+线段树合并 分类:Data Structure SegMent Tree Merge 1. 题目链接 [牛客网#35D 树的距离] 2. 题意描述 wyf非常喜欢树 ...

  6. 850. 矩形面积 II:扫描线+离散化+线段树

    Difficulty: hard 标签: 扫描线, 离散化, 线段树 题目链接 力扣 题目解析 面试代码 /** x轴方向使用扫描线,y轴方向使用线段树维护扫描线的长度和每个区间覆盖的次数.由于y轴方 ...

  7. Mayor's posters POJ - 2528 (离散化+线段树)

    题意: 在1~10000000这个区间中读取n个海报的区间信息,后面的海报会覆 盖前面的海报,问最后能看到几张海报.(本题是一道bug题下面会提) 题目: The citizens of Byteto ...

  8. CF803G-Periodic RMQ Problem【离散化,线段树,ST表】

    正题 题目链接:https://www.luogu.com.cn/problem/CF803G 题目大意 一个长度为nnn的序列aaa复制kkk份连接,要求支持 区间赋值 区间查询最小值 n,q∈[1 ...

  9. Codeforces Round #345 (Div. 1) D. Zip-line 上升子序列 离线 离散化 线段树

    D. Zip-line 题目连接: http://www.codeforces.com/contest/650/problem/D Description Vasya has decided to b ...

最新文章

  1. 黄聪:如何使用CodeSmith批量生成代码(转:http://www.cnblogs.com/huangcong/archive/2010/06/14/1758201.html)...
  2. 虚拟机内 docker启动 局域网无发访问 问题解决
  3. css3中transition属性详解
  4. Windows Server 2016软件定义存储:Storage Spaces Direct介绍
  5. 【tool】网站测试分类
  6. linux网络保存退出,linux编辑文件后如何保存退出
  7. c++大作业迷宫游戏 规定时间内完成_孩子写作业慢的7种原因及其解决对策
  8. 动画 自制弹框上滑+渐显效果
  9. 使用React的static方法实现同构以及同构的常见问题
  10. mysql 备份100G花费时间_利用xtrabackup 全量备份100G的数据恢复到单实例测试
  11. lq分解的matlab语言,MATLAB-语言及其应用.ppt
  12. XPath 获取两个node中间的HTML Nodes
  13. 洛谷P3382 【模板】三分法
  14. jQuery 源码系列(十八)class 相关操作
  15. python数据分析的概念_Python数据分析入门篇
  16. putty怎么更改为中文_Putty怎么样设置显示中文 设置Putty显示中文
  17. C语言BT软件项目总结
  18. 如何计算IT投资回报(ROI)
  19. tcp支持浏览器websocket协议
  20. excel锁定后忘记密码的解决办法

热门文章

  1. DHCP:(2)思科交换机上部署DHCP服务以及DHCP中继功能
  2. 使用Pandas进行数据分析
  3. 基于51单片机 + ds12c887 + ds18b20 + lcd1602的时钟温度显示器(带闹钟功能)
  4. Spring统一日志处理(AOP)
  5. 中国巡游帆船行业市场供需与战略研究报告
  6. 【以太坊】雷电网络的101网络原理概述
  7. 数据分析(入门)纳米学位_tensorflow纳米级程序对机器学习入门的回顾
  8. 解决更换电池引发的乐视2手机(lex620)不进系统问题
  9. 数据中心拥塞控制集中式架构Fastpass之深度剖析
  10. Acrobat Reader XI启动后自动关闭的分析