题目链接

题目

题目描述

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

  1. [LeetCode] All O`one Data Structure 全O(1)的数据结构

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  2. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  3. [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

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

  5. Mesh Data Structure in OpenCascade

    Mesh Data Structure in OpenCascade eryar@163.com 摘要Abstract:本文对网格数据结构作简要介绍,并结合使用OpenCascade中的数据结构,将网 ...

  6. ✡ 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 - ...

  7. leetcode Add and Search Word - Data structure design

    我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...

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

  9. HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...

  10. LeetCode Two Sum III - Data structure design

    原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...

随机推荐

  1. ASIC 功能验证SVTB

    System Verilog进行验证是可以不综合的 发现DUT中的功能问题 预备知识:Linux/verilog/gvim System Verilog学习目录 System Verilog Test ...

  2. 神经网络优化篇:详解局部最优的问题(The problem of local optima)

    局部最优的问题 在深度学习研究早期,人们总是担心优化算法会困在极差的局部最优,不过随着深度学习理论不断发展,对局部最优的理解也发生了改变.向展示一下现在怎么看待局部最优以及深度学习中的优化问题. 这是 ...

  3. [转帖]br 备份时排除某个库

    https://tidb.net/book/tidb-monthly/2023/2023-02/usercase/excluded-a-storeroom-during-br-backup 生产环境中 ...

  4. [转帖]PostgreSQL任意命令执行漏洞利用(CVE-2019-9193)

    https://zhuanlan.zhihu.com/p/143443516   最近没事曰曰内网,偶然发现了一个使用空密码的pg(是的,连爆破都省了).用navicat连上去看了下几个库都是一些业务 ...

  5. [转帖]淫技巧 | 如何查看已连接的wifi密码

    https://blog.csdn.net/DynmicResource/article/details/120134984?spm=1001.2014.3001.5502 主题使用方法:https: ...

  6. [转帖]nginx的luajit安装luarocks并安装luafilesystem

    nginx的luajit安装luarocks并安装luafilesystem by admin on 2015-07-11 08:05:23 in , 69次 标题有点绕口.我尽量把关键词都贴进去.之 ...

  7. [转帖]服务器稳定性测试-LTP压力测试方法及工具下载

    简介 LTP(LinuxTest Project)是SGI.IBM.OSDL和Bull合作的项目,目的是为开源社区提供一个测试套件,用来验证Linux系统可靠性.健壮性和稳定性.LTP测试套件是测试L ...

  8. [转帖]焱融全闪系列科普| 为什么 SSD 需要 NVMe?

    https://xie.infoq.cn/article/7026237b455c7d62f33afc4a9 NVMe 的由来 目前机械硬盘大多数使用 SATA (Serial ATA Advance ...

  9. Operating.System.Concepts.10th.Edition中文翻译

    <操作系统概念>是一本很好的书,主要介绍了操作系统的各个层面的概念,包含CPU调度,内存处理,文件系统等,目前已经出到第10版,是一本非常经典的书籍,从第1版至今被国内外众多高校选作教材, ...

  10. TortoiseGit 常见问题汇总

    1.test分支修改后合并到master分支 1)切换本地分支到master分支 2)TortoiseGit --->  merge,选择远程分支test 提交到远程分支master 2.将远程 ...