当前位置:网站首页>Special topic of binary tree -- [deep base 16. Example 7] ordinary binary tree (simplified version) (multiset seeks the precursor and subsequent sentry Art)
Special topic of binary tree -- [deep base 16. Example 7] ordinary binary tree (simplified version) (multiset seeks the precursor and subsequent sentry Art)
2022-07-02 10:59:00 【Morgannr】
【 Deep base 16. example 7】 Ordinary binary tree ( Simplified edition )
Title Description
You need to write a data structure , To maintain some numbers ( All are 1 0 9 10^9 109 Numbers within ) Set , At first, the collection is empty . The following operations need to be provided , Operating frequency q q q No more than 1 0 4 10^4 104:
- Inquire about x x x Number of rankings ( Ranking is defined as the number of numbers smaller than the current number + 1 +1 +1. If there are more than one identical number , The smallest ranking should be output ).
- Query rank is x x x Number of numbers .
- seek x x x The precursor of ( Precursor is defined as less than x x x, And the largest number ). If not found, output − 2147483647 -2147483647 −2147483647.
- seek x x x In the subsequent ( The following definition is greater than x x x, And the smallest number ). If not found, output 2147483647 2147483647 2147483647.
- Insert a number x x x.
Input format
Output format
Examples #1
The sample input #1
7
5 1
5 3
5 5
1 3
2 2
3 3
4 3
Sample output #1
2
3
1
5
Learn a black Technology , When Use multiset
Find a value Forerunner / The subsequent when , We can The first multiset
Insert a positive infinity and a negative infinity two sentinels , after , look for Forerunner / The subsequent There is no need for special judgment .
Be careful , This question is for Forerunner / The subsequent Is defined as : Less than x
/ Greater than x
, And Maximum / Minimum Number of numbers .
Specific code snippet as follows :
tr.insert(inf), tr.insert(-inf); // Insert the sentry
auto p1 = tr.lower_bound(x);
int down = *(--p1); // Forerunner
auto p2 = tr.upper_bound(x);
int up = *p2; // The subsequent
Code :
#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;
}
边栏推荐
- UVM learning - object attribute of UVM phase
- Primary key policy problem
- 二叉树专题--AcWing 3384. 二叉树遍历(已知先序遍历 边建树 边输出中序遍历)
- 13. Semaphore critical zone protection
- 洛谷 P1892 [BOI2003]团伙(并查集变种 反集)
- Operator-1 first acquaintance with operator
- What is the significance of the college entrance examination
- Pywin32 opens the specified window
- 华为游戏初始化init失败,返回错误码907135000
- Win11 arm系统配置.net core环境变量
猜你喜欢
Special topic of binary tree -- acwing 47 Path with a certain value in binary tree (preorder traversal)
Read H264 parameters from mediarecord recording
Retrofit's callback hell is really vulnerable in kotlin synergy mode!
JSP webshell免杀——webshell免杀
[SUCTF2018]followme
二叉树专题--AcWing 19. 二叉树的下一个节点(找树中节点的后继)
简洁、快速、节约内存的Excel处理工具EasyExcel
拆解美图SaaS:开着飞机换引擎
UVM learning - build a simple UVM verification platform
Sus system availability scale
随机推荐
洛谷 P4281 [AHOI2008]紧急集合 / 聚会(树上倍增 LCA)
14. Code implementation of semaphore
首份中国企业敏捷实践白皮书发布| 附完整下载
【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
Special topic of binary tree -- acwing 47 Path with a certain value in binary tree (preorder traversal)
二叉树专题--【深基16.例7】普通二叉树(简化版)(multiset 求前驱 后继 哨兵法)
Special topic of binary tree -- acwing 1589 Building binary search tree
Introduction to MySQL 8 DBA foundation tutorial
Special topic of binary tree -- acwing 3540 Binary search tree building (use the board to build a binary search tree and output the pre -, middle -, and post sequence traversal)
Operator-1 first acquaintance with operator
Special topic of binary tree -- acwing 19 The next node of the binary tree (find the successor of the node in the tree)
[SUCTF2018]followme
HDU1228 A + B(map映射)
大华设备播放过程中设置播放速度
6种单例模式的实现方式
洛谷 P1892 [BOI2003]团伙(并查集变种 反集)
618再次霸榜的秘密何在?耐克最新财报给出答案
Set the playback speed during the playback of UOB equipment
QT学习日记7——QMainWindow
Analysis of hot spots in AI technology industry