平衡树模板

· · 个人记录

替罪羊

#include <algorithm>
#include <iostream>

using namespace std;

const int MaxN = 4e5 + 10;
const double eps = 0.75;

int d[MaxN], l[MaxN], r[MaxN], cnt[MaxN], sum[MaxN], sz[MaxN], a[MaxN], tot, n, root, len;

void update(int x) {  // 更新
  sum[x] = sum[l[x]] + sum[r[x]] + cnt[x];
  sz[x] = sz[l[x]] + sz[r[x]] + 1;
}

bool Check(int x) {  // 判断是否需要重构
  return cnt[x] && 1.0 * max(sz[l[x]], sz[r[x]]) > eps * sz[x];
}

void dfs(int x) {  // 中序遍历
  if (!x) {
    return;
  }
  dfs(l[x]);
  cnt[x] && (a[++len] = x);
  dfs(r[x]);
}

int build(int pl, int pr) {  // 二分重构
  if (pl > pr) {
    return 0;
  }
  int mid = (pl + pr) >> 1;
  l[a[mid]] = build(pl, mid - 1);
  r[a[mid]] = build(mid + 1, pr);
  update(a[mid]);
  return a[mid];
}

int G(int k) {
  len = 0;
  dfs(k);
  return k = build(1, len);
}

void insert(int &k, int x) {  // 添加
  if (!k) {
    k = ++tot;
    (!root) && (root = tot);
    d[tot] = x, l[tot] = r[tot] = 0, sum[tot] = cnt[tot] = sz[tot] = 1;
    return;
  }
  if (d[k] == x) {
    cnt[k]++;
  } else if (d[k] < x) {
    insert(r[k], x);
  } else {
    insert(l[k], x);
  }
  update(k);
  if (Check(k)) {
    k = G(k);
  }
}

void delet(int &k, int x) {  // 删除
  if (!k) {
    return;
  }
  if (d[k] == x) {
    cnt[k] && (cnt[k]--);
  } else if (d[k] < x) {
    delet(r[k], x);
  } else {
    delet(l[k], x);
  }
  update(k);
  if (Check(k)) {
    k = G(k);
  }
}

int At(int k, int x) {  // @
  if (!k) {
    return 0;
  }
  if (sum[l[k]] < x && x <= sum[l[k]] + cnt[k]) {
    return d[k];
  } else if (sum[l[k]] + cnt[k] < x) {
    return At(r[k], x - sum[l[k]] - cnt[k]);
  }
  return At(l[k], x);
}

int upbd(int k, int x) {  // 大于
  if (!k) {
    return 1;
  }
  if (d[k] == x && cnt[k]) {
    return sum[l[k]] + cnt[k] + 1;
  } else if (x < d[k]) {
    return upbd(l[k], x);
  }
  return upbd(r[k], x) + sum[l[k]] + cnt[k];
}

int upgrt(int k, int x) {  // 小于
  if (!k) {
    return 0;
  }
  if (d[k] == x && cnt[k]) {
    return sum[l[k]];
  } else if (d[k] < x) {
    return upgrt(r[k], x) + sum[l[k]] + cnt[k];
  }
  return upgrt(l[k], x);
}

int p(int x) {  // 前驱
  return At(root, upgrt(root, x));
}

int h(int x) {  // 后继
  return At(root, upbd(root, x));
}

int main() {
  cin >> n;
  for (int op, x; n; n--) {
    cin >> op >> x;
    if (op == 1) {
      insert(root, x);
    } else if (op == 2) {
      delet(root, x);
    } else if (op == 3) {
      cout << upgrt(root, x) + 1 << '\n';
    } else if (op == 4) {
      cout << At(root, x) << '\n';
    } else if (op == 5) {
      cout << p(x) << '\n';
    } else {
      cout << h(x) << '\n';
    }
  }
  return 0;
}

旋转Treap

#include <ctime>
#include <iostream>
#include <random>

using namespace std;

const int MaxN = 1e5 + 10;

struct Node {
  int d, v, l, r, sum, cnt;
} w[MaxN];

int n, root, tot;

void update(int x) {
  w[x].sum = w[w[x].l].sum + w[w[x].r].sum + w[x].cnt;
}

void left(int &k) {
  int p = w[k].l;
  w[k].l = w[p].r, w[p].r = k, k = p;
  update(w[k].r), update(k);
}

void right(int &k) {
  int p = w[k].r;
  w[k].r = w[p].l, w[p].l = k, k = p;
  update(w[k].l), update(k);
}

