题目描述

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的更多相关文章

  1. [POJ3277]City Horizon

    [POJ3277]City Horizon 试题描述 Farmer John has taken his cows on a trip to the city! As the sun sets, th ...

  2. 离散化+线段树 POJ 3277 City Horizon

    POJ 3277 City Horizon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18466 Accepted: 507 ...

  3. poj City Horizon (线段树+二分离散)

    http://poj.org/problem?id=3277 City Horizon Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  4. 1645: [Usaco2007 Open]City Horizon 城市地平线

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

  5. 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 ...

  6. BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线

    BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线 Description N个矩形块,交求面积并. Input * Line 1: A single i ...

  7. bzoj1645 / P2061 [USACO07OPEN]城市的地平线City Horizon(扫描线)

    P2061 [USACO07OPEN]城市的地平线City Horizon 扫描线 扫描线简化版 流程(本题为例): 把一个矩形用两条线段(底端点的坐标,向上长度,添加$or$删除)表示,按横坐标排序 ...

  8. 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树

    [BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...

  9. 【BZOJ】1645: [Usaco2007 Open]City Horizon 城市地平线(线段树+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1645 这题的方法很奇妙啊...一开始我打了一个“离散”后的线段树.............果然爆了. ...

  10. xtu数据结构 H. City Horizon

    H. City Horizon Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java cl ...

随机推荐

  1. GAN!生成对抗网络GAN全维度介绍与实战

    本文为生成对抗网络GAN的研究者和实践者提供全面.深入和实用的指导.通过本文的理论解释和实际操作指南,读者能够掌握GAN的核心概念,理解其工作原理,学会设计和训练自己的GAN模型,并能够对结果进行有效 ...

  2. Hugging News #0821: 新的里程碑:一百万个代码仓库!

    每一周,我们的同事都会向社区的成员们发布一些关于 Hugging Face 相关的更新,包括我们的产品和平台更新.社区活动.学习资源和内容更新.开源库和模型更新等,我们将其称之为「Hugging Ne ...

  3. 如何基于 Kubernetes 实现优质开发者平台体验?

    内部开发者平台(或 IDP)是使开发团队能够更快.更轻松.更一致地交付应用程序的基础设施.Kubernetes 本身是一个功能强大的平台,但它引入了太多复杂性和功能,因此不能简单地将其作为 IDP 交 ...

  4. What is TLS ?

    TLS intrduction TLS是一种保障数据传输安全的一种技术(方案),它在传输层工作. 想知道TLS和SSL的关系,先看看他们的历史: 人们遇到数据传输安全问题 1994 SSL 1.0 1 ...

  5. 原来你是这样的SpringBoot--初识SpringBootAdmin

    简介 Spring Boot Admin(SBA)是一个针对spring-boot的actuator接口进行UI美化封装的监控工具.它可以:在列表中浏览所有被监控spring-boot项目的基本信息, ...

  6. 小白弄明白了 unix 时间戳的转换问题

    小白对于将 unix 时间戳转换为日期时间和使用日期时间转换为 unix 时间戳,在项目中见到过很多,每次使用时不是用现有的方法转换就是网上搜索方法. 小白见过各种转换方式觉得moment库很是方便, ...

  7. Linux下MySQL备份指定数据库命令

    比如我们要备份mysql中已经存在的名为linux的数据库,要用到命令mysqldump 命令格式如下: [root@linuxsir01 root]# mysqldump -u root -p li ...

  8. 基于间隔密度的概念漂移检测算法mdm-DDM

    概念漂移 ​ 概念漂移是数据流挖掘领域中一个重要的研究点.传统的机器学习算法在操作时通常假设数据是静态的,其数据分布不会随着时间发生变化.然而对于真实的数据流来说,由于数据流天生的时间性,到达的数据的 ...

  9. 【RocketMQ】事务实现原理总结

    RocketMQ事务的使用场景 单体架构下的事务 在单体系统的开发过程中,假如某个场景下需要对数据库的多张表进行操作,为了保证数据的一致性,一般会使用事务,将所有的操作全部提交或者在出错的时候全部回滚 ...

  10. 手动实现Transformer

      Transformer和BERT可谓是LLM的基础模型,彻底搞懂极其必要.Transformer最初设想是作为文本翻译模型使用的,而BERT模型构建使用了Transformer的部分组件,如果理解 ...