Skip to content
Snippets Groups Projects
Commit 8b25fdc8 authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei
Browse files

Add descriptions and improve latex

parent f6e37529
No related branches found
No related tags found
No related merge requests found
/// Heavy Light Decomposition /// Heavy Light Decomposition
/// ///
/// Description:
/// The Heavy Light Decomposition splits a tree into several path so that we
/// can reach the root from any node by traversing at most $\log n$ paths. For
/// every vertice $v$, an edge is called heavy if it leads to a child with the
/// largest subtree size; all other edges are called light. \par
/// The set of disjoint paths containing heavy edges are called the ``heavy
/// chains''. These paths (both heavy and light) can be indexed by a single
/// segment tree, allowing fast queries and updates to edges (or vertices). And
/// also, information about the chains (such as the head), allows for fast
/// traversals from any node to any of its ancestors.
///
/// Time: /// Time:
/// - build: O(n log n) /// - build: O(n log n)
/// - query: O(log^2 n) /// - query: O(log^2 n)
...@@ -7,7 +18,7 @@ ...@@ -7,7 +18,7 @@
/// Space: O(n log n) /// Space: O(n log n)
/// ///
/// Caution: /// Caution:
/// - Modifications are necessary if values are associated to vertices; /// - Modifications are necessary if values are associated with vertices;
/// - Modifications are necerrary if other data structure is used. /// - Modifications are necerrary if other data structure is used.
/// ///
/// Status: Tested (QTREE,URI2887) /// Status: Tested (QTREE,URI2887)
......
...@@ -15,10 +15,11 @@ struct Kruskal { ...@@ -15,10 +15,11 @@ struct Kruskal {
Kruskal(int N) : N(N), ds(N) {} Kruskal(int N) : N(N), ds(N) {}
int run(vector<iii> &mst) { // Minimum Spanning Tree: comp = less<int>()
// Maximum Spanning Tree: comp = greater<int>()
int run(vector<iii> &mst, function<bool(int,int)> comp) {
sort(all(edges), [&](const iii &a, const iii &b) { sort(all(edges), [&](const iii &a, const iii &b) {
// ('>' for maximum spanning tree) return comp(a.se, b.se);
return a.se < b.se;
}); });
int size = 0; int size = 0;
......
...@@ -4,13 +4,13 @@ ...@@ -4,13 +4,13 @@
/// The LCA between two nodes in a tree is a node that is an ancestor to both /// The LCA between two nodes in a tree is a node that is an ancestor to both
/// nodes with the lowest height possible. \par /// nodes with the lowest height possible. \par
/// The algorithm works by following the path up the tree from both nodes /// The algorithm works by following the path up the tree from both nodes
/// "simultaneously" until a common node is found. The naive approach for that /// ``simultaneously'' until a common node is found. The naive approach for
/// would be $O(n)$ in the worst case. To improve that, this implementation /// that would be $O(n)$ in the worst case. To improve that, this
/// uses "binary lifting" which is a way of figuring out the right number of /// implementation uses ``binary lifting'' which is a way of figuring out the
/// up-moves needed to find the LCA by following the binary representation of /// right number of up-moves needed to find the LCA by following the binary
/// the distance to the destination (similar to the "binary search by jumping"), /// representation of the distance to the destination (similar to the ``binary
/// but, for that, a preprocessing must be done to set every parent at a $2^i$ /// search by jumping''), but, for that, a preprocessing must be done to set
/// distance. /// every parent at a $2^i$ distance.
/// ///
/// Time: /// Time:
/// - preprocess: O(V log V) /// - preprocess: O(V log V)
......
...@@ -2,13 +2,13 @@ ...@@ -2,13 +2,13 @@
/// ///
/// Space: O(n) /// Space: O(n)
/// ///
/// Status: Tested (KRAKOVIA)
//
/// Caution: /// Caution:
/// - Just use python. /// - Just use Python if possible.
// ///
/// Include: /// Include:
/// - math/karatsuba /// - math/karatsuba
///
/// Status: Tested (KRAKOVIA)
const int base = 1000000000; const int base = 1000000000;
const int base_d = 9; const int base_d = 9;
......
/// Binary Exponentiation /// Binary Exponentiation
/// ///
/// Description:
/// Computes fast exponentiation by looking a the binary representation of
/// the exponent. The usage of ``bin\_mul'' is not necessary, but it ensures
/// that no overflow will occur when multiplying two large integers. It is
/// definitely required in order to work with Miller-Rabin and Pollard's
/// Rho implementations (when dealing with numbers that might go up to
/// $10^{18}$.
///
/// Time: O(log n) /// Time: O(log n)
/// Space: O(1) /// Space: O(1)
......
/// Legendre's Formula /// Legendre's Formula
/// ///
/// Description:
/// Given an interger $n$ and a prime number $p$, find the largest power of
/// $p$ ($x$) such that $n!$ is divisible by $k^x$. \par
/// Writing $n!$ as $1 \cdot 2 \cdot 3 ... (n-1) \cdot n$ shows that every
/// $k$-th element of the product is divisible by $k$ (i.e. adds $1$ to the
/// answer), the number of such elements is $\lfloor\frac{n}{k}\rfloor$. The
/// same is true for every $k^i$, thus, the answer is given by:
/// \[ \lfloor\frac{n}{k}\rfloor + \lfloor\frac{n}{k^2}\rfloor + ... +
/// \lfloor\frac{n}{k^i}\rfloor \] only approximately the first $log_p n$
/// elements are greater than zero, therefore the sum is finite.
///
/// Time: O(log_p n) /// Time: O(log_p n)
/// Space: O(1) /// Space: O(1)
......
/// Linear Diophantine Equation /// Linear Diophantine Equation
/// ///
/// Description: /// Description:
/// A Linear Diophantine Equation is an equation in the form $ax + by = c$. /// A Linear Diophantine Equation is an equation in the form \[ ax + by = c \]
/// A solution of this equation is a pair $(x,y)$ that satisfies the equation. /// A solution of this equation is a pair $(x,y)$ that satisfies the equation.
/// The locus of (lattice) points whose coordinates $x$ and $y$ satisfy the /// The locus of (lattice) points whose coordinates $x$ and $y$ satisfy the
/// equation is a straigh line. \par /// equation is a straigh line. \par
......
/// Modular Multiplicative Inverse /// Modular Multiplicative Inverse
/// ///
/// Description:
/// Given integers $a$ and $m$, the modular multiplicative inverse of $a$ is
/// an integer $x$ such that \[ a \cdot x \equiv 1 \pmod{m} \] also denoted as
/// $a^{-1}$.
///
/// Time: O(log m) /// Time: O(log m)
/// Space: O(1) /// Space: O(1)
......
/// Ternary Search /// Ternary Search
/// ///
/// Description:
/// Searches for the minimum (or maximum) value of a unimodal function
/// $f(x)$ for $x$ between $l$ and $r$.
///
/// Time: O(log n) /// Time: O(log n)
/// Space: O(1) /// Space: O(1)
///
/// Caution:
/// - f must be a unimodal function
// Unimodal function // Minimum of f: comp = less<T>()
double f(double x) { // Maximum of f: comp = greater<T>()
return x * x; template <typename T, class F = function<T(T)>>
} T ternary_search(T l, T r, F f, function<bool(T,T)> comp) {
T rt, lt;
double ternary_search(double l, double r) {
double rt, lt;
for (int i = 0; i < 500; ++i) { for (int i = 0; i < 500; ++i) {
if (fabs(r - l) < EPS) if (fabs(r - l) < EPS)
return (l + r) / 2.0; return (l + r) / 2;
lt = (r - l) / 3.0 + l; lt = (r - l) / 3 + l;
rt = ((r - l) * 2.0) / 3.0 + l; rt = ((r - l) * 2) / 3 + l;
// '<' for minimum of f, if (comp(f(lt), f(rt)))
// '>' for maximum of f
if (f(lt) < f(rt))
l = lt; l = lt;
else else
r = rt; r = rt;
} }
return (l + r) / 2.0; return (l + r) / 2;
} }
...@@ -9,7 +9,9 @@ ...@@ -9,7 +9,9 @@
/// Time: O(n) /// Time: O(n)
/// Space: O(n) /// Space: O(n)
string booth(string s) { // Maximal: func = greater<char>()
// Minimal: func = less<char>()
string booth(string s, function<bool(char,char)> func) {
string S = s + s; string S = s + s;
vector<int> f(S.size(), -1); vector<int> f(S.size(), -1);
...@@ -19,13 +21,13 @@ string booth(string s) { ...@@ -19,13 +21,13 @@ string booth(string s) {
int i = f[j - k - 1]; int i = f[j - k - 1];
while (i != -1 && sj != S[k+i+1]) { while (i != -1 && sj != S[k+i+1]) {
if (sj < S[k + i + 1]) // >: maximal if (func(sj, S[k + i + 1]))
k = j - i - 1; k = j - i - 1;
i = f[i]; i = f[i];
} }
if (sj != S[k+i+1]) { if (sj != S[k+i+1]) {
if (sj < S[k]) // >: maximal if (func(sj, S[k]))
k = j; k = j;
f[j-k] = -1; f[j-k] = -1;
......
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
/// ///
/// Time: O((n + q) * sqrt{n}) /// Time: O((n + q) * sqrt{n})
/// Space: O(n + q) /// Space: O(n + q)
///
/// Caution:
/// - Remember to implement add, remove, and get\_ans functions.
struct Query { struct Query {
int l, r, idx; int l, r, idx;
......
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
/// - insert: O(M) /// - insert: O(M)
/// - search: O(M) /// - search: O(M)
/// Space: O(alph * N) /// Space: O(alph * N)
///
/// Caution:
/// - If Trie stores integers, remember to check order of bits (most/least significant first).
template <typename T> template <typename T>
struct Trie { struct Trie {
......
No preview for this file type
...@@ -117,7 +117,7 @@ class LatexGenerator: ...@@ -117,7 +117,7 @@ class LatexGenerator:
def to_big_o(expr): def to_big_o(expr):
expr = expr.replace('O', '\\mathcal{O}') expr = expr.replace('O', '\\mathcal{O}')
expr = expr.replace('log', '\\log') expr = expr.replace('log', '\\log')
expr = expr.replace('*', '\\times') expr = expr.replace('*', '\\cdot')
expr = expr.replace('sqrt', '\\sqrt') expr = expr.replace('sqrt', '\\sqrt')
return expr return expr
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment