跳到正文

个人记录

位压缩线段树。

UnyieldingTrilobite约 6 分钟
原文
文章信息与版本

文章信息

发布
2024年10月18日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 6 分钟
获赞
3

版本

仅有当前版本

众所周知 01 Trie 可以压缩,那么完全等价的值域线段树也必然能。

照例维护虚树即可。例题依然是 【模板】普通平衡树(数据加强版)。

C++
#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 SGT::EXPORT {
void update(int, int);
int query(int);
int kth(int);
}  // namespace SGT::EXPORT
using namespace SGT::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());
  while (q--) {
    int op = rd(), x = rd() ^ lstans;
    if (op == 1)
      ins(x);
    else if (op == 2)
      del(x);
    else if (op == 3)
      lstans = rk(x);
    else if (op == 4)
      lstans = get(x);
    else if (op == 5)
      lstans = pre(x);
    else if (op == 6)
      lstans = nxt(x);
    if (op >= 3) ret ^= lstans;
  }
  return cout << ret << endl, 0;
}

namespace SGT {
constexpr int S = 1 << 30;
struct {
  int sm, lb, rb, l, r;
} tr[N << 1];
int rt, tot;
inline void update(int& rt, int p, int v) {
  if (!rt) tr[rt = ++tot] = {0, 0, S - 1, 0, 0};
  int x = rt, L, R, mid;
  while (tr[x].sm += v, (L = tr[x].lb) != (R = tr[x].rb)) {
    auto [l, r, y] = p <= (mid = (L + R) >> 1) ? tuple(L, mid, &tr[x].l)
                                               : tuple(mid + 1, R, &tr[x].r);
    if (!*y)
      tr[*y = ++tot] = {0, p, p, 0, 0};
    else if (int yl = tr[*y].lb, yr = tr[*y].rb; yl > p || yr < p) {
      yl = min(yl, p), yr = max(yr, p);
      while (true) {
        int mid = (l + r) >> 1;
        if (yl > mid)
          l = mid + 1;
        else if (yr <= mid)
          r = mid;
        else {
          tr[++tot] = {tr[*y].sm, l, r, 0, 0};
          (p > mid ? tr[tot].l : tr[tot].r) = exchange(*y, tot);
          break;
        }
      }
    }
    x = *y;
  }
}
inline int query(int x, int p) {
  int sum = 0;
  while (x)
    if (p >= tr[x].rb)
      sum += tr[x].sm, x = 0;
    else if (int l = tr[x].l; tr[l].rb <= p)
      sum += tr[l].sm, x = tr[x].r;
    else
      x = l;
  return sum;
}
inline int kth(int x, int k) {
  while (tr[x].lb != tr[x].rb)
    if (int l = tr[x].l, s = tr[l].sm; s < k)
      k -= s, x = tr[x].r;
    else
      x = l;
  return tr[x].lb;
}
namespace EXPORT {
void update(int p, int v) { SGT::update(rt, p, v); }
int query(int p) { return SGT::query(rt, p); }
int kth(int k) { return SGT::kth(rt, k); }
}  // namespace EXPORT
}  // namespace SGT
原文