当前位置:网站首页>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;
}
边栏推荐
- How to get the password of cpolar?
- UWA报告使用小技巧,你get了吗?(第四弹)
- 从MediaRecord录像中读取H264参数
- Start class, data analysis, high salary training plan, elite class
- 二叉树专题--AcWing 19. 二叉树的下一个节点(找树中节点的后继)
- 二叉树专题--AcWing 3384. 二叉树遍历(已知先序遍历 边建树 边输出中序遍历)
- Special topic of binary tree -- acwing 1497 Traversal of the tree (use post and mid order traversal to build a binary tree)
- 洛谷 P3398 仓鼠找 sugar(树上倍增 lca 判断树中两条路径是否相交 结论)
- 2022爱分析· 国央企数字化厂商全景报告
- 12. Process synchronization and semaphore
猜你喜欢

OpenMLDB Meetup No.4 会议纪要

Kustomize user manual

Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer

Easyexcel, a concise, fast and memory saving excel processing tool

二叉树专题--AcWing 19. 二叉树的下一个节点(找树中节点的后继)

12. Process synchronization and semaphore

axis设备的rtsp setup头中的url不能带参

JSP webshell免杀——JSP的基础

全网显示 IP 归属地,是怎么实现的?

From Read and save in bag file Jpg pictures and PCD point cloud
随机推荐
MySQL keyword
Matlab processing of distance measurement of experimental electron microscope
Internet News: Tencent conference application market was officially launched; Soul went to Hong Kong to submit the listing application
2022爱分析· 国央企数字化厂商全景报告
UVM learning - build a simple UVM verification platform
QT学习日记8——资源文件添加
二叉树专题--洛谷 P3884 [JLOI2009]二叉树问题(dfs求二叉树深度 bfs求二叉树宽度 dijkstra求最短路)
最详细MySql安装教程
【AGC】构建服务3-认证服务示例
OpenMLDB Meetup No.4 会议纪要
Filtering of PCL
6种单例模式的实现方式
UWA report uses tips. Did you get it? (the fourth bullet)
AttributeError: type object ‘Image‘ has no attribute ‘fromarray‘
618 what is the secret of dominating the list again? Nike's latest financial report gives the answer
How to get the password of cpolar?
2022-06-17
static 函数中的静态变量
Kustomize user manual
使用sqlcipher打开加密的sqlite方法