跳到正文

题解

题解:P8524 [Ynoi2078]

haokee约 12 分钟
原文
文章信息与版本

文章信息

发布
2026年8月28日
收录
2026年8月28日
更新
2026年11月11日
篇幅
约 12 分钟
题目
P8524

版本

  1. v1当前

提供一种题解区没有的做法,随机根 FHQ Treap + 引用计数。


首先,使用可持久化平衡树可以轻松地实现区间复制和区间和查询的操作,但是操作二显然不能简单改标记实现。

随机根 FHQ Treap 教学

朴素维护堆的 FHQ Treap 由于复制后堆内值没有改变,可能导致被构造数据卡掉。随机根 FHQ Treap 就是完全不记录堆值(甚至还削减了一个 int 用于卡空间),按照节点大小的比例随机分配。

具体地,在合并 pp 和 qq 时,以 siz(p)siz(p)+siz(q)\cfrac{\text{siz}(p)}{\text{siz}(p)+\text{siz}(q)} 的概率让 pp 在 qq 上面,siz(q)siz(p)+siz(q)\cfrac{\text{siz}(q)}{\text{siz}(p)+\text{siz}(q)} 的概率 qq 在 pp 上面。

最常用的势能均摊方法在本题无效,因为操作一会重置势能导致复杂度退化。

考虑每个节点开个 3030 大小的数组,维护管辖区间进行 kk 次除 22 操作的和,那么区间修改只需要对该数组进行偏移即可。当然,得使用懒标记。

每个节点多 3030 个 long long 显然也是无法承受的,考虑在二进制上优化。我们将该数组的定义改写为“区间内第 kk 位为 11 的元素数量”,那么只需要开 3030 个 int,就能计算 ∑k=0292kcnt(k)\sum\limits_{k=0}^{29}2^k \text{cnt}(k) 得到答案。

为了继续卡空间,可以不维护当前节点的值,只维护区间内 cnt\text{cnt}。下放懒标记后从 cnt\text{cnt} 减掉左右儿子的(即只留自己的),Push Up 的时候再加回来。另外懒标记的类型可用 uint8_t 继续优化。

这样下来,单个节点内有 30+330+3 个 int,一个 uint_8,总共 133133 字节,因此节点数量可以开到 3.5×1063.5\times 10^6 左右。

显然原始节点数量就有 3×1063\times 10^6 了,每次多 O(log⁡n)\mathcal O(\log n) 个节点很快就会爆掉。常用的优化方法是阈值重构,节点数到了某一阈值就全量重构一下,不过实测重构次数过多导致 TLE,需要额外优化。

此时就可以搬出我们优雅的引用计数了。对于每个节点,额外维护 ref\text{ref} 字段,表示当前有多少个有效引用持有该物理节点。父节点指向儿子的边算一个引用,当前根或操作过程中产生的临时根同样算一个引用。

对于一个节点,ref\text{ref} 有三种情况

  • ref=0\text{ref}=0,该节点不存在有效引用,可直接回收;
  • ref=1\text{ref}=1,该节点没有被共享,持有这份唯一引用的一方可以原地修改;
  • ref>1\text{ref}>1,节点存在共享,修改节点信息前必须拷贝一个干净的、ref=1\text{ref}=1 的新节点出来,再对新节点修改。

每次查询过后,当前的临时版本以后都不会再用,因此将本次操作临时分离出来的所有节点的 ref\text{ref} 减一。对于变为 00 的节点就可以直接回收进节点池,因为以后都不会再访问了。

注意主函数内需要小心地操作引用计数,万不可释放仍须使用的节点或少释放了本应释放的节点,否则复杂度会退化。

期望空间复杂度为 n+O(log⁡n)n+\mathcal O(\log n),时间复杂度 O((n+mlog⁡n)log⁡V)\mathcal O((n+m\log n)\log V)。

AC 代码如下,常数较大,最慢点 37s。

约定 split 和 merge 会消耗传入节点的那份引用,并将其所有权转移到返回结果中。因此已经作为 split/merge 参数使用的引用不能再次释放;只有不再参与新版本构造的根才需要释放。

C++
#include <iostream>
#include <cstdint>
#include <cstring>
#include <random>
#include <vector>

using namespace std;

constexpr int kMaxN = 3.2e6;

// 取消结构体对齐,可省空间
#pragma pack(1)
struct {
  int lc, rc, siz, ref, cnt[30];
  uint8_t tag;
} tr[kMaxN];
#pragma pack()

vector<int> pool;               // 节点回收池
mt19937 rd(random_device{}());  // 随机数引擎
int a[kMaxN], n, m, rt, cnt;

// 为 p 添加一次引用
int retain(int p) {
  if (p) {
    tr[p].ref++;
  }
  return p;
}

// 为 p 减少一次引用
void release(int p) {
  if (!p) {
    return;
  }
  if (--tr[p].ref) {
    return;
  }
  pool.push_back(p);
  release(tr[p].lc);
  release(tr[p].rc);
}

// 分配一个新节点
int alloc() {
  if (pool.empty()) {
    return ++cnt;
  }
  int _ = pool.back();
  pool.pop_back();
  return _;
}

// 拷贝一个节点(对于 ref>1)
int clone(int p) {
  int q = alloc();
  tr[q] = tr[p];
  tr[q].ref = 1;
  retain(tr[q].lc);
  retain(tr[q].rc);
  return q;
}

