Notes「AtCoder Library 指南」

· · 个人记录

官方文档

注意:以下代码需要使用

#include <atcoder/all>
// 这个头文件可以换为 <atcoder/***> *** 为列出的二级标题的英文
using namespace atcoder;

声明

数据结构

树状数组(fenwicktree)

定义

fenwick_tree<T> fw(int n)

定义了一个初始所有元素为 0 且长度为 n 的树状数组。

限制:

时空复杂度:\mathcal O(n)。

单点加

void fw.add(int p, T x)

执行操作 a[p] += x,a 为树状数组的元素序列。

限制:

时空复杂度:\mathcal O(\log n)。

区间求和

T fw.sum(int l, int r)

返回 a[l] + a[l + 1] + ... + a[r - 1]。注意最后一项是 a[r - 1],下同。若 T 不是 modint,则自然溢出。

限制:

时空复杂度:\mathcal O(\log n)。

参考实现

practice2_b

不带懒标记的线段树(segtree)

定义

segtree<S, op, e> seg(int n)
segtree<S, op, e> seg(vector<S> v)

以下内容要被定义:

集合 S、二元运算 \cdot 与单位元 e 应满足:

对于第一种定义方式,可以定义一个初始所有元素为 0 且长度为 n 的线段树。

对于第二种定义方式,可以定义一个初始长度为 v.size(),元素为 v 中元素的线段树。

限制:

时空复杂度:\mathcal O(Tn)。

单点修改

void seg.set(int p, S x)

它将 a[p] 覆盖为 x。

限制:

时空复杂度:\mathcal O(T\log n)。

单点查询

S seg.get(int p)

它返回 a[p]。

限制:

时空复杂度:\mathcal O(T\log n)。

区间 \bm\cdot 运算

S seg.prod(int l, int r)

它返回 op(a[l], ..., a[r - 1])(如果上述结合律和单位元性质成立)。如果 l=r,它返回单位元 e()。

限制:

时空复杂度:\mathcal O(T\log n)。

全局 \bm\cdot 运算

S seg.all_prod()

它返回 op(a[0], ..., a[n - 1])(如果上述结合律和单位元性质成立)。如果 n=0,它返回单位元 e()。

限制:

时空复杂度:\mathcal O(T)。

在线段树右半部分上二分

int seg.max_right<f>(int l)
int seg.max_right<F>(int l, F f)

对于第一种实现方式,函数 bool f(S x) 需要被定义。

对于第二种实现方式,一个给定 S 返回 bool 的函数类型 F 和函数 f。

设 f 函数需要 \mathcal O(T) 时空。

它返回一个位置 r 满足:

若 f 单调,r 是满足 f(op(a[l], a[l + 1], ..., a[r - 1])) = true 的最大值。

限制:

时空复杂度:\mathcal O(T\log n)。

在线段树左半部分上二分

int seg.min_left<f>(int r)
int seg.min_left<F>(int r, F f)

对于第一种实现方式,函数 bool f(S x) 需要被定义。

对于第二种实现方式,一个给定 S 返回 bool 的函数类型 F 和函数 f。

设 f 函数需要 \mathcal O(T) 时空。

它返回一个位置 l 满足:

若 f 单调,l 是满足 f(op(a[l], a[l + 1], ..., a[r - 1])) = true 的最小值。

限制:

时空复杂度:\mathcal O(T\log n)。

参考实现

practice2_j

带懒标记的线段树(lazysegtree)

定义

lazy_segtree<S, op, e, F, mapping, composition, id> seg(int n);
lazy_segtree<S, op, e, F, mapping, composition, id> seg(vector<T> v);

以下内容要被定义:

集合 S、二元运算 \cdot、单位元 e、映射集合 F、复合函数 \circ、特征映射 \text{id} 应满足:

对于第一种定义方式,可以定义一个初始所有元素为 0 且长度为 n 的线段树。

对于第二种定义方式,可以定义一个初始长度为 v.size(),元素为 v 中元素的线段树。

限制:

时空复杂度:\mathcal O(Tn)。

单点修改

void seg.set(int p, S x)

它将 a[p] 覆盖为 x。

限制:

时空复杂度:\mathcal O(T\log n)。

单点查询

S seg.get(int p)

它返回 a[p]。

限制:

时空复杂度:\mathcal O(T\log n)。

区间 \bm\cdot 运算

S seg.prod(int l, int r)