void insert(int &k, int x) {
  if (!k) {
    k = ++tot;
    w[k] = {x, rand(), 0, 0, 1, 1};
    return;
  }
  if (w[k].d == x) {
    w[k].cnt++;
  } else if (w[k].d < x) {
    insert(w[k].r, x);
    if (w[w[k].r].v < w[k].v) {
      right(k);
    }
  } else {
    insert(w[k].l, x);
    if (w[w[k].l].v < w[k].v) {
      left(k);
    }
  }
  update(k);
}

void delet(int &k, int x) {
  if (!k) {
    return;
  }
  if (w[k].d == x) {
    if (w[k].cnt) {
      w[k].cnt--, update(k);
    } else if (w[k].l || w[k].r) {
      if (!w[k].r || w[w[k].l].d > w[w[k].r].d) {
        left(k), delet(w[k].l, x);
      } else {
        right(k), delet(w[k].r, x);
      }
      update(k);
    } else {
      k = 0;
    }
  }
  if (w[k].d < x) {
    delet(w[k].r, x);
  } else {
    delet(w[k].l, x);
  }
  update(k);
}

int upbd(int k, int x) {
  if (!k) {
    return 1;
  }
  if (w[k].d == x) {
    return w[w[k].l].sum + w[k].cnt + 1;
  } else if (w[k].d < x) {
    return upbd(w[k].r, x) + w[w[k].l].sum + w[k].cnt;
  }
  return upbd(w[k].l, x);
}
int upgrt(int k, int x) {
  if (!k) {
    return 0;
  }
  if (w[k].d == x) {
    return w[w[k].l].sum;
  } else if (x < w[k].d) {
    return upgrt(w[k].l, x);
  }
  return upgrt(w[k].r, x) + w[w[k].l].sum + w[k].cnt;
}

int At(int k, int x) {
  if (!k) {
    return 0;
  }
  if (w[w[k].l].sum < x && x <= w[w[k].l].sum + w[k].cnt) {
    return w[k].d;
  } else if (w[w[k].l].sum + w[k].cnt < x) {
    return At(w[k].r, x - w[w[k].l].sum - w[k].cnt);
  }
  return At(w[k].l, x);
}

int main() {
  srand(time(NULL));
  cin >> n;
  for (int op, x; n; n--) {
    cin >> op >> x;
    if (op == 1) {
      insert(root, x);
    } else if (op == 2) {
      delet(root, x);
    } else if (op == 3) {
      cout << upgrt(root, x) + 1 << '\n';
    } else if (op == 4) {
      cout << At(root, x) << '\n';
    } else if (op == 5) {
      cout << At(root, upgrt(root, x)) << '\n';
    } else {
      cout << At(root, upbd(root, x)) << '\n';
    }
  }
  return 0;
}

FHQ

#include <ctime>
#include <iostream>

using namespace std;

const int MaxN = 1e5 + 10;

struct S {
  int w, v, l, r, sum;
} a[MaxN];

int n, root, tot;

void update(int k) {
  a[k].sum = k ? a[a[k].l].sum + a[a[k].r].sum + 1 : 0;
}

void split(int k, int v, int &x, int &y) {
  if (!k) {
    return;
  }
  if (a[k].w <= v) {
    x = k;
    int r = a[k].r;
    split(r, v, a[k].r = 0, y);
  } else {
    y = k;
    int l = a[k].l;
    split(l, v, x, a[k].l = 0);
  }
  update(k);
}

int merge(int x, int y) {
  if (!x || !y) {
    return x + y;
  }
  if (a[x].v <= a[y].v) {
    a[x].r = merge(a[x].r, y), update(x);
    return x;
  } else {
    a[y].l = merge(x, a[y].l), update(y);
    return y;
  }
}

void insert(int &k, int val) {
  int x = 0, y = 0;
  a[++tot] = {val, rand(), 0, 0, 1};
  split(k, val, x, y);
  k = merge(merge(x, tot), y);
}

void delet(int &k, int x) {
  if (a[k].w == x) {
    k = merge(a[k].l, a[k].r);
  } else {
    a[k].w <= x ? delet(a[k].r, x) : delet(a[k].l, x);
    update(k);
  } 
}

int upgrt(int k, int x) {
  if (!k) {
    return 0;
  }
  if (a[k].w >= x) {
    return upgrt(a[k].l, x);
  }
  return upgrt(a[k].r, x) + a[a[k].l].sum + 1;
}

int At(int k, int x) {
  if (!k) { 
    return 0;
  }
  if (a[a[k].l].sum < x && x <= a[a[k].l].sum + 1) {
    return a[k].w;
  } else if (a[a[k].l].sum + 1 < x) {
    return At(a[k].r, x - a[a[k].l].sum - 1);
  }
  return At(a[k].l, x);
}

