题目链接

题目

题目描述

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. AvaloniaUI 取消标题栏,无边框无最大最小化,

    AvaloniaUI 取消标题栏,无边框无最大最小化, 创建一个Window控件 并且在Window中添加以下代码 ExtendClientAreaToDecorationsHint="Tr ...

  2. SD协议-时序

    1.SD Timing 时序主要体现在CMD和Response的时序 S - 起始位,一直为0 T - Transmitter,1表示CMD(发起端是Host),0表示Response(发起端是Car ...

  3. 让vs支持wsl调试

    WSL安装 wsl --install -d Ubuntu 等一会提示输入用户名,不用管它,直接关闭,下次打开wsl,会以无密码的root用户打开 wsl卸载 wsl --unregister Ubu ...

  4. 阿里云IPV6 创建虚拟机的过程

    阿里云IPV6 创建虚拟机的过程 背景 IPV6 已经越来越广泛的应用. 想在外网开通一下IPV6,发现还有一些坑. 这里总结一下. 备忘. 开通方式 1. 登录阿里云的控制台, 打开云服务器ECS的 ...

  5. [转帖]Debian开启SSH

    一.Debian开启SSH 参考链接: https://blog.csdn.net/zzpzheng/article/details/71170572 https://help.aliyun.com/ ...

  6. [转帖]为什么不推荐使用/etc/fstab

    https://www.jianshu.com/p/af49a5d0553f 对于工作中使用服务器的公司来讲,每到节假日来临时,总免不了对服务器进行下电.而收假回来的早上,则会有一个早上的时间会花费在 ...

  7. [转帖]什么是拒绝服务(DoS)攻击?

    https://www.cloudflare.com/zh-cn/learning/ddos/glossary/denial-of-service/ 什么是拒绝服务攻击? 拒绝服务(DoS)攻击是一种 ...

  8. Typescript基本数据类型的讲解

    1. typescript 的 7 种数据类型 typescript 的原始数据类型 string number boolean null underfined enum(枚举) symbol 这 7 ...

  9. ABP-VNext 用户权限管理系统实战03---动态api调用并传递token

    一.使用动态api的目的 ABP可以自动创建C# API 客户端代理来调用远程HTTP服务(REST APIS).通过这种方式,你不需要通过 HttpClient 或者其他低级的HTTP功能调用远程服 ...

  10. wap2app下拉刷新

    支持全局刷新,支持vue项目 目前支持wap2app,uin-app全局下拉刷新 戳我阅读原文 --转载自微信公众号:酿俗