它返回 op(a[l], ..., a[r - 1])(如果上述结合律和单位元性质成立)。如果 l=r,它返回单位元 e()。

限制:

时空复杂度:\mathcal O(T\log n)。

全局 \bm\cdot 运算

S seg.all_prod()

它返回 op(a[0], ..., a[n - 1])(如果上述结合律和单位元性质成立)。如果 n=0,它返回单位元 e()。

限制:

时空复杂度:\mathcal O(T)。

单点或区间 \bm f 运算

void seg.apply(int p, F f)
void seg.apply(int l, int r, F f)

对于第一种实现方式,它调用 a[p] = f(a[p])。

对于第二种实现方式,对于所有 l\leq i<r,它调用 a[i] = f(a[i])。

限制:

时空复杂度:\mathcal O(T\log n)。

在线段树右半部分上二分

int seg.max_right<g>(int l)
int seg.max_right<G>(int l, G g)

对于第一种实现方式,函数 bool g(S x) 需要被定义。

对于第二种实现方式,一个给定 S 返回 bool 的函数类型 G 和函数 g。

设 g 函数需要 \mathcal O(T) 时空。

它返回一个位置 r 满足:

若 g 单调,r 是满足 g(op(a[l], a[l + 1], ..., a[r - 1])) = true 的最大值。

限制:

时空复杂度:\mathcal O(T\log n)。

在线段树左半部分上二分

int seg.min_left<g>(int r)
int seg.min_left<G>(int r, G g)

对于第一种实现方式,函数 bool g(S x) 需要被定义。

对于第二种实现方式,一个给定 S 返回 bool 的函数类型 F 和函数 g。

设 g 函数需要 \mathcal O(T) 时空。

它返回一个位置 l 满足:

若 g 单调,l 是满足 g(op(a[l], a[l + 1], ..., a[r - 1])) = true 的最小值。

限制:

时空复杂度:\mathcal O(T\log n)。

参考实现

practice2_k

practice2_l

字符串(string)

下面,s_{[l,r)} 指 s_ls_{l+1}\cdots s_{r-1},即这些元素依次连接。

SA

vector<int> suffix_array(string s)
vector<int> suffix_array<T>(vector<T> s)
vector<int> suffix_array(vector<int> s, int upper)

它返回 sa 数组满足 \forall 0\leq i< n-1 有 s_{[\text{sa}_i,n)}<s_{[\text{sa}_{i+1},n)}。

限制:

时空复杂度:

LCP 数组

需要先选择合理的 suffix_array 函数调用。

vector<int> lcp_array(string s, vector<int> sa)
vector<int> lcp_array<T>(vector<T> s, vector<int> sa)

它返回 lcp 数组满足 \forall 0\leq i< n-1 有 \text{lcp}_i=\left|\operatorname{lcp}(s_{[\text{sa}_i,n)},s_{[\text{sa}_{i+1},n)})\right|。现在才知道,这就是所谓的 \textit{height} 数组……

限制:

时空复杂度:\mathcal O(n)。

Z 算法

vector<int> z_algorithm(string s)
vector<int> z_algorithm<T>(vector<T> s)

它返回 z 数组满足 \forall 0\leq i< n 有 z_i=\left|\operatorname{lcp}(s,s_{[i,n)})\right|。

限制:

时空复杂度:\mathcal O(n)。

参考实现

practice2_i

数学

数论(math)

快速幂

ll pow_mod(ll x, ll n, int m)

返回 x^n\bmod m。

限制:

时空复杂度:\mathcal O(\log n)。

求逆

ll inv_mod(ll x, ll m)

返回 y 满足 0\leq y<m 且 xy\equiv1\pmod m。

限制:

时空复杂度:\mathcal O(\log m)。

CRT

pair<ll, ll> crt(vector<ll> r, vector<ll> m)

返回同余方程组(n 为 r,m 的长度)

x\equiv r_i\pmod{m_i},\forall 0\leq i<n

的解。

如果无解,返回 (0,0)。

如果 n=1,返回 (0,1)。

否则返回 (y,z),表示解为

x\equiv y\pmod z

限制:

时空复杂度:\mathcal O\left(n\log \mathop{\operatorname{lcm}}\limits_{i=0}^{n-1}m_i\right)。

类欧几里得算法

ll floor_sum(ll n, ll m, ll a, ll b)

返回

\sum_{i=0}^{n-1}\left\lfloor\dfrac{a\cdot i+b}{m}\right\rfloor

自然溢出(对 2^{64} 取模)。

限制:

