Note -「模板」FHQ-Treap
2021/9/1 23:10:35
本文主要是介绍Note -「模板」FHQ-Treap,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
// Fhq-Treap const int MAXN = 1e5 + 5; struct Fhq_Treap { struct Fhq_Node { int l, r, val, key, size; #define lson tr[p].l #define rson tr[p].r Fhq_Node() {} Fhq_Node(int L, int R, int Val, int Key, int Size) { l = L; r = R; val = Val; key = Key; size = Size; } } tr[MAXN]; int tot = 0, root = 0; int Get(int val) { tr[++tot] = Fhq_Treap(0, 0, val, rand(), 1); return tot; } void Push_Up(int p) { tr[p].size = tr[lson].size + tr[rson].size + 1; } void Split(int p, int val, int &x, int &y) { if (!p) { x = 0, y = 0; return; } if (tr[p].val <= val) { x = p; Split(rson, val, rson, y); } else { y = p; Split(lson, val, x, lson); } Push_Up(p); } int Merge(int x, int y) { if (!x || !y) return x + y; if (tr[x].key <= tr[y].key) { tr[x].r = Merge(tr[x].r, y); Push_Up(x); return x; } else { tr[y].l = Merge(x, tr[y].l); Push_Up(y); return y; } } void Insert(int val) { int x, y; Split(root, val, x, y); root = Merge(Merge(x, Get(val)), y); } void Delete(int val) { int x, y, z; Split(root, val, x, z); Split(x, val - 1, x, y); y = Merge(tr[y].l, tr[y].r); root = Merge(Merge(x, y), z); } int Get_Rank(int val) { int x, y, ret; Split(root, val - 1, x, y); ret = tr[x].size + 1; root = Merge(x, y); return ret; } int Get_Val(int Rank) { int p = root; while (p) { if (tr[lson].size + 1 == Rank) return tr[p].val; else if (Rank <= tr[lson].size) p = lson; else { Rank -= (tr[lson].size + 1); p = rson; } } return 0; } int Get_Pre(int val) { int x, y, p; Split(root, val - 1, x, y); p = x; while (rson) p = rson; root = Merge(x, y); return tr[p].val; } int Get_Next(int val) { int x, y, p; Split(root, val, x, y); p = y; while (lson) p = lson; root = Merge(x, y); return tr[p].val; } } tree;
这篇关于Note -「模板」FHQ-Treap的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南