跳到正文

剪贴板

剪贴板 5g5ylrt4

原文
剪贴板信息

剪贴板信息

收录
更新

Main code:

C++
constexpr int SIZE = 2000000;
mt19937 rnd(random_device{}());
typedef int T;
#define isNull(x) (x->son[0] == x)

struct TreapNode {
  TreapNode *son[2];
  int size, priority;
  T val;
  inline void pushup() {
    if (isNull(this))
      return;
    size = son[0]->size + son[1]->size + 1;
  }
};

struct TreapType {
  TreapNode MemeryPool[SIZE], *const null = MemeryPool, *root = null, *pointer = null;
  TreapType() {
    null->son[0] = null->son[1] = null;
  }
  inline TreapNode *allocate(T num) {
    return *++pointer = {null, null, 1, (int)rnd(), num}, pointer;
  }

  void split(TreapNode *r, T num, TreapNode *&x, TreapNode *&y) {
    if (r == null) {
      x = y = null;
      return;
    }
    if (r->val <= num)
      x = r, split(r->son[1], num, x->son[1], y), x->pushup();
    else
      y = r, split(r->son[0], num, x, y->son[0]), y->pushup();
  }
  TreapNode *merge(TreapNode *x, TreapNode *y) {
    if (x == null || y == null)
      return max(x, y);
    if (x->priority >= y->priority) {
      x->son[1] = merge(x->son[1], y), x->pushup();
      return x;
    } else {
      y->son[0] = merge(x, y->son[0]), y->pushup();
      return y;
    }
  }

  inline void insert(T num) {
    TreapNode **r = &root, *const New = allocate(num);
    for (; *r != null && New->priority <= (*r)->priority; r = &(*r)->son[num >= (*r)->val])
      (*r)->size++;
    split(*r, num, New->son[0], New->son[1]);
    (*r = New)->pushup();
  }
  inline void erase(T num) {
    TreapNode **r = &root;
    for (; *r != null && (*r)->val != num; r = &(*r)->son[num > (*r)->val])
      (*r)->size--;
    *r = merge((*r)->son[0], (*r)->son[1]);
  }

  //键值<=num的节点数量
  inline int rank_lower(T num) {
    TreapNode *r = root;
    int res      = 0;
    for (; r != null; r = r->son[num >= r->val])
      res += (num >= r->val) * (r->son[0]->size + 1);
    return res;
  }
  //键值<num的节点数量
  inline int rank_less(T num) {
    TreapNode *r = root;
    int res      = 0;
    for (; r != null; r = r->son[num > r->val])
      res += (num > r->val) * (r->son[0]->size + 1);
    return res;
  }
  inline TreapNode *kth(int k) {
    TreapNode *r = root;
    while (1) {
      if (r->son[0] != null && k <= r->son[0]->size)
        r = r->son[0];
      else if (r->son[1] != null && k > r->son[0]->size + 1)
        k -= r->son[0]->size + 1, r = r->son[1];
      else
        return r;
    }
  }
} Treap;

Part of main():

C++
    switch (op) {
      case 1:
        Treap.insert(x);
        break;
      case 2:
        Treap.erase(x);
        break;
      case 3:
        ans ^= (last = Treap.rank_less(x) + 1);
        break;
      case 4:
        ans ^= (last = Treap.kth(x)->val);
        break;
      case 5:
        ans ^= (last = Treap.kth(Treap.rank_less(x))->val);
        break;
      case 6:
        ans ^= (last = Treap.kth(Treap.rank_lower(x) + 1)->val);
        break;
    }