- B1: 前缀和与差分
- B2: 快速组合
- B3: 二分查找
- B4: 高精度加法
- B4: 高精度除法
- B4: 高精度乘法
- B4: 高精度减法
- D1: 单调栈
- D2: 单调队列
- D3: Spare Table
- D4: 字典树
- D5: 并查集
- D6: 树状数组-限定区间计数
- D6: 树状数组-单点更新区间求和
- D7: 线段树-基础
- G1: 二分图-判定
- G2: 二分图-最大匹配
- G3: Astar k短路 on matrix
- G4: 拓扑排序
- G5: 连通无环无向图的重心
- G6: MST-Kruskal
- G7: MST-Prim
- G8: ShortestPath-BellmanFord
- G9: ShortestPath-Dijkstra
- G9: ShortestPath-Dijkstra Heap OPT
- G10: ShortestPath-SPFA
- G11: ShortestPath-Floyd
- M1: 快速幂与龟速乘
- M2: 欧几里得-最大公约数
- M2: EX欧几里得-翡蜀定理
- M2: EX欧几里得-线性同余方程
- M2: EX欧几里得-乘法逆元
- M3: 费马小定理-乘法逆元
- M4: 分解质因数
- M4: 分解质因数-欧拉函数
- M4: 分解质因数-多数乘积约数计数
- M4: 分解质因数-多数乘积约数求和
- M5: 欧拉筛
- M5: 欧拉筛-质数筛
- M6: 埃氏筛-质数筛
- M7: Stein算法-最大公约数
- M8: 矩阵乘法与快速幂
- O1: 快速排序
- O2: 并归排序
- S1: KMP
- S2: 字符串哈希
This doc has been marked needs-review. Following information are in upcoming update plan.
template< class type, class Comp >
void quick_sort(type *v, int l, int r, Comp cmp) {
static int i, j;
static type x;
if (l >= r-1) return ;
i = l-1, j = r;
x = v[(l+r)>>1u];
while (i < j) {
while (cmp(v[++i], x)) ;
while (cmp(x, v[--j])) ;
if (i < j) swap(v[i], v[j]);
}
quick_sort(v, l, i, cmp);
quick_sort(v, i, r, cmp);
}
Usage
如果不是自定义比较函数, 一般cmp只能是less<>()和greater<>().
Notes
-
l, r的意义类似于begin(), end() - 注意边界问题, 此模版中x不可取值为
v[l] -
i, j两个指针向中间逼近时必须使用严格的大于(小于)比较, 这样能保证指针不越界 - 取中值或随机(位置)值优于取左(右)值
快速排序的应用
查找rank_k
template< class type, class Comp >
type seek_kth(type *v, int l, int r, int k, Comp cmp) {
static int i, j;
static type x;
i = l-1, j = r;
x = v[(l+r)>>1u];
while (i < j) {
while (cmp(v[++i], x)) ;
while (cmp(x, v[--j])) ;
if (i < j) swap(v[i], v[j]);
}
if (v[i] == x && i == k) return x;
if(k < i) return seek_kth(v, l, i, k, cmp);
else return seek_kth(v, i, r, k, cmp);
}