时空复杂度:\mathcal O(\log m)。

参考实现

practice2_c

卷积(convolution)

NTT 模数下的卷积

vector<T> convolution<int m = 998244353>(vector<T> a, vector<T> b)
vector<static_modint<m>> convolution<int m>(vector<static_modint<m>> a, vector<static_modint<m>> b)

返回 a 和 b 的卷积序列

c_i=\sum_{j=0}^ia_jb_{i-j}

对 m 取模的结果。若 a,b 中任意一个是空的,返回值就是空的。第一种实现下,m 默认为 998\ 244\ 353。

限制:

时空复杂度:\mathcal O(n\log n+\log m),其中 n=|a|+|b|。

ll 范围内的卷积

vector<ll> convolution_ll(vector<ll> a, vector<ll> b)

返回 a 和 b 的卷积序列

c_i=\sum_{j=0}^ia_jb_{i-j}

若 a,b 中任意一个是空的,返回值就是空的。

限制:

时空复杂度:\mathcal O(n\log n),其中 n=|a|+|b|。

参考实现

practice2_f

\bm{\mathbb{F}_p} 下的整数运算(modint)

定模数

可以使用 modint998244353、modint1000000007。

其他模数可使用 static_modint<p>,p 为模数。

不定模数

可以使用 setmod,见下。

还可以使用 dynamic_modint<p>,p 用于区分不同模数。

定义

modint x()
modint x<T>(T y)

在第一种定义方式下,初始值为 0。

在第二种定义方式下,T 可以为 int、char、ull、bool 等,初始值为 y。

定义模数

void modint::set_mod(int m)

定义了模数为 m。必须事先调用。

限制:

时空复杂度:\mathcal O(1)。

返回模数

int modint::mod()

返回了模数。

限制:

时空复杂度:\mathcal O(1)。

返回储存的值

int x.val();

返回了储存在 x 的值。

限制:

时空复杂度:\mathcal O(1)。

运算

可以支持以下运算。

// 取相反数
-modint;

// 自增自减
modint++;
modint--;
++modint;
--modint;

// 四则运算
modint + modint;
modint - modint;
modint * modint;
modint / modint;

// 四则赋值运算
modint += modint;
modint -= modint;
modint *= modint;
modint /= modint;

// 关系运算
modint == modint;
modint != modint;

modint 与 int 运算时,int 类型会自动解析为 modint。

限制:

时空复杂度:做除法为 \mathcal O(\log\text{mod}),其余为 \mathcal O(1)。

快速幂(modint 下)

modint x.pow(ll n)

返回 x^n。

限制:

时空复杂度:\mathcal O(\log n)

求逆(modint 下)

modint x.inv()

返回 y 满足 xy\equiv1\pmod m。

限制:

时空复杂度:\mathcal O(\log \text{mod})。

卡♂ 常♂ 大♂ 法

modint modint::raw(int x)

前面说过,modint 与 int 运算时,int 类型会自动解析为 modint。这时,int 类型的量会对 mod 取模,即使值小于 mod。使用这个函数,可以直接把 x 转成 mod,减小常数。

限制:

图论

并查集(dsu)

定义

dsu d(int n)

创建一个 n 个点的空并查集。

限制:

时空复杂度:\mathcal O(n)。

合并

int d.merge(int a, int b)

连接边 a 和 b,并返回 a 和 b 所在联通块代表节点的编号。

限制:

时空复杂度:均摊 \mathcal O(\alpha(n))。

查询两个点是否在同一联通块

bool d.same(int a, int b)

返回 a 和 b 是否在同一联通块。

限制:

时空复杂度:均摊 \mathcal O(\alpha(n))。

查询联通块代表节点

int d.leader(int a)

返回 a 所在联通块的代表节点。

限制:

时空复杂度:均摊 \mathcal O(\alpha(n))。

查询联通块大小

int d.size(int a)

返回 a 所在联通块的大小。

限制:

时空复杂度:均摊 \mathcal O(\alpha(n))。

所有联通块

vector<vector<int>> d.groups()

返回所有的联通块。具体地,返回联通块所组成的 vector,每一个 vector 内表示同一个联通块的所有点。

限制:

时空复杂度:\mathcal O(n)。

参考实现

practice2_a

网络最大流(maxflow)

定义

mf_graph<Cap> graph(int n)

创建一个 n 个点的空图。Cap 是容量的类型。

限制:

时空复杂度:\mathcal O(n)。

加边

