[USACO2007OPENS] City Horizon S
题目描述
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 Ai through Bi along the horizon (1 ≤ Ai < Bi ≤ 1,000,000,000) and has height Hi (1 ≤ Hi ≤ 1,000,000,000). Determine the area, in square units, of the aggregate silhouette formed by all N buildings.
输入格式
第一行一个整数N,然后有N行,每行三个正整数ai、bi、Hi。
输出格式
一个数,数列中所有元素的和。
样例 #1
样例输入 #1
4
2 5 1
9 10 4
6 8 2
4 6 3
样例输出 #1
16
提示
\(N<=40000 , a、b、k<=10^9\) 。
所有的左端点和右端点把线段切成了很多段,每一段的值都是一样的。
把所有左右端点离散一下,然后从左到右枚举每一段。那么就要研究这个时候每一段的值是什么。
可以开一个set维护现在有的高度,这一段的高度就是现有的高度的最大值。我们遇到一个左端点就把对应高度放入set,遇到一个右端点就把对应高度从set中删去。然后一段的面积和就是长度*最大值。加起来即可
#include<iostream>
#include<set>
#include<algorithm>
using namespace std;
int a,b,h;
int n;
long long ans;
multiset<int>s;
struct evt{
int x,h;
bool operator<(const evt e)const{
return x<e.x;
}
}e[80005];
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a>>b>>h;
e[2*i-1]=(evt){a,h};
e[2*i]=(evt){b,-h};
}
sort(e+1,e+2*n+1);
s.insert(0);
for(int i=1;i<=2*n;i++)
{
if (i!=1&&!s.empty()) ans += (long long)(e[i].x-e[i - 1].x)*(*s.rbegin());
if(e[i].h>0)
s.insert(e[i].h);
else
s.erase(s.find(-e[i].h));
}
cout<<ans<<endl;
}
[USACO2007OPENS] City Horizon S的更多相关文章
- [POJ3277]City Horizon
[POJ3277]City Horizon 试题描述 Farmer John has taken his cows on a trip to the city! As the sun sets, th ...
- 离散化+线段树 POJ 3277 City Horizon
POJ 3277 City Horizon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18466 Accepted: 507 ...
- poj City Horizon (线段树+二分离散)
http://poj.org/problem?id=3277 City Horizon Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- 1645: [Usaco2007 Open]City Horizon 城市地平线
1645: [Usaco2007 Open]City Horizon 城市地平线 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 315 Solved: ...
- 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 ...
- BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线
BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线 Description N个矩形块,交求面积并. Input * Line 1: A single i ...
- bzoj1645 / P2061 [USACO07OPEN]城市的地平线City Horizon(扫描线)
P2061 [USACO07OPEN]城市的地平线City Horizon 扫描线 扫描线简化版 流程(本题为例): 把一个矩形用两条线段(底端点的坐标,向上长度,添加$or$删除)表示,按横坐标排序 ...
- 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树
[BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...
- 【BZOJ】1645: [Usaco2007 Open]City Horizon 城市地平线(线段树+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1645 这题的方法很奇妙啊...一开始我打了一个“离散”后的线段树.............果然爆了. ...
- xtu数据结构 H. City Horizon
H. City Horizon Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java cl ...
随机推荐
- 交换机通过SFTP进行文件操作
组网图形 通过SFTP进行文件操作简介 配置设备作为SFTP服务器,用户可以在终端通过SFTP通信方式,利用SSH协议提供的安全通道与远端设备进行安全连接.通过SFTP进行文件操作的方式对数据进行了 ...
- Mysql基础9-事务
一.事务简介 事务是一组操作的集合,它是一个不可分割的工作单位,事务会把所有操作作为一个整体一起向系统提交或者撤销操作请求,即这些操作要么同时成功,要么同时失败.mysql的事务默认是自动提交的,也就 ...
- OpenStack-T版+Ceph
OpenStack OpenStack 中有哪些组件 keystone:授权 [授权后各个组件才可以进行相应的功能] Keystone 认证所有 OpenStack 服务并对其进行授权.同时,它也是所 ...
- Oracle主键自增列
SQL Server创建主键自增列我们可以使用"ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY"一步到位创建,而Oracle创建主键自增列和SQ ...
- # 简明快速配置 Rust 工具链
以下内容为本人的学习笔记,如需要转载,请声明原文链接微信公众号「ENG八戒」https://mp.weixin.qq.com/s/dBzL9WZ8P1L1X9j_XkmNQg 你可能会为不同版本的工具 ...
- Solution -「CF 959E」Mahmoud and Ehab and the xor-MST
Description Link. 一完全图有 \(n\) 个节点 \(0,...,n-1\),其中边 \((i,j)\) 的权值为 \(i\oplus j\),其中 \(\oplus\) 为位异或操 ...
- MySQL数据库操作 Lab1
实验一 MySQL数据库操作 实验目的: 掌握MySQL安装.配置与登录方法,使用MySQL客户创建数据库及对数据库表完成各种操作 实验内容: 1. 安装MySQL数据库管理系统, ...
- CF1575I Illusions of the Desert
prologue 还是太菜了,这个 154 行的树剖 20min 才敲完. analysis 首先,处理这个给到我们的这个式子. \[\max(| a _ u + a _ v |, | a _ u - ...
- CMake中添加 -lpthread 编译参数
问题:当在linux命令行中编译关于进程/线程的源文件时,需要加上 -lpthread 参数动态链接线程库而在CMake中如何加入呢? 方法:只需在 add_executable() 命令前面加上以下 ...
- 关于AI时代的程序架构的变化
以ChatGPT为代表的AI出现,表示着AI的零点时刻已经突破.现在AI的使用已经不用再多说了,实际上是已经侵入到各行各业.所有人都在疯狂寻找本行业AI的使用场景,这样的盛景只在互联网刚出现的时候能感 ...