当前位置:网站首页>二叉树专题--【深基16.例7】普通二叉树(简化版)(multiset 求前驱 后继 哨兵法)
二叉树专题--【深基16.例7】普通二叉树(简化版)(multiset 求前驱 后继 哨兵法)
2022-07-02 07:21:00 【Morgannr】
【深基16.例7】普通二叉树(简化版)
题目描述
您需要写一种数据结构,来维护一些数( 都是 1 0 9 10^9 109 以内的数字)的集合,最开始时集合是空的。其中需要提供以下操作,操作次数 q q q 不超过 1 0 4 10^4 104:
- 查询 x x x 数的排名(排名定义为比当前数小的数的个数 + 1 +1 +1。若有多个相同的数,应输出最小的排名)。
- 查询排名为 x x x 的数。
- 求 x x x 的前驱(前驱定义为小于 x x x,且最大的数)。若未找到则输出 − 2147483647 -2147483647 −2147483647。
- 求 x x x 的后继(后继定义为大于 x x x,且最小的数)。若未找到则输出 2147483647 2147483647 2147483647。
- 插入一个数 x x x。
输入格式
输出格式
样例 #1
样例输入 #1
7
5 1
5 3
5 5
1 3
2 2
3 3
4 3
样例输出 #1
2
3
1
5
学到一个黑科技,当使用 multiset
求某个值的 前驱 / 后继 时,我们可以先往 multiset
中插入一个正无穷和一个负无穷两个哨兵,之后,找 前驱 / 后继 就无需特殊判断了。
注意,本题对于 前驱 / 后继 的定义是:小于 x
/ 大于 x
,且 最大 / 最小 的数。
具体代码片段如下:
tr.insert(inf), tr.insert(-inf); //插入哨兵
auto p1 = tr.lower_bound(x);
int down = *(--p1); //前驱
auto p2 = tr.upper_bound(x);
int up = *p2; //后继
代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <bits/stdc++.h>
using namespace std;
//#define int long long
//#define map unordered_map
const int N = 1e4 + 10, inf = 2147483647;
signed main()
{
int T = 1; //cin >> T;
while (T--)
{
multiset<int> tr;
tr.insert(inf), tr.insert(-inf);
int n; cin >> n;
while (n--)
{
int op, x;
cin >> op >> x;
if (op == 5)
{
tr.insert(x);
}
else if (op == 1)
{
int rk = 0;
for (auto i = tr.begin(); i != tr.end(); i++)
{
if ((*i) < x)
{
rk++;
}
}
cout << rk << '\n';
}
else if (op == 2)
{
int rk = 0;
int tgt;
for (auto i = tr.begin(); i != tr.end(); i++)
{
rk++;
if (rk == x + 1)
{
tgt = (*i);
break;
}
}
cout << tgt << '\n';
}
else if (op == 3)
{
auto p = tr.lower_bound(x);
int down = *(--p);
cout << down << '\n';
}
else if (op == 4)
{
auto p = tr.upper_bound(x);
int up = *p;
cout << up << '\n';
}
}
}
return 0;
}
边栏推荐
- 点云投影图片
- 使用华为性能管理服务,按需配置采样率
- Kustomize使用手册
- 1287_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS
- Session cookies and tokens
- PCL之K-d树与八叉树
- The URL in the RTSP setup header of the axis device cannot take a parameter
- 12. Process synchronization and semaphore
- Dialogue Wu Gang: why do I believe in the rise of "big country brands"?
- 2.hacking-lab脚本关[详细writeup]
猜你喜欢
(5) Gear control setting of APA scene construction
14. Code implementation of semaphore
2. Hacking lab script off [detailed writeup]
《实习报告》Skywalking分布式链路追踪?
MySQL environment configuration
Use of vscode tool
Internet News: Tencent conference application market was officially launched; Soul went to Hong Kong to submit the listing application
[SUCTF2018]followme
shell编程01_Shell基础
华为游戏初始化init失败,返回错误码907135000
随机推荐
【AGC】构建服务3-认证服务示例
(5) Gear control setting of APA scene construction
How to get the password of cpolar?
Use of vscode tool
UVM learning - object attribute of UVM phase
Hdu1236 ranking (structure Sorting)
Jsp webshell Free from killing - The Foundation of JSP
Transport Optimization abstraction
【ARK UI】HarmonyOS ETS的启动页的实现
session-cookie与token
12. Process synchronization and semaphore
JSP webshell免殺——JSP的基礎
数据库字典Navicat自动生成版本
Record attributeerror: 'nonetype' object has no attribute 'nextcall‘
从.bag文件中读取并保存.jpg图片和.pcd点云
华为游戏初始化init失败,返回错误码907135000
HDU1234 开门人和关门人(水题)
Stm32 et développement de moteurs (système supérieur)
MySQL lethal serial question 4 -- are you familiar with MySQL logs?
"Matching" is true love, a new attitude for young people to make friends