int main() {
  srand(time(NULL));
  cin >> n;
  for (int op, x; n; n--) {
    cin >> op >> x;
    if (op == 1) {
      insert(root, x);
    } else if (op == 2) {
      delet(root, x);
    } else if (op == 3) {
      cout << upgrt(root, x) + 1 << '\n';
    } else if (op == 4) {
      cout << At(root, x) << '\n';
    } else if (op == 5) {
      cout << At(root, upgrt(root, x)) << '\n';
    } else {
      cout << At(root, upgrt(root, x + 1) + 1)  << '\n';
    }
  }
  return 0;
}

FHQ进阶

#include <iostream>
#include <ctime>

using namespace std;

const int MaxN = 2e5 + 10;

struct Tree {
  struct Node {
    int w, v, l, r, fa, sum, tag;
    char c;
  } a[MaxN];

  int root, tot;
  bool vis[MaxN];

  void update(int k) {
    a[k].sum = a[a[k].l].sum + a[a[k].r].sum + 1;
    a[k].w = a[a[k].l].w | a[a[k].r].w | (1 << (a[k].c - 'a'));
    (a[k].l) && (a[a[k].l].fa = k);
    (a[k].r) && (a[a[k].r].fa = k);
    a[k].fa = 0;
  }

  void pd(int k) {
    if (a[k].tag) {
      swap(a[k].l, a[k].r);
      a[a[k].l].tag ^= 1;
      a[a[k].r].tag ^= 1;
      a[k].tag = 0;
    }
  }

  void split(int k, int v, int &x, int &y) {
    if (!k) {
      x = y = 0;
      return;
    }
    pd(k);
    if (a[a[k].l].sum < v) {
      x = k;
      split(a[k].r, v - a[a[k].l].sum - 1, a[k].r, y);
    } else {
      y = k;
      split(a[k].l, v, x, a[k].l);
    }
    update(k);
  }

  void merge(int &k, int x, int y) {
    if (!x || !y) {
      k = x + y;
      return;
    }
    if (a[x].v > a[y].v) {
      pd(x);
      merge(a[x].r, a[x].r, y);
      k = x;
    } else {
      pd(y);
      merge(a[y].l, x, a[y].l);
      k = y;
    }
    update(k);
  }

  void insert(int id, char v) {
    int x, y;    
    a[++tot] = {1 << (v - 'a'), rand(), 0, 0, 0, 1, 0, v};
    split(root, id, x, y);
    merge(x, x, tot);
    merge(root, x, y);
  }

  void erase(int id) {
    int x, y, z;
    split(root, id - 1, x, y);
    split(y, 1, y, z);
    vis[y] = 1;
    merge(root, x, z);
  }

  int lb(int x) {
    return x & -x;
  }

  int G(int x) {
    int ans = 0;
    for (x; x; x -= lb(x), ans++) {
    }
    return ans;
  }

  int query(int l, int r) {
    int x, y, z;
    split(root, r, x, y);
    split(x, l - 1, x, z);
    int res = a[z].w;
    merge(x, x, z);
    merge(root, x, y);
    return G(res);
  }

  void reverse(int l, int r) {
    int x, y, z;
    split(root, r, x, y);
    split(x, l - 1, x, z);
    a[z].tag ^= 1;
    merge(x, x, z);
    merge(root, x, y);
  }

  char At(int k, int x) {
    if (!k) {
      return 0;
    }
    pd(k);
    if (a[a[k].l].sum + 1 < x) {
      return At(a[k].r, x - a[a[k].l].sum - 1);
    } else if (a[a[k].l].sum >= x) {
      return At(a[k].l, x);
    } else {
      return a[k].c;
    }
  }

  void Debug(int k) {
    if (!k) {
      return;
    }
    pd(k);
    Debug(a[k].l);
    cout << a[k].c << " ";
    Debug(a[k].r);
  }

  void DFS(int x) {
    if (!x) return;
    DFS(a[x].fa);
    pd(x);
  }

  int query(int k) {
    if (k == root) {
      return 0;
    }
    if (a[a[k].fa].r == k) {
      return a[a[a[k].fa].l].sum + 1 + query(a[k].fa);
    }
    return query(a[k].fa);
  }
} tr;

int n, m;
string s;
char c, op;