// p 只读(不可改)当且仅当引用数量超过一
#define readonly(p) (tr[p].ref > 1)

// 使 p 变为可写入节点
void makeWritable(int &p) {
  if (!readonly(p))  {
    return;
  }
  int q = clone(p);
  release(p);
  p = q;
}

// 区间内 cnt 整体向右偏移 d 位并打标记
void addTag(int p, uint8_t d) {
  if (d >= 30) {
    memset(tr[p].cnt, 0, sizeof(tr[p].cnt));
    tr[p].tag = 30;
    return;
  }
  for (int i = 0; i < 30; i++) {
    if (i + d < 30) {
      tr[p].cnt[i] = tr[p].cnt[i + d];
    } else {
      tr[p].cnt[i] = 0;
    }
  }
  tr[p].tag = min(30, (int)tr[p].tag + d);
}

// 下传标记并扣除儿子贡献
// 后面须 pushUp 加回来
void pushDown(int p) {
  if (tr[p].tag) {
    if (tr[p].lc) {
      makeWritable(tr[p].lc);
      addTag(tr[p].lc, tr[p].tag);
    }
    if (tr[p].rc) {
      makeWritable(tr[p].rc);
      addTag(tr[p].rc, tr[p].tag);
    }
    tr[p].tag = 0;
  }
  for (int i = 0; i < 30; i++) {
    tr[p].cnt[i] -= tr[tr[p].lc].cnt[i] + tr[tr[p].rc].cnt[i];
  }
}

void pushUp(int p) {
  tr[p].siz = tr[tr[p].lc].siz + tr[tr[p].rc].siz + 1;
  for (int i = 0; i < 30; i++) {
    tr[p].cnt[i] += tr[tr[p].lc].cnt[i] + tr[tr[p].rc].cnt[i];
  }
}

// 创建值为 val 的单点
int create(int val) {
  int p = alloc();
  tr[p].lc = tr[p].rc = tr[p].tag = 0;
  tr[p].ref = tr[p].siz = 1;
  for (int i = 0; i < 30; i++) {
    tr[p].cnt[i] = val >> i & 1;
  }
  return p;
}

// 分裂 p 前 k 个元素到 x,其余到 y
void split(int p, int &x, int &y, int k) {
  if (!p) {
    x = y = 0;
    return;
  }
  makeWritable(p);
  pushDown(p);
  if (k <= tr[tr[p].lc].siz) {
    y = p;
    split(tr[p].lc, x, tr[p].lc, k);
  } else {
    x = p;
    split(tr[p].rc, tr[p].rc, y, k - tr[tr[p].lc].siz - 1);
  }
  pushUp(p);
}

// 随机化合并
int merge(int p, int q) {
  if (!p || !q) {
    return p + q;
  }
  // siz(p) / (siz(p) + siz(q)) 的概率 p 在上
  if (uniform_int_distribution<int>(1, tr[p].siz + tr[q].siz)(rd) <= tr[p].siz) {
    makeWritable(p);
    pushDown(p);
    tr[p].rc = merge(tr[p].rc, q);
    pushUp(p);
    return p;
  }
  makeWritable(q);
  pushDown(q);
  tr[q].lc = merge(p, tr[q].lc);
  pushUp(q);
  return q;
}

long long calc(int cnt[30]) {
  long long res = 0;
  for (int i = 0; i < 30; i++) {
    res += (1LL << i) * cnt[i];
  }
  return res;
}

int build(int l, int r) {
  if (l > r) {
    return 0;
  }
  int m = (l + r) / 2, p = create(a[m]);
  tr[p].lc = build(l, m - 1);
  tr[p].rc = build(m + 1, r);
  pushUp(p);
  return p;
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  pool.reserve(kMaxN - 1);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  rt = build(1, n);
  cin >> m;
  for (int o, l, r, x, src_a, src_bc, src_b, src_c, a, bc, old_b, b, c; m; m--) {
    cin >> o >> l >> r;
    if (o == 1) { 
      cin >> x;
      int src_rt = retain(rt);                 // 新建一个引用用来持有复制源
      split(src_rt, src_a, src_bc, l - 1);
      split(src_bc, src_b, src_c, r - l + 1);  // 此时 b 为复制源
      release(src_a), release(src_c);          // 这俩没用,可释放
      split(rt, a, bc, x - 1);
      split(bc, old_b, c, r - l + 1);          // old_b 为粘贴对象
      release(old_b);                          // 被覆盖就没用了,释放
      rt = merge(a, merge(src_b, c));
    } else if (o == 2) {
      split(rt, a, bc, l - 1);
      split(bc, b, c, r - l + 1);
      makeWritable(b);                         // 注意 b 要在可写状态下 addTag
      addTag(b, 1);
      rt = merge(a, merge(b, c));
    } else if (o == 3) {
      split(retain(rt), a, bc, l - 1);         // 加个引用用于拆分,这样就不用 merge 回去
      split(bc, b, c, r - l + 1);
      cout << calc(tr[b].cnt) << '\n';
      release(a);                              // 注意用于拆分出的节点以后没用了,需要释放
      release(b);
      release(c);
    }
  }
  return 0;
}
原文
存档评论2 条抓取于