NC22596 Rinne Loves Data Structure
题目
题目描述
Rinne 喜欢 OI。在 9102 年的 PION 中,她在初赛遇到了这样一道题目:
阅读下列代码,然后回答问题。
补充:建树过程中会更新lc和rc,这实质上是一个二叉查找树的插入过程。
定义一个玄学节点叫做 R,每次操作读入 val ,执行 Insert(R,val)。
问题:每次 Insert 操作结束之后,输出当前节点的深度和。
这里我们定义 R 节点的深度为 0。
输入描述
第一行一个整数 N,表示操作次数。
接下来 N 行,第 i 行有一个值 \(val_i\),表示第 i 次操作的 \(val_i\)。
输出描述
N 行,每行输出该次操作完后的答案。
示例1
输入
8
3
5
1
6
8
7
2
4
输出
0
1
2
4
7
11
13
15
备注
\(N \leq 3 \times 10^5\)。
题解
知识点:树,STL。
画图易知二叉排序树插入的节点有 \(4\) 种情况:插入节点比所有节点都小,那会存放在最小节点的左孩子;插入节点比所有节点都大,那会存放在最大节点的右孩子;插入节点比某子树根小,但比其左子树根大,那会存放在左子树的右孩子;插入节点比某子树根大,但比其右子树根小,那会存放在右子树的左孩子。
因为二叉排序树不能出现相同键值的节点,用 \(set\) 存储比较好,特判一下插入相同元素的情况。再用一个 \(map\) 存储相应节点的深度。
随后用 \(set\) 成员函数 \(lower\_bound\) (比STL泛用函数要快)查找第一个大于等于插入元素的节点位置,然后如果是 \(begin()\) 或者 \(end()\) ,则取相应的头/尾节点的深度加一;如果是中间某个元素,则取前后两个元素的深度最大值加一。最后别忘记插入回去。
时间复杂度 \(O(n \log n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
unordered_map<int, int> dep;
set<int> s;
ll ans = 0;
while (n--) {
int val;
cin >> val;
if (s.empty()) {
s.insert(val);
dep[val] = 0;
cout << 0 << '\n';
continue;
}
auto pos = s.lower_bound(val);
if (*pos == val) {
cout << ans << '\n';
continue;
}
if (pos == s.begin()) dep[val] = dep[*pos] + 1;
else if (pos == s.end()) pos--, dep[val] = dep[*pos] + 1;
else {
dep[val] = dep[*pos];
pos--;
dep[val] = max(dep[*pos], dep[val]) + 1;
}
ans += dep[val];
s.insert(val);
cout << ans << '\n';
}
return 0;
}
NC22596 Rinne Loves Data Structure的更多相关文章
- [LeetCode] All O`one Data Structure 全O(1)的数据结构
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations:add and find. add - ...
- Finger Trees: A Simple General-purpose Data Structure
http://staff.city.ac.uk/~ross/papers/FingerTree.html Summary We present 2-3 finger trees, a function ...
- Mesh Data Structure in OpenCascade
Mesh Data Structure in OpenCascade eryar@163.com 摘要Abstract:本文对网格数据结构作简要介绍,并结合使用OpenCascade中的数据结构,将网 ...
- ✡ leetcode 170. Two Sum III - Data structure design 设计two sum模式 --------- java
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- leetcode Add and Search Word - Data structure design
我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...
- LeetCode Two Sum III - Data structure design
原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...
随机推荐
- Redis 常用五种数据类型编码
转载请注明出处: 目录 Redis 的五种数据结构 Redis 数据结构的内部编码 1.String 1.1 常用命令 1.2 内部编码 1.3 典型使用场景 2. Hash 2.1 常用命令及时间复 ...
- python3使用diagrams生成架构图
技术背景 对于一个架构师或者任何一个软件工程师而言,绘制架构图都是一个比较值得学习的技能.这就像我们学习的时候整理的一些Xmind那种思维逻辑图一样,不仅可以帮我们看到组件之间的联系和层级,还能够展示 ...
- 搞了个Blazor工具站,域名一次性买了10年!
大家好,我是沙漠尽头的狼. 在 Dotnet9 上线在线小工具和小游戏后,服务器的压力感觉挺大的,打开25个页面,内存占用170MB左右,CPU保持在60~70%,看来Server真不适合搞这类交互较 ...
- 浏览器兼容 : IE10
<script> /*@cc_on @*//*@ if (document.documentMode == 10) { // 只在 IE10 文档模式下运行,例如 IE10 浏览器或 IE ...
- CPU信息查看的工具
CPU信息查看的工具 背景 信创国产化如火如荼. CPU的型号其实越来越多 lscpu出来的结果其实太抽象, 对CPU的缓存架构显示不充分 今天在看大佬的文章是看到了一个工具: hwloc 感觉非常优 ...
- [转帖]Linux之bash反弹shell原理浅析
环境 攻击机:kali ip:192.168.25.144 靶 机:centos ip:192.168.25.142 过程 kali 监听本地8888端口 靶机 ...
- [转帖]Jmeter常用配置元件(二):“HTTP Cookie管理器”登录状态保持
在API接口测试过程中,我们需要传递cookie值作为连接状态的保持,例如登录后状态信息.通过使用Jmeter提供的"HTTP Cookie管理器"来实现. 一般情况下不需要输入什 ...
- 【转帖】50.设置HotSpot采用解释器还是JIT编译器(-Xint、-Xcomp、Xmixed以及-Server、-Client)
目录 1.设置HotSpot 1.设置HotSpot 1.设置采用解释器还是JIT编译器 -Xint: 完全采用解释器模式执行程序. -Xcomp: 完全采用即时编译器模式执行程序.如果即时编译出现问 ...
- 【转帖】Ethernet 与 Infiniband的网络特性对比
一.两者定位 以太网(Ethernet): 应用最广泛,是最成熟的网络互联技术,也是整个互联网络大厦的基石,兼容性非常好,可实现不同的系统之间的互连互通 IB(Infiniband): 领域很专,作为 ...
- ipset的学习与使用
ipset的学习与使用 场景说明 虽然可以通过: firewall-cmd --zone=trusted --add-source=$1 --permanent && firewall ...