Appendix / FAQ
Environments
- Do not use the macro beginning with
ATCODER_. - Although we aimed to make it work in many environments, it requires some C++ extension. We assume the following.
__int128 / unsigned __int128(g++, clang++)or_mul128 / _umul128(Visual Studio)works.__builtin_(ctz/ctzll/clz/clzll/popcount)(g++, clang++)or_BitScan(Forward/Reverse)(Visual Studio)works.char / short / int / lland theirunsignedtypes (andsigned char) are8 / 16 / 32 / 64bits.- 💻 Signed integers are two’s complement.
How to Install
g++ / clang++
The easiest way is to put atcoder folder in the same place as main.cpp and execute g++ main.cpp -std=c++14 -I ., as noted in the index. Here, . is the symbol that represents the current directory (you should type a space and a period after I).
If you don’t want to copy atcoder folder manually, do as follows.
- Specify the command like
g++ main.cpp -std=c++14 -I /path/to/ac-library(/path/to/ac-librarystands for the directory where the downloaded ac-library is located). - Specify the place of
atcoderfolder by the environment variableCPLUS_INCLUDE_PATHasCPLUS_INCLUDE_PATH="/path/to/ac-library". (On Windows, type likeC:\path\to\ac-libraryin the field of the variableCPLUS_INCLUDE_PATHon the Window of Environment Variables. Note that, you should use backslashes, not slashes.) Then, you can compile just byg++ main.cpp -std=c++14.
Visual Studio
Visual Studio 2019 / 2022 is supported. Update it if you are using old Visual Studio.
If Visual Studio is installed, there is a folder like the following.
C:\Program Files\Microsoft Visual Studio\2022\(Community, Professional or Enterprise)\VC\Tools\MSVC\(Some number, e.g. 14.35.32215)\includeC:\Program Files (x86)\Microsoft Visual Studio\2019\(Community, Professional or Enterprise)\VC\Tools\MSVC\(Some number, e.g. 14.27.29110)\include
Copy atcoder folder into it, i.e., put it so that the path will be as follows.
C:\Program Files\Microsoft Visual Studio\2022\(Community, Professional or Enterprise)\VC\Tools\MSVC\(Some number, e.g. 14.35.32215)\include\atcoder\dsu.hpp
How to Submit to Other Online Judge Systems
We prepared the script expander.py (python3.5 or later).
The code combined.cpp, which can be compiled on other online judge systems, is generated by executing python3 expander.py main.cpp.
Although we tested it, we do not guarantee that it works correctly.
Preliminaries
💻Mark
This mark represents that the part, e.g., modint, may be difficult to use if you do not know much about C++. AC Library is designed to be still usable if you ignore the parts with this mark.
Template Function
For example, suffix_array(v) can take vector<int>, vector<ll>, et cetera as an argument. In this document, we unify these notations to suffix_array<T>(vector<T> v).
For example, to calculate the suffix array of the integral array stored in vector<int>, we can code as follows.
vector<int> sa = suffix_array(v);
// vector<int> sa = suffix_array<int>(v); : wrong usageDefault Constructor
The structs like scc_graph can be declared not only like the former code, but also like the latter code without initialization.
#include <atcoder/scc>;
using namespace atcoder;
int main() {
int n;
scanf("%d", &n);
scc_graph g(n); // create the graph with n vertices
return 0;
}#include <atcoder/scc>;
using namespace atcoder;
scc_graph g;
int main() {
return 0;
}If it is declared in the latter way, the behavior (of the default constructor) is as follows.
- It takes -time.
- The behavior is undefined if the member variables are accessed or the member functions are called.
You can also assign a value to the struct later.
#include <atcoder/scc>;
using namespace atcoder;
scc_graph g;
int main() {
g = scc_graph(10);
return 0;
}The Type of the Edges
In the graph libraries, the type mf_graph<Cap>::edge is used to store edges.
For example, the type of the edges of mf_graph<int> is mf_graph<int>::edge.
If you are not familiar to ::, you can use the string mf_graph<int>::edge in the same manner as int or string, like the following example.
vector<mf_graph<int>::edge> v;
mf_graph<int>::edge e;Default Template Arguments
Sometimes the following notation is used, as in the document of convolution.
vector<T> convolution<int m = 998244353>(vector<T> a, vector<T> b)It means the default template argument. As the following example, you can call the function without explicitly specifying m.
vector<long long> c = convolution(a, b);
vector<long long> c = convolution<924844033>(a, b);In the first case, is automatically set to be . In the second case, becomes the value that is explicitly specified, which is here.
💻 explicit specifier
Constructors of structs except modint are declared with the explicit specifier.
Precise requirements in Segtree / LazySegtree
In some situations, the cardinality of algebraic structures for Segtree / LazySegtree would be infinite. In precise meaning, it may break the constraints in the document.
For example, for the typical LazySegtree on that processes the queries of range max and range addition, is not closed under addition due to the overflow. To resolve this problem, it is ensured to work correctly in the following situation.
Segtree
- is an algebraic structure that satisfies the properties in the document.
- If the arguments and the results are in , the types and operations work correctly.
- For any moment, every contigurous subsequence satisfies .
LazySegtree
- is a pair of algebraic structures that satisfies the properties in the document.
- If the arguments and the results are in or , the types and operations work correctly.
- For any moment, every contigurous subsequence satisfies .
- Let us fix an element and denote the maps acted on it by in this order. Then, for every contigurous subsequence , .
Above LazySegtree can naturally be defined as follows, using infinite algebraic structures and .
- The binary operation on is and .
- is the set of the maps that adds a constant integer ( is the map that adds ).
Under some sufficiently small constraints, it can be treated by this library by setting as follows.
- is represented by . The element is represented as in if and if .
- is the set of the maps that adds a constant integer in and represented naturally using the type .
The Behavior of maxflow
Internally, for each edge , it stores the flow amount and the capacity . Let and be the set of edges starts and ends at , respectively. For each vertex , let .
flow(s, t)
It changes the flow amount of each edge. Let and be the flow amount of edge before and after calling it, respectively. Precisely, it changes the flow amount as follows.
- holds for all vertices other than and .
- If flow_limit is specified, .
- is maximized under these conditions. It returns this .
min_cut(s)
The residual network is the graph whose edge set is given by gathering for each edge with and for each edge with . It returns the set of the vertices that is reachable from in the residual network.
change_edge(i, new_cap, new_flow)
It changes the flow amount and the capacity of the edge to new_flow and new_cap, respectively. It doesn’t change other values.
Convolution
It calculates convolution. Given two arrays and , it calculates the array of length , defined by
convolution
(1) vector<T> convolution<int m = 998244353>(vector<T> a, vector<T> b)
💻(2) vector<static_modint<m>> convolution<int m>(vector<static_modint<m>> a, vector<static_modint<m>> b)It calculates the convolution in . It returns an empty array if at least one of and are empty.
@{keyword.constraints}
- is prime.
- There is an integer with and .
- (1)
Tisint,uint,ll, orull.
@{keyword.complexity}
- , where .
convolution_ll
vector<ll> convolution_ll(vector<ll> a, vector<ll> b)It calculates the convolution. It returns an empty array if at least one of and are empty.
@{keyword.constraints}
- All the elements of the array are in
llafter convolution
@{keyword.complexity}
- , where .
DSU
Given an undirected graph, it processes the following queries in time (amortized).
- Edge addition
- Deciding whether given two vertices are in the same connected component
Each connected component internally has a representative vertex.
When two connected components are merged by edge addition, one of the two representatives of these connected components becomes the representative of the new connected component.
Constructor
dsu d(int n)- It creates an undirected graph with vertices and edges.
@{keyword.constraints}
@{keyword.complexity}
merge
int d.merge(int a, int b)It adds an edge .
If the vertices and were in the same connected component, it returns the representative of this connected component. Otherwise, it returns the representative of the new connected component.
@{keyword.constraints}
@{keyword.complexity}
- amortized
same
bool d.same(int a, int b)It returns whether the vertices and are in the same connected component.
@{keyword.constraints}
@{keyword.complexity}
- amortized
leader
int d.leader(int a)It returns the representative of the connected component that contains the vertex .
@{keyword.constraints}
@{keyword.complexity}
- amortized
size
int d.size(int a)It returns the size of the connected component that contains the vertex .
@{keyword.constraints}
@{keyword.complexity}
- amortized
groups
vector<vector<int>> d.groups()It divides the graph into connected components and returns the list of them.
More precisely, it returns the list of the “list of the vertices in a connected component”. Both of the orders of the connected components and the vertices are undefined.
@{keyword.complexity}
Fenwick Tree
Given an array of length , it processes the following queries in time.
- Updating an element
- Calculating the sum of the elements of an interval
Constructor
fenwick_tree<T> fw(int n)- It creates an array of length . All the elements are initialized to .
@{keyword.constraints}
Tisint,uint,ll,ull, ormodint
@{keyword.complexity}
add
void fw.add(int p, T x)It processes a[p] += x.
@{keyword.constraints}
@{keyword.complexity}
sum
T fw.sum(int l, int r)It returns a[l] + a[l + 1] + ... + a[r - 1].
If T is integer type(int, uint, ll, or ull), it returns the answer in , if overflowed.
@{keyword.constraints}
@{keyword.complexity}
Lazy Segtree
It is the data structure for the pair of a monoid and a set of mappings that satisfies the following properties.
- contains the identity map , where the identity map is the map that satisfies for all .
- is closed under composition, i.e., holds for all .
- holds for all and .
Given an array of length , it processes the following queries in time (see Appendix for further details).
- Acting the map (cf. ) on all the elements of an interval
- Calculating the product of the elements of an interval
For simplicity, in this document, we assume that the oracles op, e, mapping, composition, and id work in constant time. If these oracles work in time, each time complexity appear in this document is multipled by .
Constructor
(1) lazy_segtree<S, op, e, F, mapping, composition, id> seg(int n);
(2) lazy_segtree<S, op, e, F, mapping, composition, id> seg(vector<S> v);The following should be defined.
- The type
Sof the monoid - The binary operation
S op(S a, S b) - The function
S e()that returns - The type
Fof the map - The function
S mapping(F f, S x)that returns - The function
F composition(F f, F g)that returns - The function
F id()that returns
See examples and here for further details.
- (1): It creates an array
aof lengthn. All the elements are initialized toe(). - (2): It creates an array
aof lengthn = v.size(), initialized tov.
@{keyword.constraints}
@{keyword.complexity}
set
void seg.set(int p, S x)a[p] = x
@{keyword.constraints}
@{keyword.complexity}
get
S seg.get(int p)It returns a[p].
@{keyword.constraints}
@{keyword.complexity}
prod
S seg.prod(int l, int r)It returns op(a[l], ..., a[r - 1]), assuming the properties of the monoid. It returns e() if .
@{keyword.constraints}
@{keyword.complexity}
all_prod
S seg.all_prod()It returns op(a[0], ..., a[n - 1]), assuming the properties of the monoid. It returns e() if .
@{keyword.complexity}
apply
(1) void seg.apply(int p, F f)
(2) void seg.apply(int l, int r, F f)- (1) It applies
a[p] = f(a[p]). - (2) It applies
a[i] = f(a[i])for alli = l..r-1.
@{keyword.constraints}
- (1)
- (2)
@{keyword.complexity}
max_right
(1) int seg.max_right<g>(int l)
(2 ) int seg.max_right<G>(int l, G g)- (1): It applies a binary search on the segment tree. The function
bool g(S x)should be defined. - (2): The function object that takes
Sas the argument and returnsboolshould be defined.
It returns an index r that satisfies both of the followings.
r = lorg(op(a[l], a[l + 1], ..., a[r - 1])) = truer = norg(op(a[l], a[l + 1], ..., a[r])) = false
If g is monotone, this is the maximum r that satisfies g(op(a[l], a[l + 1], ..., a[r - 1])) = true.
@{keyword.constraints}
- if
gis called with the same argument, it returns the same value, i.e.,ghas no side effect. g(e()) = true
@{keyword.complexity}
min_left
(1) int seg.min_left<g>(int r)
(2 ) int seg.min_left<G>(int r, G g)- (1): It applies a binary search on the segment tree. The function
bool g(S x)should be defined. - (2): The function object that takes
Sas the argument and returnsboolshould be defined.
It returns an index l that satisfies both of the following.
l = rorg(op(a[l], a[l + 1], ..., a[r - 1])) = truel = 0org(op(a[l - 1], a[l], ..., a[r - 1])) = false
If g is monotone, this is the minimum l that satisfies g(op(a[l], a[l + 1], ..., a[r - 1])) = true.
@{keyword.constraints}
- if
gis called with the same argument, it returns the same value, i.e.,ghas no side effect. g(e()) = true
@{keyword.complexity}
Math
It contains number-theoretic algorithms.
pow_mod
ll pow_mod(ll x, ll n, int m)It returns .
@{keyword.constraints}
@{keyword.complexity}
inv_mod
ll inv_mod(ll x, ll m)It returns an integer such that and .
@{keyword.constraints}
@{keyword.complexity}
crt
pair<ll, ll> crt(vector<ll> r, vector<ll> m)Given two arrays with length , it solves the modular equation system
If there is no solution, it returns . Otherwise, all the solutions can be written as the form , using integers . It returns this as a pair. If , it returns .
@{keyword.constraints}
- is in
ll.
@{keyword.complexity}
floor_sum
ll floor_sum(ll n, ll m, ll a, ll b)It returns
It returns the answer in , if overflowed.
@{keyword.constraints}
@{keyword.complexity}
MaxFlow
It solves maximum flow problem.
Constructor
mf_graph<Cap> graph(int n)It creates a graph of n vertices and edges. Cap is the type of the capacity.
@{keyword.constraints}
Capisintorll
@{keyword.complexity}
add_edge
int graph.add_edge(int from, int to, Cap cap);It adds an edge oriented from the vertex from to the vertex to with the capacity cap and the flow amount . It returns an integer such that this is the -th edge that is added.
@{keyword.constraints}
@{keyword.complexity}
- amortized
flow
(1) Cap graph.flow(int s, int t);
(2) Cap graph.flow(int s, int t, Cap flow_limit);- (1) It augments the flow from to as much as possible. It returns the amount of the flow augmented.
- (2) It augments the flow from to as much as possible, until reaching the amount of
flow_limit. It returns the amount of the flow augmented. - You may call it multiple times. See Appendix for further details.
@{keyword.constraints}
- The answer should be in
Cap.
@{keyword.complexity}
- (if all the capacities are ),
- (general), or
- , where is the returned value
where is the number of added edges.
min_cut
vector<bool> graph.min_cut(int s)It returns a vector of length , such that the -th element is true if and only if there is a directed path from to in the residual network.
The returned vector corresponds to a minimum cut after calling flow(s, t) exactly once without flow_limit. See Appendix for further details.
@{keyword.complexity}
- , where is the number of added edges.
get_edge / edges
struct mf_graph<Cap>::edge {
int from, to;
Cap cap, flow;
};
(1) mf_graph<Cap>::edge graph.get_edge(int i);
(2) vector<mf_graph<Cap>::edge> graph.edges();- It returns the current internal state of the edges.
- The edges are ordered in the same order as added by
add_edge.
@{keyword.constraints}
- (1):
@{keyword.complexity}
- (1):
- (2): , where is the number of added edges.
change_edge
void graph.change_edge(int i, Cap new_cap, Cap new_flow);It changes the capacity and the flow amount of the -th edge to new_cap and new_flow, respectively. It doesn’t change the capacity or the flow amount of other edges. See Appendix for further details.
@{keyword.constraints}
@{keyword.complexity}
MinCostFlow
It solves Minimum-cost flow problem.
Constructor
mcf_graph<Cap, Cost> graph(int n);It creates a directed graph with vertices and edges. Cap and Cost are the type of the capacity and the cost, respectively.
@{keyword.constraints}
CapandCostareintorll.
@{keyword.complexity}
add_edge
int graph.add_edge(int from, int to, Cap cap, Cost cost);It adds an edge oriented from from to to with capacity cap and cost cost. It returns an integer such that this is the -th edge that is added.
@{keyword.constraints}
@{keyword.complexity}
- amortized
min_cost_max_flow
(1) pair<Cap, Cost> graph.flow(int s, int t);
(2) pair<Cap, Cost> graph.flow(int s, int t, Cap flow_limit);- It augments the flow from to as much as possible. It returns the amount of the flow and the cost.
- (1) It augments the flow as much as possible.
- (2) It augments the flow as much as possible, until reaching the amount of
flow_limit.
@{keyword.constraints}
- same as
min_cost_slope.
@{keyword.complexity}
- same as
min_cost_slope.
min_cost_slope
vector<pair<Cap, Cost>> graph.slope(int s, int t);
vector<pair<Cap, Cost>> graph.slope(int s, int t, Cap flow_limit);Let be a function such that is the cost of the minimum cost flow when the amount of the flow is exactly . is known to be piecewise linear. It returns as the list of the changepoints, that satisfies the followings.
- The first element of the list is .
- Both of
.firstand.secondare strictly increasing. - No three changepoints are on the same line.
- (1) The last element of the list is , where is the maximum amount of the flow.
- (2) The last element of the list is , where .
@{keyword.constraints}
Let be the maximum cost among all edges.
- You can’t call
min_cost_slopeormin_cost_max_flowmultiple times. - The total amount of the flow is in
Cap. - The total cost of the flow is in
Cost. - (Cost :
int): - (Cost :
ll):
@{keyword.complexity}
- , where is the amount of the flow and is the number of added edges.
edges
struct edge<Cap, Cost> {
int from, to;
Cap cap, flow;
Cost cost;
};
(1) mcf_graph<Cap, Cost>::edge graph.get_edge(int i);
(2) vector<mcf_graph<Cap, Cost>::edge> graph.edges();- It returns the current internal state of the edges.
- The edges are ordered in the same order as added by
add_edge.
@{keyword.constraints}
- (1):
@{keyword.complexity}
- (1):
- (2): , where is the number of added edges.
Modint
It is the struct that treats the modular arithmetic. All the remaining parts of AC Library works without modint, so you don’t necessarily read this to use the remaining parts.
For most of the problems, it is sufficient to use modint998244353, modint1000000007, or modint, which can be used as follows.
#include <atcoder/modint>
#include <iostream>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
// or: typedef modint998244353 mint;
int main() {
// print sum of array (mod 998244353)
int n;
cin >> n;
mint sum = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
sum += x;
}
cout << sum.val() << endl;
}If the mod is not fixed, you can use modint as follows.
#include <atcoder/modint>
#include <iostream>
using namespace std;
using namespace atcoder;
using mint = modint;
// or: typedef modint mint;
int main() {
// print sum of array (input mod)
int n, mod;
cin >> n >> mod;
mint::set_mod(mod);
mint sum = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
sum += x;
}
cout << sum.val() << endl;
}All the functions except set_mod work for all three of them.
Constructor
(1) modint x()
(2) modint x<T>(T y)- (1) It is the default constructor of modint. will be stored.
- (2) It is the constructor for the integer type
T(int, char, ull, bool, ...).yis stored in modint after taking mod.
set_mod
void modint::set_mod(int m)It sets the mod. It must be called first.
@{keyword.constraints}
@{keyword.complexity}
mod
int modint::mod()It returns the mod.
val
int x.val();It returns the value that is stored in x.
Operations
The following operations work.
-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;The following also works, because it is interpreted as modint(1) + x.
modint x = 10;
1 + x;The following also works, because it is interpreted as y * modint(z).
modint::set_mod(11);
modint y = 10;
int z = 1234;
y * z;@{keyword.constraints}
gcd(b.val(), mod) == 1should be satisfied when evaluatinga / b(ora /= b).
@{keyword.complexity}
- (all operations except division)
- (division)
pow
modint x.pow(ll n)It returns .
@{keyword.constraints}
@{keyword.complexity}
inv
modint x.inv()It returns with .
@{keyword.constraints}
gcd(x.val(), mod) = 1
@{keyword.complexity}
raw
modint modint::raw(int x)It returns modint(x) without taking mod. It is the function for constant-factor speedup.
For example, the following code works even if i is greater than or equal to mod, because mod is automatically taken.
modint a;
int i;
a += i;However, in the following code, it is ensured that i is less than mod.
int main() {
modint::set_mod(1000000007);
modint a = 1;
for (int i = 1; i < 100000; i++) {
a += i;
}
}In such a situation, we can decrease the number of mod operations as follows.
int main() {
modint::set_mod(1000000007);
modint a = 1;
for (int i = 1; i < 100000; i++) {
a += modint::raw(i);
}
}When the value more than or equal to mod is assigned to modint::raw(x), the behavior is undefined.
@{keyword.constraints}
Tips (other mod)
You can use the other fixed mod like 1000000009 as follows.
using mint = static_modint<1000000009>;modint998244353 (resp. modint1000000007) is the alias of static_modint<998244353> (resp. static_modint<1000000007>).
using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;Tips (multiple mods)
You can use multiple mods as follows.
using mint0 = dynamic_modint<0>;
using mint1 = dynamic_modint<1>;modint is the alias of dynamic_modint<-1>.
using modint = dynamic_modint<-1>;SCC
It calculates the strongly connected components of directed graphs.
Constructor
scc_graph graph(int n)It creates a directed graph with vertices and edges.
@{keyword.constraints}
@{keyword.complexity}
add_edge
void graph.add_edge(int from, int to)It adds a directed edge from the vertex from to the vertex to.
@{keyword.constraints}
@{keyword.complexity}
- amortized
scc
vector<vector<int>> graph.scc()It returns the list of the “list of the vertices” that satisfies the following.
- Each vertex is in exactly one “list of the vertices”.
- Each “list of the vertices” corresponds to the vertex set of a strongly connected component. The order of the vertices in the list is undefined.
- The list of “list of the vertices” are sorted in topological order, i.e., for two vertices in different strongly connected components, if there is a directed path from to , the list containing appears earlier than the list containing .
@{keyword.complexity}
- , where is the number of added edges.
Segtree
It is the data structure for monoids , i.e., the algebraic structure that satisfies the following properties.
- associativity: = for all
- existence of the identity element: = = for all
Given an array of length , it processes the following queries in time (see Appendix for further details).
- Updating an element
- Calculating the product of the elements of an interval
For simplicity, in this document, we assume that the oracles op and e work in constant time. If these oracles work in time, each time complexity appear in this document is multipled by .
Constructor
(1) segtree<S, op, e> seg(int n)
(2) segtree<S, op, e> seg(vector<S> v)The following should be defined.
- The type
S - The binary operation
S op(S a, S b) - The identity element
S e()
For example, for Range Minimum Query, the definitions are as follows.
int op(int a, int b) {
return min(a, b);
}
int e() {
return (int)(1e9);
}
segtree<int, op, e> seg(10);- (1): It creates an array
aof lengthn. All the elements are initialized toe(). - (2): It creates an array
aof lengthn = v.size(), initialized tov.
See examples and here for further details.
@{keyword.constraints}
@{keyword.complexity}
set
void seg.set(int p, S x)It assigns to a[p].
@{keyword.constraints}
@{keyword.complexity}
get
S seg.get(int p)It returns a[p].
@{keyword.constraints}
@{keyword.complexity}
prod
S seg.prod(int l, int r)It returns op(a[l], ..., a[r - 1]), assuming the properties of the monoid. It returns e() if .
@{keyword.constraints}
@{keyword.complexity}
all_prod
S seg.all_prod()It returns op(a[0], ..., a[n - 1]), assuming the properties of the monoid. It returns e() if .
@{keyword.complexity}
max_right
(1) int seg.max_right<f>(int l)
(2) int seg.max_right<F>(int l, F f)- (1): It applies binary search on the segment tree. The function
bool f(S x)should be defined. - (2): The function object that takes
Sas the argument and returnsboolshould be defined.
It returns an index r that satisfies both of the following.
r = lorf(op(a[l], a[l + 1], ..., a[r - 1])) = truer = norf(op(a[l], a[l + 1], ..., a[r])) = false
If f is monotone, this is the maximum r that satisfies f(op(a[l], a[l + 1], ..., a[r - 1])) = true.
@{keyword.constraints}
- if
fis called with the same argument, it returns the same value, i.e.,fhas no side effect. f(e()) = true
@{keyword.complexity}
min_left
(1) int seg.min_left<f>(int r)
(2) int seg.min_left<F>(int r, F f)- (1): It applies binary search on the segment tree. The function
bool f(S x)should be defined. - (2): The function object that takes
Sas the argument and returnsboolshould be defined.
It returns an index l that satisfies both of the following.
l = rorf(op(a[l], a[l + 1], ..., a[r - 1])) = truel = 0orf(op(a[l - 1], a[l], ..., a[r - 1])) = false
If f is monotone, this is the minimum l that satisfies f(op(a[l], a[l + 1], ..., a[r - 1])) = true.
@{keyword.constraints}
- if
fis called with the same argument, it returns the same value, i.e.,fhas no side effect. f(e()) = true
@{keyword.complexity}
String
It contains string algorithms.
Let s be a string.
We denote the substring of s between -th and -th character by s[a..b).
suffix_array
(1) vector<int> suffix_array(string s)
(2) vector<int> suffix_array<T>(vector<T> s)
(3) vector<int> suffix_array(vector<int> s, int upper)Given a string s of length , it returns the suffix array of s.
Here, the suffix array sa of s is a permutation of such that s[sa[i]..n) < s[sa[i+1]..n) holds for all .
@{keyword.constraints}
- (2)
Tisint,uint,ll, orull. - (3)
- (3) for all elements of .
Complexity
- (1) -time
- (2) -time, -space
- (3) -time
lcp_array
(1) vector<int> lcp_array(string s, vector<int> sa)
(2) vector<int> lcp_array<T>(vector<T> s, vector<int> sa)Given a string s of length , it returns the LCP array of s.
Here, the LCP array of s is the array of length , such that the -th element is the length of the LCP (Longest Common Prefix) of s[sa[i]..n) and s[sa[i+1]..n).
@{keyword.constraints}
sais the suffix array ofs.- (2):
Tisint,uint,ll, orull.
@{keyword.complexity}
z_algorithm
(1) vector<int> z_algorithm(string s)
(2) vector<int> z_algorithm<T>(vector<T> s)Given a string of length , it returns the array of length , such that the -th element is the length of the LCP (Longest Common Prefix) of s[0..n) and s[i..n).
@{keyword.constraints}
- (2):
Tisint,uint,ll, orull
@{keyword.complexity}
2-SAT
It solves 2-SAT.
For variables and clauses with form
- ,
it decides whether there is a truth assignment that satisfies all clauses.
Constructor
two_sat ts(int n)It creates a 2-SAT of variables and clauses.
@{keyword.constraints}
@{keyword.complexity}
add_clause
void ts.add_clause(int i, bool f, int j, bool g)It adds a clause .
@{keyword.constraints}
@{keyword.complexity}
- amortized
satisfiable
bool ts.satisfiable()If there is a truth assignment that satisfies all clauses, it returns true. Otherwise, it returns false.
@{keyword.constraints}
- You may call it multiple times.
@{keyword.complexity}
- , where is the number of added clauses.
answer
vector<bool> ts.answer()It returns a truth assignment that satisfies all clauses of the last call of satisfiable.
If we call it before calling satisfiable or when the last call of satisfiable returns false, it returns the vector of length with undefined elements.
@{keyword.complexity}