其他分享
首页 > 其他分享> > P2596 [ZJOI2006]书架(fhq treap)

P2596 [ZJOI2006]书架(fhq treap)

作者:互联网

P2596 [ZJOI2006]书架

我们用fhq treap来完成这一题

对于一个新插入的节点我们取权值为其索引值,其所记录的 v a l u e value value是其当前索引所在位置。

操作一:把索引为 v a l u e value value的点放到平衡树前面,分别别得到三颗子树 x , y , z x, y, z x,y,z(前段子树,索引为 v a l u e value value所代表的子树,后段子树),同时把 v a l [ y ] val[y] val[y]修改成全局最小,然后按照顺序 m e r g e ( y , x , z ) merge(y, x, z) merge(y,x,z)。

操作二:与操作一类似得到 x , y , z x, y, z x,y,z,然后把 v a l [ y ] val[y] val[y]改成全局最大,按照顺序 m e r g e ( x , z , y ) merge(x, z, y) merge(x,z,y)。

操作三: t = 0 t = 0 t=0不做操作, t = 1 t = 1 t=1得到四颗子树 x , y , z , w x, y, z, w x,y,z,w(前段子树, v a l u e value value所代表的子树, v a l u e value value的后继,后端子树),交换信息,然后按照顺序 m e r g e ( x , z , y , w ) merge(x, z, y, w) merge(x,z,y,w), t = − 1 t = -1 t=−1得到四颗子树 x , y , z , w x, y, z, w x,y,z,w(前段子树, v a l u e value value的前驱, v a l u e value value所代表的子树,后端子树),交换信息,然后按照顺序 m e r g e ( x , z , y , w ) merge(x, z, y, w) merge(x,z,y,w)。

操作四:按照值分裂成两棵树,然后输出第一颗树的大小即可。

操作五:直接查找第 k k k大即可。

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

mt19937 rnd(233);

int minn, maxn;

struct Tree {
  int ls[N], rs[N], val[N], sz[N], key[N], root, cnt;

  
  void push_up(int rt) {
    sz[rt] = sz[ls[rt]] + sz[rs[rt]] + 1;
  }

  int new_node(int value, int pos) {
    val[value] = pos, sz[value] = 1, key[value] = rnd();
    return value;
  }

  void split(int rt, int value, int &x, int &y) {
    if (!rt) {
      x = y = 0;
      return ;
    }
    if (val[rt] <= value) {
      x = rt;
      split(rs[rt], value, rs[rt], y);
    }
    else {
      y = rt;
      split(ls[rt], value, x, ls[rt]);
    }
    push_up(rt);
  }

  int merge(int x, int y) {
    if (!x || !y) {
      return x | y;
    }
    if (key[x] < key[y]) {
      rs[x] = merge(rs[x], y);
      push_up(x);
      return x;
    }
    else {
      ls[y] = merge(x, ls[y]);
      push_up(y);
      return y;
    }
  }

  int get_num(int rt, int rank) {
    while (rt) {
      if (sz[ls[rt]] + 1 == rank) {
        break;
      }
      if (sz[ls[rt]] >= rank) {
        rt = ls[rt];
      }
      else {
        rank -= sz[ls[rt]] + 1;
        rt = rs[rt];
      }
    }
    return rt;
  }

  int get_num(int rank) {
    return get_num(root, rank);
  }

  void insert(int value, int pos) {
    int x, y;
    split(root, pos, x, y);
    root = merge(merge(x, new_node(value, pos)), y);
  }

  void update(int value, int op) {
    int x, y, z;
    split(root, val[value], x, z);
    split(x, val[value] - 1, x, y);
    if (op) {
      val[value] = --minn;
      root = merge(merge(y, x), z);
    }
    else {
      val[value] = ++maxn;
      root = merge(merge(x, z), y);
    }
  }

  void reverse(int value, int op) {
    if (!op) {
      return ;
    }
    if (op == 1) {
      int x, y, z, w;
      split(root, val[value], x, z);
      split(x, val[value] - 1, x, y);
      int t = get_num(z, 1);
      split(z, val[t], z, w);
      swap(val[y], val[z]);
      root = merge(merge(x, z), merge(y, w));
    }
    else {
      int x, y, z, w;
      split(root, val[value] - 1, x, z);
      split(z, val[value], z, w);
      int t = get_num(x, sz[x]);
      split(x, val[t] - 1, x, y);
      swap(val[y], val[z]);
      root = merge(merge(x, z), merge(y, w));
    }
  }

  int get_rank(int value) {
    int x, y, ans;
    split(root, val[value] - 1, x, y);
    ans = sz[x];
    root = merge(x, y);
    return ans;
  }
}tree;

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n, m;
  scanf("%d %d", &n, &m);
  minn = 1, maxn = n;
  for (int i = 1, x; i <= n; i++) {
    scanf("%d", &x);
    tree.insert(x, i);
  }
  char op[10];
  for (int i = 1, s, t; i <= m; i++) {
    scanf("%s %d", op, &s);
    if (op[0] == 'T') {
      tree.update(s, 1);
    }
    else if (op[0] == 'B') {
      tree.update(s, 0);
    }
    else if (op[0] == 'I') {
      scanf("%d", &t);
      tree.reverse(s, t);
    }
    else if (op[0] == 'A') {
      printf("%d\n", tree.get_rank(s));
    }
    else {
      printf("%d\n", tree.get_num(s));
    }
  }
  return 0;
}

标签:rt,val,merge,int,value,treap,P2596,ZJOI2006,root
来源: https://blog.csdn.net/weixin_45483201/article/details/113838330