版本比较
浅谈 vEBT
正文
增加 332 行删除 1 行
16 行未改动17181920212122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135216 行未改动
如何查询遍历?我们每块维护一个双向链表,用来把所有非空块串起来,这样查询时不用在每一层扫描值域,直接顺着链表走。由于链表是 $O(1)$ ,并不会带来额外的维护负担。
所以我们做到了 $O(\log \log V) - O(n)$ 解决此问题。 据说这个东西叫 vEB tree ,太有缘分了!据说这个东西叫 vEB tree ,太有缘分了!
:::info[Code:仅供参考]```cpp#include <bits/stdc++.h>using namespace std;
// 值域 [0, 2^(2^MAX_LEVEL))// MAX_LEVEL = 5 覆盖 [0, 2^32)// 若需要更大值域,可调到 6(覆盖 [0, 2^64),但需用 unsigned long long)const int MAX_LEVEL = 5;
// ==================== vEBT 本体 ====================
struct VEBNode { int level = 0; bool empty = true; long long mn = 0, mx = 0;
int summary = -1; // 子节点下标 unordered_map<long long, int> blocks; // 块号 h -> 子节点下标
// 有序链表:把所有非空块按 h 从小到大串起来 int head = -1, tail = -1; unordered_map<long long, int> linkIdx; // h -> 链表节点下标};
struct LinkNode { long long h; int prev, next;};
deque<VEBNode> vebPool;deque<LinkNode> linkPool;
int newNode(int level) { vebPool.push_back(VEBNode()); int id = (int)vebPool.size() - 1; vebPool[id].level = level; if (level > 0) { vebPool[id].summary = newNode(level - 1); } return id;}
int newLink(long long h) { linkPool.push_back({h, -1, -1}); return (int)linkPool.size() - 1;}
long long bsize(int id) { return 1LL << (1LL << (vebPool[id].level - 1));}
// 严格小于 x 的最大值,不存在返回 -1long long findPred(int id, long long x) { VEBNode& u = vebPool[id]; if (u.empty || x <= u.mn) return -1; if (x > u.mx) return u.mx; if (u.level == 0) return u.mn;
long long B = bsize(id); long long h = x / B, l = x % B;
auto it = u.blocks.find(h); if (it != u.blocks.end()) { long long pl = findPred(it->second, l); if (pl != -1) return h * B + pl; }
long long ph = findPred(u.summary, h); if (ph == -1) return -1; return ph * B + vebPool[u.blocks.at(ph)].mx;}
// 严格大于 x 的最小值,不存在返回 -1long long findSucc(int id, long long x) { VEBNode& u = vebPool[id]; if (u.empty || x >= u.mx) return -1; if (x < u.mn) return u.mn; if (u.level == 0) return u.mx;
long long B = bsize(id); long long h = x / B, l = x % B;
auto it = u.blocks.find(h); if (it != u.blocks.end()) { long long sl = findSucc(it->second, l); if (sl != -1) return h * B + sl; }
long long sh = findSucc(u.summary, h); if (sh == -1) return -1; return sh * B + vebPool[u.blocks.at(sh)].mn;}
// 把块号 h 插入链表(要求 h 已在 summary 中)void listInsert(int id, long long h) { VEBNode& u = vebPool[id]; int node = newLink(h); u.linkIdx[h] = node;
if (u.head == -1) { u.head = u.tail = node; return; }
long long pred = findPred(u.summary, h); long long succ = findSucc(u.summary, h);
if (pred == -1) { linkPool[node].next = u.head; linkPool[u.head].prev = node; u.head = node; } else if (succ == -1) { linkPool[node].prev = u.tail; linkPool[u.tail].next = node; u.tail = node; } else { int p = u.linkIdx[pred]; int s = u.linkIdx[succ]; linkPool[node].prev = p; linkPool[node].next = s; linkPool[p].next = node; linkPool[s].prev = node; }}
void listErase(int id, long long h) { VEBNode& u = vebPool[id]; int node = u.linkIdx[h]; int p = linkPool[node].prev; int s = linkPool[node].next;
if (p != -1) linkPool[p].next = s; else u.head = s; if (s != -1) linkPool[s].prev = p; else u.tail = p;
u.linkIdx.erase(h);}
void insert(int id, long long x) { VEBNode& u = vebPool[id];
if (u.empty) { u.mn = u.mx = x; u.empty = false; return; }
if (x < u.mn) swap(x, u.mn); if (x > u.mx) swap(x, u.mx); if (x == u.mn || x == u.mx) return; if (u.level == 0) return;
long long B = bsize(id); long long h = x / B, l = x % B;
if (!u.blocks.count(h)) { int child = newNode(u.level - 1); u.blocks[h] = child; insert(u.summary, h); listInsert(id, h); }
insert(u.blocks[h], l);}
void erase(int id, long long x) { VEBNode& u = vebPool[id]; if (u.empty || x < u.mn || x > u.mx) return;
if (u.mn == u.mx) { u.empty = true; return; }
// 删除 min if (x == u.mn) { if (u.level == 0 || u.summary == -1 || vebPool[u.summary].empty) { u.mn = u.mx; return; } long long B = bsize(id); long long h = vebPool[u.summary].mn; long long l = vebPool[u.blocks.at(h)].mn; u.mn = h * B + l; erase(u.blocks[h], l); if (vebPool[u.blocks[h]].empty) { erase(u.summary, h); listErase(id, h); u.blocks.erase(h); } return; }
// 删除 max if (x == u.mx) { if (u.level == 0 || u.summary == -1 || vebPool[u.summary].empty) { u.mx = u.mn; return; } long long B = bsize(id); long long h = vebPool[u.summary].mx; long long l = vebPool[u.blocks.at(h)].mx; u.mx = h * B + l; erase(u.blocks[h], l); if (vebPool[u.blocks[h]].empty) { erase(u.summary, h); listErase(id, h); u.blocks.erase(h); } return; }
// 普通删除 long long B = bsize(id); long long h = x / B, l = x % B;
auto it = u.blocks.find(h); if (it == u.blocks.end()) return;
erase(it->second, l); if (vebPool[it->second].empty) { erase(u.summary, h); listErase(id, h); u.blocks.erase(h); }}
// 按从小到大顺序输出所有唯一值void traverse(int id, long long base, vector<long long>& out) { VEBNode& u = vebPool[id]; if (u.empty) return;
out.push_back(base + u.mn); if (u.mn == u.mx) return;
if (u.level == 0) { out.push_back(base + u.mx); return; }
long long B = bsize(id); for (int cur = u.head; cur != -1; cur = linkPool[cur].next) { long long h = linkPool[cur].h; traverse(u.blocks.at(h), base + h * B, out); }
out.push_back(base + u.mx);}
// ==================== 序列维护 ====================
struct SequenceVEB { int root; vector<long long> a; unordered_map<long long, int> cnt; // 值 -> 出现次数
SequenceVEB(const vector<long long>& init) { a = init; root = newNode(MAX_LEVEL); for (long long v : a) { if (cnt[v]++ == 0) insert(root, v); } }
// 单点修改:a[pos] 改成 new_val void update(int pos, long long new_val) { long long old = a[pos]; if (old == new_val) return;
if (--cnt[old] == 0) erase(root, old); if (cnt[new_val]++ == 0) insert(root, new_val);
a[pos] = new_val; }
// 查询排序后的结果 vector<long long> querySorted() const { vector<long long> vals; traverse(root, 0, vals);
vector<long long> res; res.reserve(a.size()); for (long long v : vals) { auto it = cnt.find(v); for (int k = 0; k < it->second; k++) { res.push_back(v); } } return res; }};
// ==================== 测试 ====================
int main() { // 初始序列 vector<long long> init = {5, 3, 7, 1, 9, 2, 8, 5, 3, 1};
SequenceVEB seq(init);
cout << "初始排序结果: "; for (long long v : seq.querySorted()) cout << v << " "; cout << "\n"; // 期望: 1 1 2 3 3 5 5 7 8 9
// 单点修改 seq.update(0, 100); // 把第一个 5 改成 100 seq.update(1, 1); // 把 3 改成 1
cout << "修改后排序结果: "; for (long long v : seq.querySorted()) cout << v << " "; cout << "\n"; // 期望: 1 1 1 2 3 5 7 8 9 100
// 再改回去 seq.update(0, 5); seq.update(1, 3);
cout << "还原后排序结果: "; for (long long v : seq.querySorted()) cout << v << " "; cout << "\n"; // 期望: 1 1 2 3 3 5 5 7 8 9
return 0;}```
:::