Splay 模板题

先介绍一下BST的性质:

BST是一颗左儿子小于当前点权值,右儿子大于当前节点权值的一颗二叉树,这样一棵树有什么好处呢?这样的一棵树十分的强大,因为不管怎么变化这颗树的形状,只要符合以上条件,呢么这颗树的中序遍历都是从小到大的排序。

缺点:

这样一棵树可能因为某种单调的数据插入,从而退化成一条链,这样最坏的时间复杂度就会演变为 O ( n ) O(n) O(n)的,这是我们不愿意看到的,显然这么强大的数据结构,时间复杂度演变成 O ( n ) O(n) O(n)是谁也不愿意看到的,所以科学家 Tarjan 就优化出了 Splay 这颗平衡树。

Splay

Splay的核心操作就是伸展,每次伸展,我们需要把伸展的点,伸展到根节点,伸展的核心是旋转,每次伸展时,会破坏最坏情况下的链状平衡树,从而让一颗树变成一个树高 l o g log log级的数据结构

学习Splay的目的其实是为了学习LCT这个传说中的算法,但目前看来,还需要多刷击倒Splay的题目才能掌握Splay。

模板题:P3369

参考代码:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 5;
const int mod = 998244353;
void read(int& v) {
  int k = 1;
  v = 0;
  int c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      k = 0;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    v = (v << 3) + (v << 1) + (c - 48), c = getchar();
  if (k == 0)
    v = -v;
}
inline ll ksm(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a % mod;
    b >>= 1, a = a * a % mod;
  }
  return res;
}
int St[maxn][2], fa[maxn], rot, w[maxn];
int cnt = 0, size[maxn], sum[maxn];
void pushup(int k) {
  size[k] = size[St[k][1]] + size[St[k][0]] + sum[k];
}
void Rotate(int x) {
  int y = fa[x], z = fa[y], k = (St[y][1] == x);
  St[z][y == St[z][1]] = x, fa[x] = z;
  St[y][k] = St[x][k ^ 1], fa[St[x][k ^ 1]] = y;
  St[x][k ^ 1] = y, fa[y] = x;
  pushup(y), pushup(x);
}
void splay(int x, int pos) {
  while (fa[x] != pos) {
    int y = fa[x], z = fa[y];
    if (z != pos) {
      (St[z][0] == y) ^ (St[y][0] == x) ? Rotate(x) : Rotate(y);
    }
    Rotate(x);
  }
  if (pos == 0)
    rot = x;
}
void Insert(int x) {
  int f = 0, u = rot;
  while (u && (x != w[u])) {
    f = u;
    u = St[u][x > w[u]];
  }
  if (u) {
    sum[u]++;
  } else {
    u = ++cnt;
    if (f == 0)
      rot = u;
    else
      St[f][x > w[f]] = u;
    size[u] = sum[u] = 1, w[u] = x, fa[u] = f;
  }
  splay(u, 0);
}
void query(int x) {
  int u = rot;
  if (!u)
    return;
  while ((x != w[u]) && St[u][x > w[u]])
    u = St[u][x > w[u]];
  splay(u, 0);
}
int pre(int x) {
  query(x);
  int u = rot;
  if (w[u] < x)
    return u;
  u = St[u][0];
  while (St[u][1])
    u = St[u][1];
  return u;
}
int nxt(int x) {
  query(x);
  int u = rot;
  if (w[u] > x)
    return u;
  u = St[u][1];
  while (St[u][0])
    u = St[u][0];
  return u;
}
void Delete(int x) {
  int p = pre(x), n = nxt(x);
  splay(p, 0), splay(n, p);
  int u = St[n][0];
  if (sum[u] > 1)
    sum[u]--, splay(u, 0);
  else
    St[n][0] = 0;
}
int kth(int k) {
  int u = rot;
  while (true) {
    if (size[St[u][0]] + sum[u] < k)
      k -= size[St[u][0]] + sum[u], u = St[u][1];
    else if (size[St[u][0]] >= k)
      u = St[u][0];
    else
      return u;
  }
}
int rank(int x) {
  query(x);
  return size[St[rot][0]];
}
int main() {
  int n, q, x;
  read(n);
  Insert(-100000000);
  Insert(100000000);
  while (n--) {
    int x, op;
    read(op), read(x);
    if (op == 1)
      Insert(x);
    if (op == 2)
      Delete(x);
    if (op == 3) {
      printf("%d\n", rank(x));
    }
    if (op == 4)
      printf("%d\n", w[kth(x + 1)]);
    if (op == 5) {
      printf("%d\n", w[pre(x)]);
    }
    if (op == 6) {
      printf("%d\n", w[nxt(x)]);
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值