正文
增加 10 行删除 10 行
15 行未改动161718192021222324103 行未改动全部展开12812913013113213313413513656 行未改动19319419519619719819920020146 行未改动24824925025125225325425525625725825926026126226326421 行未改动2862872882892902912922932942 行未改动2972982993003013023033043053063073083093103113123133143153163173183193203214 行未改动15 行未改动堆实质上是一颗二叉树,二叉树的存储使用一维数组,编号为 $i$ 的结点的父结点编号为 $\left\lfloor\dfrac{i}{2}\right\rfloor$,左儿子编号为 $2 \times i$,右儿子编号为 $2 \times i +1$。因为性质 $1$,堆还能用于排序。#**手写堆:**手写堆虽然麻烦,但是效率比较高,尽管有替代品但是这是理解堆及二叉树思想的最好方法,**以下讲解为小根堆**。103 行未改动全部展开return heap[0];}```#**代码**:_此代码可过模板题_56 行未改动return 0;}```#**STL**STL 实现的堆有两种:46 行未改动`empty()` 查询堆是否为空,返回值为 `bool` 类型,堆为空时返回 `true`,否则返回 `false`。使用方法都为:`q.成员函数()` _`q` 代表 `priority_queue` 的名称_。#**pb__ds**`pb__ds` ($\tt Policy-Based\ Data\ Structures$)库包含于 `__gnu_pbds` 命名空间里,使用其中的堆需要两个头文件:```cpp#include <ext/pb_ds/assoc_container.hpp>#include <ext/pb_ds/priority_queue.hpp>```**万能头不能解决问题,`<bits/stdc++.h>` 只包含标准库的所有头文件,而pb__ds库是扩展库!**21 行未改动8. `join()` 把另一个堆和当前堆合并并清空被合并的堆。时间复杂度:$\begin{array}{llllll}& \text { push } & \text {pop } & \text { modify } & \text { erase } & \text { join } \\\small \text { std::priority\_queue } & \Theta(n) / \Theta(\log n) &\Theta(\log n) & \Theta(n \log n) & \Theta(n \log n) & \Theta(n \log n) \\\small \text { \_\_gnu\_pbds::pairing\_heap\_tag } & \mathcal{O}(1) & \Theta(n) / \Theta(\log n) &\Theta(n) / \Theta(\log n) & \Theta(n) / \Theta(\log n)& \mathcal{O}(1) \\\small \text { \_\_gnu\_pbds::binary\_heap\_tag } & \Theta(n) / \Theta(\log n) &\Theta(n) / \Theta(\log n) &\Theta(n) & \Theta(n) & \Theta(n) \\2 行未改动\small \text { \_\_gnu\_pbds::thin\_heap\_tag } & \mathcal{O}(1) & \Theta(n) / \Theta(\log n) &\Theta(\log n) / \mathcal{O}(1) &\Theta(n) / \Theta(\log n) & \mathcal{O}(n)\end{array}$#**斐波那契堆**:斐波那契堆是堆的集合,比二叉堆拥有更高的效率,但是由于常数比较大而且不好写所以不常用。**堆优化 dijkstra**:每次更新最短距离的时候把对应的元素上调,每次从小根堆中取堆顶做下一次要用的顶点,从复杂度 $\mathcal{O}(V^2)$ 优化到 $\mathcal{O}(E \log V)$。#**复杂度**:除建堆和 `top()` 操作是 $\mathcal{O}(1)$ 外,其余操作全部为 $\mathcal{O}(\log n)$。#**例题**:[P3378 【模板】堆](https://www.luogu.com.cn/problem/P3378)4 行未改动15 行未改动161718192021222324103 行未改动全部展开12812913013113213313413513656 行未改动19319419519619719819920020146 行未改动24824925025125225325425525625725825926026126226326421 行未改动2862872882892902912922932942 行未改动2972982993003013023033043053063073083093103113123133143153163173183193203214 行未改动15 行未改动堆实质上是一颗二叉树,二叉树的存储使用一维数组,编号为 $i$ 的结点的父结点编号为 $\left\lfloor\dfrac{i}{2}\right\rfloor$,左儿子编号为 $2 \times i$,右儿子编号为 $2 \times i +1$。因为性质 $1$,堆还能用于排序。#**手写堆:**手写堆虽然麻烦,但是效率比较高,尽管有替代品但是这是理解堆及二叉树思想的最好方法,**以下讲解为小根堆**。103 行未改动全部展开return heap[0];}```#**代码**:_此代码可过模板题_56 行未改动return 0;}```#**STL**STL 实现的堆有两种:46 行未改动`empty()` 查询堆是否为空,返回值为 `bool` 类型,堆为空时返回 `true`,否则返回 `false`。使用方法都为:`q.成员函数()` _`q` 代表 `priority_queue` 的名称_。#**pb__ds**`pb__ds` ($\tt Policy-Based\ Data\ Structures$)库包含于 `__gnu_pbds` 命名空间里,使用其中的堆需要两个头文件:```cpp#include <ext/pb_ds/assoc_container.hpp>#include <ext/pb_ds/priority_queue.hpp>```**万能头不能解决问题,`<bits/stdc++.h>` 只包含标准库的所有头文件,而pb__ds库是扩展库!**21 行未改动8. `join()` 把另一个堆和当前堆合并并清空被合并的堆。时间复杂度:$\begin{array}{llllll}& \text { push } & \text {pop } & \text { modify } & \text { erase } & \text { join } \\\small \text { std::priority\_queue } & \Theta(n) / \Theta(\log n) &\Theta(\log n) & \Theta(n \log n) & \Theta(n \log n) & \Theta(n \log n) \\\small \text { \_\_gnu\_pbds::pairing\_heap\_tag } & \mathcal{O}(1) & \Theta(n) / \Theta(\log n) &\Theta(n) / \Theta(\log n) & \Theta(n) / \Theta(\log n)& \mathcal{O}(1) \\\small \text { \_\_gnu\_pbds::binary\_heap\_tag } & \Theta(n) / \Theta(\log n) &\Theta(n) / \Theta(\log n) &\Theta(n) & \Theta(n) & \Theta(n) \\2 行未改动\small \text { \_\_gnu\_pbds::thin\_heap\_tag } & \mathcal{O}(1) & \Theta(n) / \Theta(\log n) &\Theta(\log n) / \mathcal{O}(1) &\Theta(n) / \Theta(\log n) & \mathcal{O}(n)\end{array}$#**斐波那契堆**:斐波那契堆是堆的集合,比二叉堆拥有更高的效率,但是由于常数比较大而且不好写所以不常用。**堆优化 dijkstra**:每次更新最短距离的时候把对应的元素上调,每次从小根堆中取堆顶做下一次要用的顶点,从复杂度 $\mathcal{O}(V^2)$ 优化到 $\mathcal{O}(E \log V)$。#**复杂度**:除建堆和 `top()` 操作是 $\mathcal{O}(1)$ 外,其余操作全部为 $\mathcal{O}(\log n)$。#**例题**:[P3378 【模板】堆](https://www.luogu.com.cn/problem/P3378)4 行未改动