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 ...
随机推荐
- P5704 【深基2.例6】字母转换
1.题目介绍 2.题解 2.1 ASCII码表 在ASCII码表中,小写字母=大写字母+32,则大写字母=小写字母-32 #include<iostream> using namespac ...
- SQL联结
1联结 那我们又该如何创建联结呢? So easy! 规定要联结的所有表以及它们如何关联就可以了. 在设置关联条件时,为避免不同表被引用的列名相同,我们需要使用完全限定列名(用一个点分隔表名和列名), ...
- [转帖]性能分析之TCP全连接队列占满问题分析及优化过程(转载)
https://www.cnblogs.com/wx170119/p/12068005.html 前言 在对一个挡板系统进行测试时,遇到一个由于TCP全连接队列被占满而影响系统性能的问题,这里记录下如 ...
- [转帖]Python连接Oracle数据库进行数据处理操作
https://www.dgrt.cn/a/2259443.html?action=onClick 解决以下问题: Python连接Oracle数据库,并查询.提取Oracle数据库中数据? 通过Py ...
- Linux bridge使用dummy接口调用IPVS的问题
Linux bridge使用dummy接口调用IPVS的问题 在IPVS: How Kubernetes Services Direct Traffic to Pods一文中,作者给出了一个简单的组网 ...
- NutUI-React 京东移动端组件库 2月份上新!欢迎使用!
作者:京东零售 佟恩 NutUI 是一款京东风格的移动端组件库.NutUI 目前支持 Vue 和 React技术栈,支持Taro多端适配. 本次,是2月的一个示例输出,希望对你有帮助! 2月,我们对组 ...
- js获取字符串最后几位字符数
截取字符串 为什要截取字符串呢??? 因为有些时候,我们需要判断某一个字符串中是不是,含有特定的字符 substring(a)从起始位置开始(包含a这个位置),一直到字符串的末尾(截取字符串最后6个) ...
- 【scikit-learn基础】--『回归模型评估』之可视化评估
在scikit-learn中,回归模型的可视化评估是一个重要环节.它帮助我们理解模型的性能,分析模型的预测能力,以及检查模型是否存在潜在的问题.通过可视化评估,我们可以更直观地了解回归模型的效果,而不 ...
- 2021美亚杯团队赛write up
个人赛与团队赛下载文件解压密码:MeiyaCup2021 加密容器解密密码: uR%{)Y'Qz-n3oGU`ZJo@(1ntxp8U1+bW;JlZH^I4%0rxf;[N+eQ)Lolrw& ...
- 从零开始配置vim(28)——代码的编译、运行与调试
在前面几个章节,我们逐渐为 Vim 配置了语法高亮.代码的跳转和自动补全功能.现在的 Vim 已经可以作为代码编辑器来使用了.但是想将它作为日常发开的主力编辑器来用还需要很长一段路要走,其中一个就是要 ...