int main() {
  srand(time(NULL));
  cin >> n >> m >> s, s = " " + s;
  for (int i = 1; i <= n; i++) {
    tr.insert(i, s[i]);
  }
  for (int x, y; m; m--) {
    cin >> op >> x;
    if (op == 'I') {
      cin >> c;
      tr.insert(x, c);
    } else if (op == 'D') {
      tr.erase(x);
    } else if (op == 'R') {
      cin >> y;
      tr.reverse(x, y);
    } else if (op == 'P') {
      if (tr.vis[x]) {
        cout << 0 << endl;
        continue;
      }
      tr.DFS(x);
      cout << tr.query(x) + 1 + tr.a[tr.a[x].l].sum << endl;
    } else if (op == 'T') {
      cout << tr.At(tr.root, x) << endl;
    } else if (op == 'Q') {
      cin >> y;
      cout << tr.query(x, y) << endl;
    }
  } 
  return 0;
}

splay

#include <iostream>

using namespace std;

const int MaxN = 1e6 + 1e5 + 10;

struct Tree {
  struct Node {
    int w, ch[2], fa, cnt, sum, tag, x;
  } a[MaxN];

  int tot, root, l, r;

  Node operator[](int k) {
    return a[k];
  }

  void update(int k) {
    a[k].sum = a[a[k].ch[0]].sum + a[a[k].ch[1]].sum + a[k].cnt;
  }

  void pushdown(int k) {
    if (a[k].tag) {
      swap(a[k].ch[0], a[k].ch[1]);
      a[a[k].ch[1]].tag ^= 1;
      a[a[k].ch[0]].tag ^= 1;
      a[k].tag = 0;
      }
  }

  void rotatr(int x) {
    int y = a[x].fa, z = a[y].fa, k = a[y].ch[1] == x;
    a[z].ch[y == a[z].ch[1]] = x, a[x].fa = z;
    a[y].ch[k] = a[x].ch[k ^ 1], a[a[x].ch[k ^ 1]].fa = y;
    a[x].ch[k ^ 1] = y, a[y].fa = x;
    update(y);
  }

  void splay(int x, int to = 0) {
    for (int y, z; a[x].fa != to; y = a[x].fa, z = a[y].fa, (z != to) ? (((y == a[z].ch[1]) ^ (x == a[y].ch[1])) ? rotatr(x) : rotatr(y)) : void(), rotatr(x)) {
    }
    update(x), (to) || (root = x);
  }

  int At(int x) {
    int k = root;
    if (!k) return k;
    for (; (pushdown(k), a[k].ch[x > a[k].x]) && a[k].x != x; k = a[k].ch[x > a[k].x]) {
    }
    return splay(k), k;
  }

  void insert(int x) {
    int k = root, y = 0;
    for (; k && a[k].x != x; y = k, k = a[k].ch[x > a[k].x]) {
    }
    (k) ? (a[k].cnt++) : (k = ++tot, a[k] = {x, {0, 0}, y, 1, 1, 0, x}, (y) && (a[y].ch[x > a[y].x] = k));
    splay(k);
  }

  Tree() {
    root = tot = 0;
    a[0] = {0, {0, 0}, 0, 0, 0, 0};
    insert(-2e9), insert(2e9);
  }

  int kth(int x) {
    int k = root;
    for (int tx, kll; (pushdown(k), k) && !(a[a[k].ch[0]].sum < x && x <= a[a[k].ch[0]].sum + a[k].cnt); kll = a[k].cnt + a[a[k].ch[0]].sum, tx = x, (kll < tx) && (x -= kll), k = a[k].ch[kll < tx]) {
    }
    return splay(k), k;
  }

  int pn(int x, bool flag) {  // 0 为前驱,1 为后继
    At(x);
    int k = root;
    if (a[k].x > x && flag || a[k].x < x && !flag) return splay(k), k;
    k = a[k].ch[flag];
    for (; a[k].ch[!flag]; k = a[k].ch[!flag]) {
    }
    return splay(k), k;
  }

  void erase(int x) {
    int pre = pn(x, 0), nxt = pn(x, 1);
    splay(pre), splay(nxt, pre);
    int k = a[nxt].ch[0];
    if (a[k].cnt > 1) {
      a[k].cnt--;
      splay(k);
    } else {
      a[nxt].ch[0] = 0;
      splay(nxt);
    }
  }

  void reverse(int l, int r){
    l = kth(l), r = kth(r + 2);
    splay(l), splay(r, l);
    a[a[r].ch[0]].tag ^= 1;
  }

  void DFS(int x) {
    if (!x) return;
    pushdown(x);
    DFS(a[x].ch[0]);
    if (a[x].x != -2e9 && a[x].x != 2e9) cout << a[x].x << " ";
    DFS(a[x].ch[1]);
  }
} tree;

int n, m, ans;

int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    tree.insert(i);
  }
  for (int i = 1, l, r; i <= m; i++) {
    cin >> l >> r;
    tree.reverse(l, r);
  }
  tree.DFS(tree.root);
  return 0;
}