版本比较
stl ❌ 手搓 ✅ 1
标题
增加 1 行删除 1 行
11stl ❌ 手搓 ✅stl ❌ 手搓 ✅ 1正文
增加 246 行删除 23 行
123456789101112131415166 行未改动232425261718192021222324272829302931323334353637383940313233343536373839404142434413 行未改动585960616263646562636465251 行未改动全部展开317318319320321322323324325326110 行未改动全部展开437438439440441442443444445446336 行未改动全部展开7837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009文章过于垃圾,大家当个闲比的乱搞看就行。
# 更新日志
2026/3/15:开始写 ```Sorted``` 并出了一个题目,以测试速度。
2026/3/19:开启项目。
2026/3/21:写了几个普通的数据结构,于是爆掉了 ```std::unordered_map``` 并更新了 ```Unordered_map``` 然后尝试替换了几题 CF 上的卡掉我原先的 ```std::unordered_map``` 的代码,并通过,找到了一些 bug 并增加了提示。
2026/3/22:开始写 ```priority_queue``` 了。
# 前言
我发现有一些的 ```stl``` 都饱受大家的诟病?
6 行未改动写的过程,大概就是使用 AI 进行速度评估,让其先实现一个初版,若速度差异不大甚至已经超越 ```stl``` 的实现,故开始手搓环节,即进行卡常。
但是大部分的 ```stl``` 中的实现都是极其优秀的,例如 ```vector``` 就是一个很典型的例子。
对比一下两个提交。
[手写 vector](https://www.luogu.com.cn/record/268764637)
[std::vector](https://www.luogu.com.cn/record/268764711)
差距极大,然后我就放弃了手写 ```vector``` 改用自带的了。
# 一些没有实现的原因
#### vector
```vector``` 实现太优秀了。根本跑不过,写了一个平衡树慢 $3$ 倍。| 名称 | 原因 | 备注 ||:-:|:-:|:-:|| ```vector``` | 发现只有 ```push_back``` 快。 | 准备写一个 ```vector``` 专门存图用。 || ```map``` | 实现了 fhq-treap,分块,红黑树均慢 $\frac{1}{20}$,但没卡过。 | - || ```set``` | 卡不过。 | - || ```queue``` | 建议大家手写定长数组,动态扩容不一定快。 | - || ```stack``` | 建议大家手写定长数组,动态扩容不一定快。 | - || ```deque``` | 建议大家手写定长数组,动态扩容不一定快。 | - || ```bitset``` | 没有卡过,因为 ```stl``` 应该有什么汇编常数非常牛逼 | 不想使用指令集等 |
#### map
```map``` 高度工程化的红黑树,跑不过,根本跑过,虽然只差 $\frac{1}{20}$ 左右,但是建议大家还是直接用 ```map``` 吧。
已经尝试了的算法有 fhq-treap 和红黑树还有分块但都跑不过。
#### set
```set``` 这个真的跑不过啊,给我卡力竭了。
# Sorted::Sort
碾压程度:$6.5$ 分。
13 行未改动| 1968年 | 双调排序 | 肯·巴彻 |
可以发现一个很有意思的事情,第一个发明的是基数排序。
#### 一些关于排序的发现
归并排序在区间长度 $\le \sqrt{n}$ 的时候跑 ```std::sort``` 可以做到 $n \log{\sqrt{n}}=\frac{1}{2}n \log n$ 但复杂度上,没有改变,而且由于常数问题,还是跑不过 ```std::sort``` 但是着从侧面肯定了 ```stl``` 实现的优秀。
### 介绍
实现了大部分排序,对基数排序和最终的可以使用全部情况,且速度在对整形排序时与 ```std::sort``` 在不同情况下各有优劣,大家在卡常的时候可以尝试。
251 行未改动全部展开}```:::
在使用时如果报错了,可能需要删除 `sort` 前面的 `static` 哦。
如果要使用最终版排序直接 ```Sorted::Sort``` 即可,其他的需要打 ```Sorted::radix_sort::sort``` 的方式。
### 测速
110 行未改动全部展开> 说明:复杂度按哈希表常规分析给出。> - 平均:在负载因子受控、哈希分布正常时> - 最坏:极端冲突下可能退化到 $O(n)$
**空间常数较大。**
| 函数 | 功能 | 平均复杂度 | 最坏复杂度 | 备注 ||---|---|---|---|---|| `Unordered_map()` | 默认构造,初始化最小容量 | $O(1)$ | $O(1)$ | 实际会分配初始桶数组 || `explicit Unordered_map(size_t reserve_n)` | 按预估元素数构造 | $O(m)$ | $O(m)$ | `m` 为初始桶数(初始化成本) |336 行未改动全部展开
[std::unordered_map](https://www.luogu.com.cn/record/268767092)
[Unordered_map](https://www.luogu.com.cn/record/268769670)
#### P3345 [ZJOI2015] 幻想乡战略游戏
[std::unordered_map](https://www.luogu.com.cn/record/202637581)
[Unordered_map](https://www.luogu.com.cn/record/269099834)
#### P4149 [IOI 2011] Race
[std::unordered_map](https://www.luogu.com.cn/record/199556024)
[Unordered_map](https://www.luogu.com.cn/record/269100095)
可以发现均比 ```stl``` 快。
# Priority_queue
碾压程度:$3.5$ 分。
### 介绍
**支持顶点删除某一个数字。**
实现的是二叉堆,没有选择其他堆的原因是,我不会,啊,不是,因为其他的堆的常数有点大。
| 函数 | 功能 | 时间复杂度 | 备注 ||---|---|---|---|| `sift_up(i)` | 将下标 `i` 元素上浮,恢复堆性质 | $\mathcal{O}(\log n)$ | 内部函数;常用于插入后调整 || `sift_down(i)` | 将下标 `i` 元素下沉,恢复堆性质 | $\mathcal{O}(\log n)$ | 内部函数;常用于删除后调整 || `build_heap()` | 将当前容器整体建堆 | $\mathcal{O}(n)$ | Floyd 建堆法 || `Priority_queue()` | 默认构造空堆 | $\mathcal{O}(1)$ | 默认比较器 || `Priority_queue(const Compare& compare)` | 指定比较器构造空堆 | $\mathcal{O}(1)$ | || `Priority_queue(const Compare& compare, const Container& cont)` | 拷贝容器并建堆 | $\mathcal{O}(n)$ | 拷贝 $\mathcal{O}(n)$ + 建堆 $\mathcal{O}(n)$ || `Priority_queue(const Compare& compare, Container&& cont)` | 移动容器并建堆 | $\mathcal{O}(n)$ | 建堆主导复杂度 || `Priority_queue(const Container& cont)` | 默认比较器,拷贝容器并建堆 | $\mathcal{O}(n)$ | || `Priority_queue(Container&& cont)` | 默认比较器,移动容器并建堆 | $\mathcal{O}(n)$ | || `Priority_queue(It first, It last, const Compare& compare = Compare())` | 区间构造并建堆 | $\mathcal{O}(n)$ | 区间长度记为 $n$ || `empty()` | 判断是否为空 | $\mathcal{O}(1)$ | || `size()` | 返回元素个数 | $\mathcal{O}(1)$ | || `top()` | 访问堆顶元素 | $\mathcal{O}(1)$ | 空堆时 `assert` 失败(Debug) || `push(const value_type& x)` | 拷贝插入元素 | 均摊 $\mathcal{O}(\log n)$ | 扩容时单次最坏可到 $\mathcal{O}(n)$ || `push(value_type&& x)` | 移动插入元素 | 均摊 $\mathcal{O}(\log n)$ | 同上 || `emplace(Args&&... args)` | 原地构造并插入 | 均摊 $\mathcal{O}(\log n)$ | 减少临时对象开销 || `pop()` | 删除堆顶元素 | $\mathcal{O}(\log n)$ | `size()==1` 时为 $\mathcal{O}(1)$ || `swap(Priority_queue& other)` | 交换两个堆 | 通常 $\mathcal{O}(1)$ | 取决于底层容器 `swap` || `reserve(size_type n)` | 预留容量 | 若扩容 $\mathcal{O}(n)$,否则 $\mathcal{O}(1)$ | 卡常常用 || `erase(const value_type& x)` | 删除一个值等于 `x` 的元素 | $\mathcal{O}(n)$ | 查找 $\mathcal{O}(n)$ + 调整 $\mathcal{O}(\log n)$,总为 $\mathcal{O}(n)$ || `swap(a, b)`(非成员) | 非成员交换接口 | 同成员 `swap` | 便于 `std::swap` 风格调用 |
### code
:::info[code]```cpp#include <vector>#include <functional>#include <utility>#include <cassert>template <class T, class Container = std::vector<T>, class Compare = std::less<T>>class Priority_queue {public: using value_type = T; using container_type = Container; using value_compare = Compare; using size_type = typename Container::size_type; using const_reference = const value_type&;private: Container c; Compare comp; inline void sift_up(size_type i) { value_type v = std::move(c[i]); while (i) { size_type p = (i - 1) >> 1; if (!comp(c[p], v)) break; c[i] = std::move(c[p]); i = p; } c[i] = std::move(v); } inline void sift_down(size_type i) { const size_type n = c.size(); value_type v = std::move(c[i]); while (true) { size_type l = (i << 1) + 1; if (l >= n) break; size_type r = l + 1; size_type best = (r < n && comp(c[l], c[r])) ? r : l; if (!comp(v, c[best])) break; c[i] = std::move(c[best]); i = best; } c[i] = std::move(v); } inline void build_heap() { if (c.empty()) return; for (size_type i = c.size() / 2; i-- > 0;) sift_down(i); }public: Priority_queue() = default; explicit Priority_queue(const Compare& compare) : c(), comp(compare) {} Priority_queue(const Compare& compare, const Container& cont) : c(cont), comp(compare) { build_heap(); } Priority_queue(const Compare& compare, Container&& cont) : c(std::move(cont)), comp(compare) { build_heap(); } explicit Priority_queue(const Container& cont) : c(cont), comp() { build_heap(); } explicit Priority_queue(Container&& cont) : c(std::move(cont)), comp() { build_heap(); } template <class It> Priority_queue(It first, It last, const Compare& compare = Compare()) : c(first, last), comp(compare) { build_heap(); } [[nodiscard]] inline bool empty() const noexcept { return c.empty(); } [[nodiscard]] inline size_type size() const noexcept { return c.size(); } inline const_reference top() const { assert(!c.empty()); return c.front(); } inline void push(const value_type& x) { c.push_back(x); sift_up(c.size() - 1); } inline void push(value_type&& x) { c.push_back(std::move(x)); sift_up(c.size() - 1); } template <class... Args> inline void emplace(Args&&... args) { c.emplace_back(std::forward<Args>(args)...); sift_up(c.size() - 1); } inline void pop() { assert(!c.empty()); if (c.size() == 1) { c.pop_back(); return; } c.front() = std::move(c.back()); c.pop_back(); sift_down(0); } inline void swap(Priority_queue& other) noexcept( noexcept(std::swap(c, other.c)) && noexcept(std::swap(comp, other.comp))) { using std::swap; swap(c, other.c); swap(comp, other.comp); } inline void reserve(size_type n) {c.reserve(n);} inline bool erase(const value_type& x) { for (size_type i = 0; i < c.size(); ++i) { if (c[i] == x) { const size_type last = c.size() - 1; if (i == last) { c.pop_back(); return true; } c[i] = std::move(c.back()); c.pop_back(); if (i > 0 && comp(c[(i - 1) >> 1], c[i])) sift_up(i); else sift_down(i); return true; } } return false; }};template <class T, class Container, class Compare>inline void swap(Priority_queue<T, Container, Compare>& a, Priority_queue<T, Container, Compare>& b) noexcept(noexcept(a.swap(b))) { a.swap(b);}```:::
### 测速
#### 模板题
[Priority_queue](https://www.luogu.com.cn/record/269092856)
[std::priority_queue](https://www.luogu.com.cn/record/269092900)
#### dijkstra
[Priority_queue](https://www.luogu.com.cn/record/269096718)
[std::priority_queue](https://www.luogu.com.cn/record/269096629)
#### Johnson
[Priority_queue](https://www.luogu.com.cn/record/269097706)
[std::priority_queue](https://www.luogu.com.cn/record/259176608)
#### P2056 [ZJOI2007] 捉迷藏
[Priority_queue](https://www.luogu.com.cn/record/269099448)
[std::priority_queue](https://www.luogu.com.cn/record/269098664)
#### P3350 [ZJOI2016] 旅行者
[Priority_queue](https://www.luogu.com.cn/record/269100814)
[std::priority_queue](https://www.luogu.com.cn/record/199735127)
可以发现均比 ```stl``` 快。