Splay 树
#include <vector>
#include <queue>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <memory>
class Splay_bst
{
using value_type = int;
static constexpr value_type very_big = 2147483647, very_small = -2147483648;
struct node
{
node *ls, *rs, *fa;
value_type v;
int siz, cnt;
node(value_type v_ = 0, int siz_ = 1, int cnt_ = 1) : ls(nullptr), rs(nullptr), fa(nullptr), v(v_), siz(siz_), cnt(cnt_) {} /*
node(const node &obj) : ls(nullptr), rs(nullptr), fa(nullptr), v(obj.v), siz(obj.siz), cnt(obj.cnt)
{
if (obj.ls != nullptr)
ls = new node(*(obj.ls)), ls->fa = this;
if (obj.rs != nullptr)
rs = new node(*(obj.rs)), rs->fa = this;
}*/
bool islson() const
{
if (fa == nullptr)
return false;
return fa->ls == this;
}
void pushup_siz()
{
siz = cnt;
if (ls != nullptr)
siz += ls->siz;
if (rs != nullptr)
siz += rs->siz;
}
};
node *root;
void rotate(node *p)
{
node *f = p->fa;
if (f == nullptr)
return;
node *grdfa = f->fa;
bool flag = f->islson();
if (p->islson())
{
f->ls = p->rs;
if (f->ls != nullptr)
f->ls->fa = f;
p->rs = f;
}
else
{
f->rs = p->ls;
if (f->rs != nullptr)
f->rs->fa = f;
p->ls = f;
}
f->fa = p;
p->fa = grdfa;
if (grdfa != nullptr)
{
if (flag)
{
grdfa->ls = p;
}
else
{
grdfa->rs = p;
}
}
f->pushup_siz();
p->pushup_siz();
}
public:
void splay(node *p)
{
#ifdef debug
std::cout << "splay" << std::endl;
#endif
node *f = p->fa;
while ((f = p->fa) != nullptr)
{
if (f->fa != nullptr)
{
if (f->islson() == p->islson())
{
rotate(f);
#ifdef debug
std::cout << "zag ";
#endif
}
else
{
rotate(p);
#ifdef debug
std::cout << "zig ";
#endif
}
}
rotate(p);
#ifdef debug
std::cout << "zig ";
#endif
}
root = p;
#ifdef debug
std::cout << std::endl;
#endif
}
void insert(value_type val)
{
#ifdef debug
std::cout << "insert " << val << std::endl;
#endif
node *now = root, *f = nullptr;
while (now != nullptr)
{
f = now;
if (val > now->v)
{
now = now->rs;
}
else if (val == now->v)
{
now->cnt += 1;
now->pushup_siz();
goto splay_now;
}
else
{
now = now->ls;
}
}
#ifdef debug
std::cout << "get new node " << val << std::endl;
#endif
now = new node(val, 1, 1);
now->fa = f;
if (f != nullptr)
{
if (now->v > f->v)
{
f->rs = now;
}
else
{
f->ls = now;
}
}
splay_now:
splay(now);
}
void del(value_type val)
{
#ifdef debug
std::cout << "del " << val << std::endl;
#endif
node *now = root, *f = nullptr;
while (now != nullptr)
{
f = now;
if (val > now->v)
{
now = now->rs;
}
else if (val == now->v)
{
break;
}
else
{
now = now->ls;
}
}
if (now == nullptr)
return splay(f);
splay(now);
f = nullptr;
if (--(now->cnt) > 0)
return;
node *cur = now->ls;
if (cur == nullptr)
{
root = now->rs;
if (now->rs != nullptr)
now->rs->fa = nullptr;
delete now;
now = nullptr;
return;
}
while (cur->rs != nullptr)
cur = cur->rs;
cur->rs = now->rs;
if (now->rs != nullptr)
now->rs->fa = cur;
now->ls->fa = nullptr;
root = now->ls;
delete now;
now = nullptr;
cur->pushup_siz();
splay(cur);
}
int get_siz(node *p) const
{
if (p == nullptr)
return 0;
return p->siz;
}
int rank(value_type val)
{
#ifdef debug
std::cout << "rank" << val << std::endl;
#endif
int ans = 1;
node *now = root, *f = nullptr;
while (now != nullptr)
{
f = now;
if (val > now->v)
{
ans += get_siz(now->ls) + now->cnt;
now = now->rs;
}
else if (val == now->v)
{
ans += get_siz(now->ls);
goto splayf;
}
else
{
now = now->ls;
}
}
splayf:
if (f != nullptr)
splay(f);
return ans;
}
value_type kth_key(int rak)
{
#ifdef debug
std::cout << "kth_key" << rak << std::endl;
#endif
int siz;
node *now = root, *f = nullptr;
while (now != nullptr)
{
f = now;
if (rak <= (siz = get_siz(now->ls)))
now = now->ls;
else if ((rak -= (siz + now->cnt)) > 0)
{
now = now->rs;
}
else
{
break; // 正好是 now
}
}
if (now == nullptr)
now = f;
if (now != nullptr)
splay(now);
return now->v;
}
value_type precursor(value_type val)
{
node *now = root, *f = nullptr;
value_type ans = very_small;
while (now != nullptr)
{
f = now;
if (val <= now->v)
now = now->ls;
else
{
ans = now->v;
now = now->rs;
}
}
splay(f);
return ans;
}
value_type successor(value_type val)
{
node *now = root, *f = nullptr;
value_type ans = very_big;
while (now != nullptr)
{
f = now;
if (val >= now->v)
now = now->rs;
else
{
ans = now->v;
now = now->ls;
}
}
splay(f);
return ans;
}
void del_tr(node *&p)
{
if (p == nullptr)
return;
del_tr(p->ls);
del_tr(p->rs);
delete p;
p = nullptr;
return;
}
Splay_bst() : root(nullptr) {}
/*
Splay_bst(Splay_bst &&obj)
{
root = obj.root;
obj.root = nullptr;
}
Splay_bst &operator=(Splay_bst &&obj)
{
del_tr(root);
root = obj.root;
obj.root = nullptr;
return *this;
}
Splay_bst(const Splay_bst &obj) : root(nullptr)
{
root = new node(*(obj.root));
}
Splay_bst &operator=(const Splay_bst &obj)
{
if (&obj == this)
return *this;
del_tr(root);
root = new node(*(obj.root));
return *this;
}
*/
~Splay_bst()
{
del_tr(root);
}
} f;
using namespace std;
int main()
{
int n, opt, zzz;
cin >> n;
while (n--)
{
cin >> opt >> zzz;
switch (opt)
{
case 1:
f.insert(zzz);
break;
case 2:
f.del(zzz);
break;
case 3:
cout << f.rank(zzz) << endl;
break;
case 4:
cout << f.kth_key(zzz) << endl;
break;
case 5:
cout << f.precursor(zzz) << endl;
break;
case 6:
cout << f.successor(zzz) << endl;
break;
}
#ifdef debug
cout << "n=" << n << endl;
#endif
}
return 0;
}