糖人糖码

· · 个人记录

P6136 【模板】普通平衡树(数据加强版)。

#include <bits/stdc++.h>
using namespace std;
constexpr int S = 1 << 25;
char buf[S], *p1, *p2;
inline int nc() {
  if (p1 == p2) {
    p2 = (p1 = buf) + cin.read(buf, S).gcount();
    if (p1 == p2) return EOF;
  }
  return *p1++;
}
inline int rd() {
  char ch;
  while (!isdigit(ch = nc()))
    ;
  int x = ch & 0xf;
  while (isdigit(ch = nc())) x = x * 10 + (ch & 0xf);
  return x;
}
constexpr int N = 1e6 + 1e5 + 9;
namespace BIT::EXPORT {
void update(int, int);
int query(int);
int kth(int);
}  // namespace BIT::EXPORT
using namespace BIT::EXPORT;
inline void ins(int x) { update(x, 1); }
inline void del(int x) { update(x, -1); }
inline int rk(int x) { return query(x - 1) + 1; }
inline int get(int x) { return kth(x); }
inline int pre(int x) { return get(rk(x) - 1); }
inline int nxt(int x) { return get(rk(x + 1)); }
signed main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int n = rd(), q = rd(), lstans = 0, ret = 0;
  while (n--) ins(rd() + 1);
  while (q--) {
    int op = rd(), x = rd() ^ lstans;
    if (op == 1)
      ins(x + 1);
    else if (op == 2)
      del(x + 1);
    else if (op == 3)
      lstans = rk(x + 1);
    else if (op == 4)
      lstans = get(x) - 1;
    else if (op == 5)
      lstans = pre(x + 1) - 1;
    else if (op == 6)
      lstans = nxt(x + 1) - 1;
    if (op >= 3) ret ^= lstans;
  }
  return cout << ret << endl, 0;
}

namespace BIT {
constexpr int S = 1 << 30;
struct {
  int sm, rb, l, r;
} tr[N << 1];
int rt, tot;
inline int lca(int x, int y) {
  if (x > y) swap(x, y);
  if (!x) return y;
  while (x < y) x += x & -x;
  return x;
}
inline int lb(int x) { return tr[x].rb & (tr[x].rb - 1); }
inline void newnode(int& x, int sn, int p) {
  tr[x = ++tot] = {tr[sn].sm, lca(p, tr[sn].rb), sn, exchange(tr[sn].r, 0)};
}
inline void update(int& rt, int p, int v) {
  if (!rt) newnode(rt, 0, S);
  for (int x = rt, r, h, *z, *q; tr[x].sm += v, p < (r = tr[x].rb); *z = x) {
    r -= h = 1 << __lg(r - p), z = &tr[x].l;
    if (!*z || lb(*z) >= p) {
      if (*z && tr[*z].rb <= r)
        newnode(x, *z, p);
      else
        tr[x = ++tot] = {0, p, 0, *z};
      continue;
    } else if (*z && tr[*z].rb >= p) {
      x = *z;
      continue;
    }
    while (*(q = &tr[*z].r) && tr[*q].rb < p) z = q;
    if (*q && lb(*q) < p)
      x = *q, z = q;
    else if (*q && tr[*q].rb <= r)
      newnode(x, *(z = q), p);
    else if (*z && lb(*z) >= r - h)
      newnode(x, *z, p);
    else
      newnode(x, 0, p), tr[x].r = *(z = q);
  }
}
inline int query(int x, int p) {
  int sum = 0;
  while (x && lb(x) < p)
    for (x = tr[x].l; x && tr[x].rb <= p; x = tr[x].r) sum += tr[x].sm;
  return sum;
}
inline int kth(int x, int k) {
  int fa = 0;
  while (x) x = tr[x].sm < k ? (k -= tr[x].sm, tr[x].r) : tr[fa = x].l;
  return tr[fa].rb;
}
namespace EXPORT {
void update(int p, int v) { BIT::update(rt, p, v); }
int query(int p) { return BIT::query(rt, p); }
int kth(int k) { return BIT::kth(rt, k); }
}  // namespace EXPORT
}  // namespace BIT

这种工业垃圾到底是谁在写,哦原来是我啊,哈哈那没事了。

晚点补注释。来了。

不难注意到 BIT 的结构是二项树,而二项树双射半树,所以我们考虑使用半树来维护 BIT 的结构。这个东西没啥用,但是写起来很克。对比直接的树状数组优势是可以动态开点,但是动态开点可以写线段树;对比线段树的优势是理论空间小,但是这条只能在序列上成立,此时不如直接树状数组。完美的逻辑闭环×

然后更加克的是这东西居然也能和线段树一样在动态开点的情况下优化到线性空间。我们这里还是用二项树来说理(虽然实现是半树)。具体而言我们维护每个叶子节点构成的虚树,然后不难发现一次单点加至多新增两个节点,于是就可以了。

(实际上 ut 意识到 BIT 可以用半树写和意识到那个基于斜二进制的数据结构是同一天……只不过这玩意实在是太没用了所以一直在咕。)

代码就是上面那团丑陋的东西。但是比压缩 Trie 快,这是好的!

以上。