题目描述
您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
插入 xx 数
删除 xx 数(若有多个相同的数,因只删除一个)
查询 xx 数的排名(排名定义为比当前数小的数的个数 +1+1 )
查询排名为 xx 的数
求 xx 的前驱(前驱定义为小于 xx,且最大的数)
求 xx 的后继(后继定义为大于 xx,且最小的数)
输入格式
第一行为 nn,表示操作的个数,下面 nn 行每行有两个数 \text{opt}opt 和 xx,\text{opt}opt 表示操作的序号( 1 \leq \text{opt} \leq 61≤opt≤6 )
输出格式
对于操作 3,4,5,63,4,5,6 每行输出一个数,表示对应答案
输入输出样例
输入 #1复制
10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
输出 #1复制
106465
84185
492737
说明/提示
【数据范围】
对于 100%100% 的数据,1\le n \le 10^51≤n≤10
5
,|x| \le 10^7∣x∣≤10
7
来源:Tyvj1728 原名:普通平衡树
在此鸣谢
extc++ yyds stl yyds
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace std;
template <class T>
using avl = __gnu_pbds::tree<T,
__gnu_pbds::null_type,
less<T>,
__gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update>;
using ll = long long;
using LL = __int128;
template<class T>
inline void read(T& x){
x=0; T f=1;
char ch = getchar();
while(ch<'0'||ch>'9'){
if(ch=='-') f=-1; ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+ch-'0'; ch=getchar();
}
x=x*f;
}
inline int read(){
int x;read(x);
return x;
}
template<class T>
inline void writ(const T& x) {
if (x > 9) writ(x / 10);
putchar(x % 10 + '0');
}
template<class T>
inline void write(T x) {
if (x < 0) {
putchar('-');
x=-x;
}
writ(x);
putchar(10);
}
avl<ll> t;
int main()
{
int n = read();
for (int i = 1; i <= n; i++)
{
LL x;
int op=read();
read(x);
if(op==1){
t.insert((x<<20)+i);
}
else if(op==2){
t.erase(t.lower_bound(x<<20));
}
else if(op==3){
auto k = t.order_of_key( x<<20 );
write(k+1);
}
else if(op==4){
auto k = t.find_by_order((int)x-1);
auto res = (*k)>>20;
write(res);
}
else if(op==5){
auto k = t.lower_bound(x<<20);
k--;
write((*k)>>20);
}
else{
auto k = t.lower_bound((x+1)<<20);
write((*k)>>20);
}
}
return 0;
}
11.14更新leetcode 67场双周赛
#include<bits/extc++.h>
using namespace __gnu_pbds;
template<class T>
using avl = __gnu_pbds::tree<T,__gnu_pbds::null_type,less<T>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update >;
class SORTracker {
struct node{
int sc;
string nm;
bool operator <(const node& t)const{
return sc==t.sc?nm<t.nm:sc<t.sc;
}
};
avl<node> trr;
int cnt;
public:
SORTracker():cnt(0){
trr.clear();
}
void add(string name, int score) {
trr.insert({-score,name});
}
string get() {
auto res = trr.find_by_order(cnt++);
return res->nm;
}
};