int graph.add_edge(int from, int to, Cap cap);

加一条从 \text{from} 到 \text{to},容量为 \text{cap} 的边。返回目前边数。

限制:

时空复杂度:均摊 \mathcal O(1)。

最大流

Cap graph.flow(int s, int t);
Cap graph.flow(int s, int t, Cap flow_limit);

跑从 s 到 t 的网络流(限制流量为 \text{flow\_limit})。

可以多次调用。

限制:

时空复杂度:(下令 m 为边数,下同)

构造最小割的方案

vector<bool> graph.min_cut(int s)

若在残量网络上,s 和 t 可达,那么返回值 a 中,a_t 为真,否则为假。这对应一种最小割的方案。

限制:

时空复杂度:\mathcal O(n+m)。

遍历边

mf_graph<Cap>::edge graph.get_edge(int i);
vector<mf_graph<Cap>::edge> graph.edges();

其中,mf_graph<Cap>::edge 的定义如下:

struct mf_graph<Cap>::edge {
    int from, to;
    Cap cap, flow;
};

返回现在边的状态,和 add_edge 加入边的顺序相同。

限制:

时空复杂度:

改变边的容量

void graph.change_edge(int i, Cap new_cap, Cap new_flow);

将第 i 条边的容量改为 \text{new\_cap},流量改为 \text{new\_flow}。

限制:

时空复杂度:\mathcal O(n+m)。

参考实现

practice2_d

最小费用最大流(mincostflow)

定义

mcf_graph<Cap, Cost> graph(int n)

创建一个 n 个点的空图。Cap 是容量的类型,Cost 是费用的类型。

限制:

时空复杂度:\mathcal O(n)。

加边

int graph.add_edge(int from, int to, Cap cap, Cost cost)

加一条从 \text{from} 到 \text{to},容量为 \text{cap},费用为 \text{cost} 的边。返回目前边数。

限制:

时空复杂度:均摊 \mathcal O(1)。

最小费用最大流

pair<Cap, Cost> graph.flow(int s, int t)
pair<Cap, Cost> graph.flow(int s, int t, Cap flow_limit)

跑从 s 到 t 的最小费用最大流(限制流量为 \text{flow\_limit}),返回流量与费用。

vector<pair<Cap, Cost>> graph.slope(int s, int t)
vector<pair<Cap, Cost>> graph.slope(int s, int t, Cap flow_limit)

函数 g(x) 是流量为 x 时的最小费用。它是线性分段函数。这个函数返回 g 的所有整数值处的元素,且满足:

这两个函数不能多次调用。

限制:

时空复杂度:\mathcal O(F(n+m)\log(n+m)),其中 F 为最大流。

遍历边

mcf_graph<Cap, Cost>::edge graph.get_edge(int i)
vector<mcf_graph<Cap, Cost>::edge> graph.edges()

其中,mcf_graph<Cap, Cost>::edge 的定义如下:

struct mcf_graph<Cap, Cost>::edge {
    int from, to;
    Cap cap, flow;
    Cost cost;
};

返回现在边的状态,和 add_edge 加入边的顺序相同。

限制:

时空复杂度:

参考实现

practice2_e

强联通分量(scc)

定义

scc_graph graph(int n)

创建一个 n 个点的空图。

限制:

时空复杂度:\mathcal O(n)。

加边

void graph.add_edge(int from, int to)

加一条从 \text{from} 到 \text{to} 的边。

限制:

时空复杂度:均摊 \mathcal O(1)。

求强联通分量

vector<vector<int>> graph.scc()

返回一个 vector 的列表,满足:

限制:

时空复杂度:\mathcal O(n+m)。

参考实现

practice2_g

2-SAT(twosat)

定义

two_sat ts(int n)

定义一个有 n 个变量的空 2-SAT 规则。

限制:

时空复杂度:\mathcal O(n)。

增加假设

void ts.add_clause(int i, bool f, int j, bool g)

增加一个假设「x_i=f 或 x_j=g」。

限制:

时空复杂度:均摊 \mathcal O(1)。

检查是否有解

bool ts.satisfiable()

如果有解,返回 true,否则返回 false。可以多次调用。

限制:

时空复杂度:\mathcal O(n+m),m 为限制数。

返回解

vector<bool> ts.answer()

如果上次 satisfiable 函数返回 true,则返回 n 个 bool 表示答案。否则,返回 n 个未定义的内容。

限制:

时空复杂度:\mathcal O(n)。

参考实现

practice2_h