diff --git a/algorithms/geometry/convex_hull.cpp b/algorithms/geometry/convex_hull.cpp index e396b6eb635efce33ad0e0067adbb61010400c1e..385259c66e44e2520cb68d83a786e347f15be681 100644 --- a/algorithms/geometry/convex_hull.cpp +++ b/algorithms/geometry/convex_hull.cpp @@ -1,7 +1,7 @@ /// Convex Hull /// -/// Complexity (Time): O(n log n) -/// Complexity (Space): O(n) +/// Time: O(n log n) +/// Space: O(n) struct ConvexHull { using point = pair<double,double>; diff --git a/algorithms/geometry/geometry_functions.cpp b/algorithms/geometry/geometry_functions.cpp index b3884c1382f951ecf83fed292b46f85bbfaff1fd..2fc624ce179c246bcd322d16a20fb98f6743ae9e 100644 --- a/algorithms/geometry/geometry_functions.cpp +++ b/algorithms/geometry/geometry_functions.cpp @@ -1,42 +1,41 @@ /// Geometry Functions +#define to_deg(x) ((x * 180.0) / M_PI) + template <typename T> struct Point { T x, y; Point(T x, T y) : x(x), y(y) {} - Point operator+(Point p) { return Point(x + p.x, y + p.y); } - Point operator-(Point p) { return Point(x - p.x, y - p.y); } + Point operator+(Point p) { return Point(x+p.x, y+p.y); } + Point operator-(Point p) { return Point(x-p.x, y-p.y); } - T dot(Point p) { return (x * p.x) + (y * p.y); } - T cross(Point p) { return (x * p.y) - (y * p.y); } + T dot(Point p) { return (x*p.x) + (y*p.y); } + T cross(Point p) { return (x*p.y) - (y*p.y); } // Returns angle between this and p: // atan2(y, x) is in the range [-180,180]. To get [0, 360], // atan2(-y, -x) + 180 is used T angle(Point p) { - return ((atan2(-cross(p), -dot(p)) * 180.0) / M_PI) + 180.0; + return to_deg(atan2(-cross(p), -dot(p))) + 180.0; } // Returns cosine value between this and p. T cosine(Point p) { - return (dot(p) / (sqrt(dot(*this)) * sqrt(p.dot(p)))); + return (dot(p) / (sqrt(dot(*this))*sqrt(p.dot(p)))); } // Returns sine value between this and p. T sine(Point p) { - return (cross(p) / (sqrt(dot(*this)) * sqrt(p.dot(p)))); + return (cross(p) / (sqrt(dot(*this))*sqrt(p.dot(p)))); } // Finds orientation of ordered triplet (a,b,c). // Colinear (0), Clockwise (1), Counterclockwise (2) static int orientation(Point a, Point b, Point c) { T val = (b - a).cross(c - b); - - if (val == 0) - return 0; - + if (val == 0) return 0; return (val > 0) ? 1 : 2; } }; @@ -47,19 +46,21 @@ struct Segment { Segment(Point a, Point b) : a(a), b(b) {} - // Checks if points p and q are on the same side of the segment. + // Checks if points p and q are on the same side + // of the segment. bool same_side(Point p, Point q) { T cpp = (p - a).cross(b - a); T cpq = (q - a).cross(b - a); - return ((cpp > 0 && cpq > 0) || (cpp < 0 && cpq < 0)); + return ((cpp > 0 && cpq > 0) || + (cpp < 0 && cpq < 0)); } // Checks if point p is on the segment. bool on_segment(Point p) { return (p.x <= max(a.x, b.x) && - p.x >= min(a.x, b.x) && - p.y <= max(a.y, b.y) && - p.y >= min(a.y, b.y)); + p.x >= min(a.x, b.x) && + p.y <= max(a.y, b.y) && + p.y >= min(a.y, b.y)); } // Checks if segment intersects with s. @@ -83,20 +84,20 @@ struct Segment { template <typename T> struct Polygon { - vector<Point<T>> vertices; + vector<Point<T>> v; Polygon() {} - Polygon(vector<Point> vertices) : vertices(vertices) {} + Polygon(vector<Point> v) : v(v) {} // Adds a vertex to the polygon. - void add_point(Point p) { vertices.pb(p); } + void add_point(Point p) { v.pb(p); } - // Returns area of polygon (only works when vertices are sorted - // in clockwise or counterclockwise order). + // Returns area of polygon (only works when vertices + // are sorted in clockwise or counterclockwise order). double area() { double ans = 0; - for (int i = 0; i < vertices.size(); ++i) - ans += vertices[i].cross(vertices[(i + 1) % vertices.size()]); + for (int i = 0; i < v.size(); ++i) + ans += v[i].cross(v[(i + 1) % v.size()]); return fabs(ans) / 2.0; } diff --git a/algorithms/graph/articulations_bridges.cpp b/algorithms/graph/articulations_bridges.cpp index 4e80bed26d95790f61b0a749ea4081a55a70f569..2fbb2b63120b75c78962e62ced786756e25f39e5 100644 --- a/algorithms/graph/articulations_bridges.cpp +++ b/algorithms/graph/articulations_bridges.cpp @@ -1,7 +1,7 @@ /// Articulations and Bridges - Tarjan /// -/// Complexity (Time): O(V + E) -/// Complexity (Space): O(V + E) +/// Time: O(V + E) +/// Space: O(V + E) vector<int> graph[MAX]; @@ -13,8 +13,7 @@ struct Tarjan { vector<int> arti; Tarjan(int N) : - N(N), vis(N), par(N), L(N), low(N) - {} + N(N), vis(N), par(N), L(N), low(N) {} void init() { fill(all(L), 0); diff --git a/algorithms/graph/bellman_ford.cpp b/algorithms/graph/bellman_ford.cpp index b3b740abebaa02e16d9a5e15ea5e45b27d4b4953..7bb5b3d21a3079d61f4ba79157494060f2bd49ee 100644 --- a/algorithms/graph/bellman_ford.cpp +++ b/algorithms/graph/bellman_ford.cpp @@ -1,7 +1,7 @@ /// Bellman-Ford /// -/// Complexity (Time): O(V*E) -/// Complexity (Space): O(V + E) +/// Time: O(V*E) +/// Space: O(V + E) struct BellmanFord { struct Edge { int u, v, w; }; @@ -11,8 +11,7 @@ struct BellmanFord { vector<Edge> graph; BellmanFord(int N) : - N(N), dist(N) - {} + N(N), dist(N) {} void init() { fill(all(dist), inf); diff --git a/algorithms/graph/bipartite_match.cpp b/algorithms/graph/bipartite_match.cpp index c74383a3be91c164523bbec6be38b5d30aa113f0..dee720862aa0566ae63303b2693a4746bf8a81c7 100644 --- a/algorithms/graph/bipartite_match.cpp +++ b/algorithms/graph/bipartite_match.cpp @@ -1,7 +1,7 @@ /// Bipartite Matching /// -/// Complexity (Time): O(V*E) -/// Complexity (Space): O(V*E) +/// Time: O(V*E) +/// Space: O(V*E) vector<int> graph[MAX]; @@ -10,15 +10,13 @@ struct BipartiteMatching { vector<int> vis, match; BipartiteMatching(int N) : - N(N), vis(N), match(N) - {} + N(N), vis(N), match(N) {} void init() { fill(all(vis), 0); fill(all(match), -1); } - // Finds match for x. int dfs(int x) { if (vis[x]) return 0; @@ -33,12 +31,8 @@ struct BipartiteMatching { return 0; } - // Returns number of left elements in matching and - // fills match array with the match itself - // (match[right_i] = left_i). int run() { int ans = 0; - for (int i = 0; i < N; ++i) ans += dfs(i); diff --git a/algorithms/graph/centroid_decomposition.cpp b/algorithms/graph/centroid_decomposition.cpp index d9aea65298ea1ac1d3f0a0737008178b32979eba..6cace6113f3fab3a6e40e5a528eb61365de90459 100644 --- a/algorithms/graph/centroid_decomposition.cpp +++ b/algorithms/graph/centroid_decomposition.cpp @@ -1,21 +1,18 @@ /// Centroid Decomposition /// -/// Complexity (Time): O(V log V) -/// Complexity (Space): O(V + E) +/// Description: +/// The Centroid Decomposition of a tree is a tree where its root is the +/// centroid of the original tree, and its children are the centroid of each +/// tree resulting from the removal of the root from the original tree. +/// The result is a tree with $lg(n)$ height, where the path from $a$ to $b$ +/// in the original tree can be decomposed into the path from a to lca($a$,$b$) +/// and from lca($a$,$b$) to $b$. +/// This is useful because each one of the $n^2$ paths of the original tree +/// is the concatenation of two paths in a set of $O(n lg(n))$ paths from a +/// node to all its ancestors in the centroid decomposition. /// -/// The Centroid Decomposition of a tree is a tree where: -/// - Its root is the centroid of the original tree. -/// - Its children are the centroid of each tree resulting from the removal -/// of the root from the original tree. -/// -/// The result is a tree with lg(n) height, where the path from a to b in -/// the original tree can be decomposed into the path from a to lca(a,b) and -/// from lca(a,b) to b, where lca(a,b) is the lowest common ancestor of a and b -/// in the centroid decomposition. -/// -/// This is useful because each one of the n^2 paths of the original tree is -/// the concatenation of two paths in a set of O(n lg(n)) paths from a node to -/// all its ancestors in the centroid decomposition. +/// Time: O(V log V) +/// Space: O(V + E) // Must be a tree vector<int> graph[MAX]; diff --git a/algorithms/graph/dijkstra.cpp b/algorithms/graph/dijkstra.cpp index 054ea2eade4658606b383e56bfdf0b8d3cf06ef5..e80f124cc70939b1eefd946705071821199f1ef7 100644 --- a/algorithms/graph/dijkstra.cpp +++ b/algorithms/graph/dijkstra.cpp @@ -1,7 +1,7 @@ /// Dijkstra /// -/// Complexity (Time): O(E + V log V) -/// Complexity (Space): O(V + E) +/// Time: O(E + V log V) +/// Space: O(V + E) vector<int> graph[MAX]; @@ -10,8 +10,8 @@ struct Dijkstra { vector<int> dist, vis; Dijkstra(int N) : - N(N), dist(N), vis(N) - {} + N(N), dist(N), vis(N) + { init(); } void init() { fill(all(vis), 0); @@ -33,12 +33,9 @@ struct Dijkstra { vis[u] = 1; for (auto i : graph[u]) { - int v = i.fi; - int wt = i.se; - - if (!vis[v] && dist[v] > dist[u] + wt) { - dist[v] = dist[u] + wt; - pq.insert(ii(dist[v], v)); + if (!vis[i.fi] && dist[i.fi] > dist[u] + i.se) { + dist[i.fi] = dist[u] + i.se; + pq.insert(ii(dist[i.fi], i.fi)); } } } diff --git a/algorithms/graph/dinic.cpp b/algorithms/graph/dinic.cpp index ca0d17080b200f65c2dc3bdef5ba14a0b4cda557..5925a25ed68c1a297c42f4d7d5157f7f690d8468 100644 --- a/algorithms/graph/dinic.cpp +++ b/algorithms/graph/dinic.cpp @@ -1,7 +1,7 @@ /// Dinic's /// -/// Complexity (Time): O(E*V^2) -/// Complexity (Space): O(V + E) +/// Time: O(E*V^2) +/// Space: O(V + E) struct Dinic { struct Edge { int u, f, c, r; }; @@ -11,10 +11,8 @@ struct Dinic { vector<vector<Edge>> graph; Dinic(int N) : - N(N), depth(N), start(N), graph(N) - {} + N(N), depth(N), start(N), graph(N) {} - // Adds edge (s, t) with capacity c to the graph. void add_edge(int s, int t, int c) { Edge forw = { t, 0, c, graph[t].size() }; Edge back = { s, 0, 0, graph[s].size() }; @@ -64,10 +62,8 @@ struct Dinic { return 0; } - // Returns maximum flow. int run(int s, int t) { int ans = 0; - while (bfs(s, t)) { fill(all(start), 0); diff --git a/algorithms/graph/edmonds_karp.cpp b/algorithms/graph/edmonds_karp.cpp index e00bfa5a6784c41920d964527714575a1b958bee..86d893a10c4f869ce0f6eb2fbc81b344441e9556 100644 --- a/algorithms/graph/edmonds_karp.cpp +++ b/algorithms/graph/edmonds_karp.cpp @@ -1,7 +1,7 @@ /// Edmonds-Karp /// -/// Complexity (time): O(V*E^2) -/// Complexity (space): O(V^2) +/// Time: O(V*E^2) +/// Space: O(V^2) int rg[MAX][MAX]; int graph[MAX][MAX]; @@ -11,8 +11,8 @@ struct EdmondsKarp { vector<int> par, vis; EdmondsKarp(int N) : - N(N), par(N), vis(N), - {} + N(N), par(N), vis(N) + { init(); } void init() { fill(all(vis), 0); @@ -26,7 +26,6 @@ struct EdmondsKarp { while (!Q.empty()) { int u = Q.front(); Q.pop(); - // Sink was found, there is a path if (u == t) return true; @@ -41,7 +40,6 @@ struct EdmondsKarp { return false; } - // Returns maximum flow. int run(int s, int t) { int ans = 0; par[s] = -1; diff --git a/algorithms/graph/floyd_warshall.cpp b/algorithms/graph/floyd_warshall.cpp index 68fb43a19b177c9308f333e6b8964271e27600d5..d84545f831be5a1e548520c9e4f087e692c00db9 100644 --- a/algorithms/graph/floyd_warshall.cpp +++ b/algorithms/graph/floyd_warshall.cpp @@ -1,7 +1,7 @@ /// Floyd Warshall /// -/// Complexity (Time): O(V^3) -/// Complexity (Space): O(V^2) +/// Time: O(V^3) +/// Space: O(V^2) int dist[MAX][MAX]; int graph[MAX][MAX]; @@ -10,10 +10,8 @@ struct FloydWarshall { int N; FloydWarshall(int N) : - N(N) - {} + N(N) {} - // Fills dist matrix with result. int run() { for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) diff --git a/algorithms/graph/ford_fulkerson.cpp b/algorithms/graph/ford_fulkerson.cpp index 7db674b10d1c47bec2ad1d668d563611cdce8a1f..7429f771659c801204c1074862f6bffe1e073f35 100644 --- a/algorithms/graph/ford_fulkerson.cpp +++ b/algorithms/graph/ford_fulkerson.cpp @@ -1,7 +1,7 @@ /// Ford-Fulkerson /// -/// Complexity (time): O(Ef) -/// Complexity (space): O(V^2) +/// Time: O(Ef) +/// Space: O(V^2) int rg[MAX][MAX]; int graph[MAX][MAX]; @@ -12,7 +12,7 @@ struct FordFulkerson { FordFulkerson(int N) : N(N), par(N), vis(N) - {} + { init(); } void init() { fill(all(vis), 0); } @@ -32,7 +32,6 @@ struct FordFulkerson { return false; } - // Returns maximum flow. int run(int s, int t) { int ans = 0; par[s] = -1; diff --git a/algorithms/graph/hopcroft_karp.cpp b/algorithms/graph/hopcroft_karp.cpp index 877469dfabfe9563ab25382859b299e286b111c9..f52187a53e2814d8493e397d841ab8a78fb7e3b3 100644 --- a/algorithms/graph/hopcroft_karp.cpp +++ b/algorithms/graph/hopcroft_karp.cpp @@ -1,7 +1,7 @@ /// Hopcroft-Karp /// -/// Complexity (Time): O(E*sqrt(V)) -/// Complexity (Space): O(V + E) +/// Time: O(E*sqrt(V)) +/// Space: O(V + E) vector<int> graph[MAX]; @@ -13,7 +13,7 @@ struct HopcroftKarp { HopcroftKarp(int L, int R) : L(L), R(R), dist(L), matchL(L), matchR(R) - {} + { init(); } void init() { fill(all(matchL), 0); @@ -23,28 +23,24 @@ struct HopcroftKarp { bool bfs() { queue<int> Q; - // Add unmatched vertices in L to the queue - for (int l = 1; l <= L; ++l) { + for (int l = 1; l <= L; ++l) if (matchL[l] == 0) { dist[l] = 0; Q.push(l); } else { dist[l] = inf; } - } dist[0] = inf; while (!Q.empty()) { int l = Q.front(); Q.pop(); - if (dist[l] < dist[0]) { - for (auto r : graph[l]) { + if (dist[l] < dist[0]) + for (auto r : graph[l]) if (dist[matchR[r]] == inf) { dist[matchR[r]] = dist[l] + 1; Q.push(matchR[r]); } - } - } } return (dist[0] != inf); @@ -54,30 +50,25 @@ struct HopcroftKarp { if (l == 0) return true; - for (auto r : graph[l]) { - if (dist[matchR[r]] == dist[l] + 1) { + for (auto r : graph[l]) + if (dist[matchR[r]] == dist[l] + 1) if (dfs(matchR[r])) { matchR[r] = l; matchL[l] = r; return true; } - } - } dist[l] = inf; return false; } - // Returns number of matched vertices on the left - // (matched edges). int run() { int ans = 0; - while (bfs(L)) { + while (bfs(L)) for (int l = 1; l <= L; ++l) if (matchL[l] == 0 && dfs(l)) ans++; - } return ans; } diff --git a/algorithms/graph/kosaraju.cpp b/algorithms/graph/kosaraju.cpp index d0ccd4ab9f70d0dec5e606316f84c450cf22e105..3d2ff621e72ec80fb702dfbcca3a22d6c2e0d498 100644 --- a/algorithms/graph/kosaraju.cpp +++ b/algorithms/graph/kosaraju.cpp @@ -1,7 +1,7 @@ /// Kosaraju /// -/// Complexity (Time): O(V + E) -/// Complexity (Space): O(V + E) +/// Time: O(V + E) +/// Space: O(V + E) vector<int> graph[MAX]; vector<int> transp[MAX]; @@ -11,7 +11,9 @@ struct Kosaraju { stack<int> S; vector<int> vis; - Kosaraju(int N) : N(N), vis(N) {} + Kosaraju(int N) : + N(N), vis(N) + { init(); } void init() { fill(all(vis), 0); } diff --git a/algorithms/graph/kruskal.cpp b/algorithms/graph/kruskal.cpp index a48ee1fcf85a7e2b539ed238c60022c6f31fbff2..b657194cac1cf87e2d8e18e7cdb4f41820846cb3 100644 --- a/algorithms/graph/kruskal.cpp +++ b/algorithms/graph/kruskal.cpp @@ -1,9 +1,10 @@ /// Kruskal /// -/// Complexity (Time): O (E log V) -/// Complexity (Space): O(E) +/// Time: O (E log V) +/// Space: O(E) /// -/// #include <structure/disjoint_set> +/// Include: +/// - structure/disjoint_set typedef pair<ii,int> iii; vector<iii> edges; diff --git a/algorithms/graph/lca.cpp b/algorithms/graph/lca.cpp index a1ee78e18a5bdc19ecaf41732a470684819cc631..9ea2aea626b27beb0d55c832ee1d6f14c53648a9 100644 --- a/algorithms/graph/lca.cpp +++ b/algorithms/graph/lca.cpp @@ -1,9 +1,9 @@ /// Lowest Common Ancestor (LCA) /// -/// Complexity (Time): +/// Time: /// - preprocess: O(V log V) /// - query: O(log V) -/// Complexity (Space): O(V + E + V log V) +/// Space: O(V + E + V log V) #define MAXLOG 20 //log2(MAX) diff --git a/algorithms/graph/prim.cpp b/algorithms/graph/prim.cpp index 729cf83748f429b5b8892ec3142892c9f6668bac..3c298730b08315494ded9fe514175467f779cc56 100644 --- a/algorithms/graph/prim.cpp +++ b/algorithms/graph/prim.cpp @@ -1,7 +1,7 @@ /// Prim /// -/// Complexity (Time): O(E log E) -/// Complexity (Space): O(V + E) +/// Time: O(E log E) +/// Space: O(V + E) vector<ii> graph[MAX]; @@ -11,7 +11,7 @@ struct Prim { Prim(int N) : N(N), vis(N) - {} + { init(); } void init() { fill(all(vis), 0); diff --git a/algorithms/graph/tarjan.cpp b/algorithms/graph/tarjan.cpp index 61cd3ec413f145f4b9b8cf213f0fee42164339c0..0232ee946f81690da7be745cf45b22e024ab2bdf 100644 --- a/algorithms/graph/tarjan.cpp +++ b/algorithms/graph/tarjan.cpp @@ -1,7 +1,7 @@ /// Tarjan - Strongly Connected Components (SCC) /// -/// Complexity (Time): O(V + E) -/// Complexity (Space): O(V + E) +/// Time: O(V + E) +/// Space: O(V + E) vector<int> scc[MAX]; vector<int> graph[MAX]; @@ -14,7 +14,7 @@ struct Tarjan { Tarjan(int N) : N(N), vis(N), id(N), low(N) - {} + { init(); } void init() { fill(all(id), -1); diff --git a/algorithms/graph/topological_sort.cpp b/algorithms/graph/topological_sort.cpp index 3bbf5edc13ff791a298dd61be90b4a23d2859968..43ae3e7721afd5ecb1be1c9229cfbf1a6f335829 100644 --- a/algorithms/graph/topological_sort.cpp +++ b/algorithms/graph/topological_sort.cpp @@ -1,7 +1,7 @@ /// Topological Sort /// -/// Complexity (Time): O(V + E) -/// Complexity (Space): O(V + E) +/// Time: O(V + E) +/// Space: O(V + E) vector<int> graph[MAX]; @@ -10,7 +10,9 @@ struct TopologicalSort { stack<int> S; vector<int> vis; - TopologicalSort(int N) : N(N), vis(N) {} + TopologicalSort(int N) : + N(N), vis(N) + { init(); } void init() { fill(all(vis), 0); } @@ -18,10 +20,8 @@ struct TopologicalSort { vis[x] = 1; for (auto i : graph[x]) { - if (vis[i] == 1) - return true; - if (!vis[i] && dfs(i)) - return true; + if (vis[i] == 1) return true; + if (!vis[i] && dfs(i)) return true; } vis[x] = 2; diff --git a/algorithms/math/big_integer.cpp b/algorithms/math/big_integer.cpp index 3e21ec2ceb539630595b327c4e42a673cf5f79f5..3e9a062659b10daa5dd255e8d4e4d64c02ba34ea 100644 --- a/algorithms/math/big_integer.cpp +++ b/algorithms/math/big_integer.cpp @@ -1,6 +1,7 @@ /// Big Integer /// -/// Complexity (space): O(n) +/// Space: O(n) + #include <bits/stdc++.h> #define EPS 1e-6 diff --git a/algorithms/math/binary_exponentiation.cpp b/algorithms/math/binary_exponentiation.cpp index 65bca42c18c020dedb1a69ae164fa96ccbb559b6..032382055cb6b8b621f1c1aa6f5bc4d0c1896779 100644 --- a/algorithms/math/binary_exponentiation.cpp +++ b/algorithms/math/binary_exponentiation.cpp @@ -1,7 +1,7 @@ /// Binary Exponentiation /// -/// Complexity (Time): O(log n) -/// Complexity (Space): O(1) +/// Time: O(log n) +/// Space: O(1) struct BinaryExponentiation { ll fast_pow(ll x, ll n) { diff --git a/algorithms/math/euler_totient.cpp b/algorithms/math/euler_totient.cpp index ee56b381700c5a88b935fd5ec4808a0438ecf7b6..97b0320ca2c9525ef00ee8292a8d9b6fcf4b0058 100644 --- a/algorithms/math/euler_totient.cpp +++ b/algorithms/math/euler_totient.cpp @@ -1,7 +1,7 @@ /// Euler Totient ($\phi$) /// -/// Complexity (time): O(sqrt(n)) -/// Complexity (space): O(1) +/// Time: O(sqrt(n)) +/// Space: O(1) struct EulerTotient { int run(int n) { diff --git a/algorithms/math/fft.cpp b/algorithms/math/fft.cpp index d0a9399c46c0919e12447527015824138f3fca10..7d87d2ffbadcdee2f25fe8ec49934a8887dcfbf8 100644 --- a/algorithms/math/fft.cpp +++ b/algorithms/math/fft.cpp @@ -1,7 +1,7 @@ /// Fast Fourier Transform (FFT) /// -/// Complexity (Time): O(N log N) -/// Complexity (Space): O(N) +/// Time: O(N log N) +/// Space: O(N) struct FFT { struct Complex { diff --git a/algorithms/math/linear_recurrence.cpp b/algorithms/math/linear_recurrence.cpp index baaf5247971bb882264dd2e0dab491618015c880..214ea425c7cef4312e3461f4e69b00e2a094257b 100644 --- a/algorithms/math/linear_recurrence.cpp +++ b/algorithms/math/linear_recurrence.cpp @@ -1,26 +1,12 @@ /// Linear Recurrence /// -/// Complexity (Time): O(log n) -/// Complexity (Space): O(1) +/// Time: O(log n) +/// Space: O(1) /// -/// #include "math/binary_exponentiation.cpp" -/// #include "math/matrix.cpp" +/// Include: +/// - math/binary_exponentiation +/// - math/matrix -/// Solves f(n) = x * f(n - 1) + y * f(n - 2) -/// This algorithm is used to solve recurrences such as: -/// f(n) = x1 * f(n - 1) + x2 * f(n - 1) + ... + xk * f(n - k) -/// -/// It works by defining this recurrence as a linear combination, -/// for example (k = 2): -/// f(n) = [x1 x2] [f(n - 1)] -/// [f(n - 2)] -/// It can be rewriten as: -/// [ f(n) ] = [x1 x2] [f(n - 1)] -/// [f(n - 1)] [ 1 0] [f(n - 2)] -/// -/// And that is solved by calculating the following matrix power: -/// [x1 x2]^n -/// [ 1 0] template <typename T> matrix<T> solve(ll x, ll y, ll n) { matrix<T> in(2); diff --git a/algorithms/math/matrix.cpp b/algorithms/math/matrix.cpp index 11c2ed3ffe68f9d5d8b67ba70c9c19a489a02d85..fa67642a595ed9e8809b29a1062981d469828ec7 100644 --- a/algorithms/math/matrix.cpp +++ b/algorithms/math/matrix.cpp @@ -1,4 +1,6 @@ /// Matrix +/// +/// Space: O(r*c) template <typename T> struct matrix { diff --git a/algorithms/math/modular_multiplicative_inverse.cpp b/algorithms/math/modular_multiplicative_inverse.cpp index a602bb4218b8675289637f340c29407ec9d06810..e52acd0581c069baa27a340306d3c5638c5178b2 100644 --- a/algorithms/math/modular_multiplicative_inverse.cpp +++ b/algorithms/math/modular_multiplicative_inverse.cpp @@ -1,7 +1,7 @@ /// Modular Multiplicative Inverse /// -/// Complexity (Time): O(log m) -/// Complexity (Space): O(1) +/// Time: O(log m) +/// Space: O(1) /// ========== Fermat's Little Theorem ========== /// Used when m is prime diff --git a/algorithms/math/sieve_of_eratosthenes.cpp b/algorithms/math/sieve_of_eratosthenes.cpp index 94f0cd92dd5b456a33506ee0eca8f8f47245c68e..1bcd2c51b73176ee94959dd170934b21dc079264 100644 --- a/algorithms/math/sieve_of_eratosthenes.cpp +++ b/algorithms/math/sieve_of_eratosthenes.cpp @@ -1,13 +1,15 @@ /// Sieve of Eratosthenes /// -/// Complexity (Time): O(n*log(log(n))) -/// Complexity (Space): O(n) +/// Time: O(n*log(log(n))) +/// Space: O(n) struct Sieve { int N; vector<int> is_prime; - Sieve(int N) : N(N), is_prime(N+1) {} + Sieve(int N) : + N(N), is_prime(N+1) + { init(); } void init() { fill(all(is_prime), 1); diff --git a/algorithms/paradigm/edit_distance.cpp b/algorithms/paradigm/edit_distance.cpp index dbafd1b05c2f884b9d2eee9ee0eba6a415681dcb..5ac6c22efbc215f87630db5bf8197c0f1603c1d7 100644 --- a/algorithms/paradigm/edit_distance.cpp +++ b/algorithms/paradigm/edit_distance.cpp @@ -1,14 +1,13 @@ /// Edit Distance /// -/// Complexity (Time): O(m*n) -/// Complexity (Space): O(m*n) +/// Time: O(m*n) +/// Space: O(m*n) struct EditDistance { vector<vector<int>> dp; EditDistance(int N, int M) : - dp(N, vector<int>(M)) - {} + dp(N, vector<int>(M)) {} int run(string a, string b) { for (int i = 0; i <= a.size(); ++i) diff --git a/algorithms/paradigm/kadane.cpp b/algorithms/paradigm/kadane.cpp index be9dcf6e7268f5163205cd163b3ed1d04a3b5ecc..c1458d0de600b6066f4836899a258602edd306a6 100644 --- a/algorithms/paradigm/kadane.cpp +++ b/algorithms/paradigm/kadane.cpp @@ -1,7 +1,7 @@ /// Kadane /// -/// Complexity (Time): O(n + m) -/// Complexity (Space): O(n + m) +/// Time: O(n + m) +/// Space: O(n + m) struct Kadane { int run(const vector<int> &v, int &start, int &end) { diff --git a/algorithms/paradigm/lis.cpp b/algorithms/paradigm/lis.cpp index e7e289d50ad21887ed7f6137eb5fe04130fb99e7..281adfcca41fe9b69db96a91a83971a214d87305 100644 --- a/algorithms/paradigm/lis.cpp +++ b/algorithms/paradigm/lis.cpp @@ -1,7 +1,7 @@ /// Longest Increasing Subsequence (LIS) /// -/// Complexity (Time): O(n^2) -/// Complexity (Space): O(n) +/// Time: O(n^2) +/// Space: O(n) struct LIS { int run(vector<int> v) { diff --git a/algorithms/paradigm/ternary_search.cpp b/algorithms/paradigm/ternary_search.cpp index e90f8ff23f3a3c45b40f4e190a8aec8a3fec7565..ae1c0b759477da5bd345c0599b67ea0464a307ab 100644 --- a/algorithms/paradigm/ternary_search.cpp +++ b/algorithms/paradigm/ternary_search.cpp @@ -1,7 +1,7 @@ /// Ternary Search /// -/// Complexity (Time): O(log n) -/// Complexity (Space): O(1) +/// Time: O(log n) +/// Space: O(1) struct TernarySearch { diff --git a/algorithms/string/kmp.cpp b/algorithms/string/kmp.cpp index 8f05cd1fc1b0fa281d1dd8d3b559a0e383aee159..48d2219648444e6a63baf176e936961bbe587932 100644 --- a/algorithms/string/kmp.cpp +++ b/algorithms/string/kmp.cpp @@ -1,9 +1,9 @@ /// Knuth-Morris-Pratt (KMP) /// -/// Complexity (Time): -/// preprocess -> O(m) -/// search -> O(n) -/// Complexity (Space): O(n + m) +/// Time: +/// - preprocess: O(m) +/// - search: O(n) +/// Space: O(n + m) struct KMP { string patt; diff --git a/algorithms/string/z-function.cpp b/algorithms/string/z-function.cpp index 678df3e5608f4ec8409d9a20df0826b01786466a..593c054358018f9041aa01492cdc06d1b0cecd62 100644 --- a/algorithms/string/z-function.cpp +++ b/algorithms/string/z-function.cpp @@ -1,7 +1,7 @@ /// Z-function /// -/// Complexity (time): O(n) -/// Complexity (space): O(n) +/// Time: O(n) +/// Space: O(n) struct ZFunction { vector<int> run(string s) { diff --git a/algorithms/structure/avl.cpp b/algorithms/structure/avl.cpp index 1a91ecd4a117b95aa2de8cdeb7233e622b7b3b99..de4cfcc1552f08c2f10b60888241ba4145eb1f6d 100644 --- a/algorithms/structure/avl.cpp +++ b/algorithms/structure/avl.cpp @@ -1,7 +1,7 @@ /// AVL tree /// -/// Complexity (Time): O(log n) -/// Complexity (Space): O(n) +/// Time: O(log n) +/// Space: O(n) struct AVL { struct Node { diff --git a/algorithms/structure/ball_tree.cpp b/algorithms/structure/ball_tree.cpp index c4cadce88aed9bef7c5b6ce721f7737a2654c7ed..687f296f25fd9744c191431a36fd5a61ee836039 100644 --- a/algorithms/structure/ball_tree.cpp +++ b/algorithms/structure/ball_tree.cpp @@ -1,7 +1,7 @@ /// Balltree /// -/// Complexity (Time): O(n log n) -/// Complexity (Space): O(n) +/// Time: O(n log n) +/// Space: O(n) #define x first #define y second diff --git a/algorithms/structure/bit.cpp b/algorithms/structure/bit.cpp index 5db0e383560535e0504274ed80b1bb1afaefe05b..ba8e8d3a6891eba186cc0a64db48de81cb3314ae 100644 --- a/algorithms/structure/bit.cpp +++ b/algorithms/structure/bit.cpp @@ -1,15 +1,17 @@ /// Binary Indexed Tree (BIT) /// -/// Complexity (Time): -/// Update -> O(log n) -/// Query -> O(log n) -/// Complexity (Space): O(n) +/// Time: +/// - update: O(log n) +/// - query: O(log n) +/// Space: O(n) struct BIT { int N; vector<int> tree; - BIT(int N) : N(N), tree(N) {} + BIT(int N) : + N(N), tree(N) + { init(); } void init() { fill(all(tree), 0); } diff --git a/algorithms/structure/bit2d.cpp b/algorithms/structure/bit2d.cpp index 748f77cd368153e37bd35f8bd0c4407223e0ebb1..3158ab251ae707dfa79ed260c5bbd6f6b4d8e59d 100644 --- a/algorithms/structure/bit2d.cpp +++ b/algorithms/structure/bit2d.cpp @@ -1,9 +1,9 @@ /// Binary Indexed Tree 2D (BIT2D) /// -/// Complexity (Time): -/// Update -> O(log^2 n) -/// Query -> O(log^2 n) -/// Complexity (Space): O(n^2) +/// Time: +/// - update: O(log^2 n) +/// - query: O(log^2 n) +/// Space: O(n^2) struct BIT2D { int N, M; diff --git a/algorithms/structure/bitmask.cpp b/algorithms/structure/bitmask.cpp index 585ec9e371bcbe832942ed613423ac8bc0252228..bb118c7914c95989a7e61ade8dc5d4d792efb75d 100644 --- a/algorithms/structure/bitmask.cpp +++ b/algorithms/structure/bitmask.cpp @@ -1,7 +1,7 @@ /// Bitmask /// -/// Complexity (Time): O(1) -/// Complexity (Space): O(1) +/// Time: O(1) +/// Space: O(1) struct Bitmask { ll state; diff --git a/algorithms/structure/disjoint_set.cpp b/algorithms/structure/disjoint_set.cpp index 071860b9ce792f58ca0e16c1c1aadb529e923367..176136ec93f344aa5b104dfbcf27c3b832113ef1 100644 --- a/algorithms/structure/disjoint_set.cpp +++ b/algorithms/structure/disjoint_set.cpp @@ -1,10 +1,10 @@ /// Disjoint-set /// -/// Complexity (Time): O(1) -/// make_set -> O(1) -/// find_set -> O(a(n)) -/// union_set -> O(a(n)) -/// Complexity (Space): O(n) +/// Time: O(1) +/// - make_set: O(1) +/// - find_set: O(a(n)) +/// - union_set: O(a(n)) +/// Space: O(n) struct DisjointSet { int N; diff --git a/algorithms/structure/lazy_segment_tree.cpp b/algorithms/structure/lazy_segment_tree.cpp index fdb4c16e0ecb3702640e55f02b8f3df1da195138..5b8f4d0bc0667742af7c9a346919a8553d4586bf 100644 --- a/algorithms/structure/lazy_segment_tree.cpp +++ b/algorithms/structure/lazy_segment_tree.cpp @@ -1,10 +1,10 @@ /// Lazy Segment Tree /// -/// Complexity (Time): -/// build_tree -> O(n log n) -/// update_tree -> O(log n) -/// query_tree -> O(log n) -/// Complexity (Space): O(n) +/// Time: +/// - build_tree: O(n log n) +/// - update_tree: O(log n) +/// - query_tree: O(log n) +/// Space: O(n) int N; struct LazySegmentTree { diff --git a/algorithms/structure/policy_tree.cpp b/algorithms/structure/policy_tree.cpp index a9c8180ad5260df2dc175f7ee3cdbe8239229a6a..e421ffa08ce53a1ac89f61a5c68a5b906a222f98 100644 --- a/algorithms/structure/policy_tree.cpp +++ b/algorithms/structure/policy_tree.cpp @@ -1,13 +1,14 @@ /// Policy Tree /// -/// A set-like STL structure with order statistics +/// Description: +/// A set-like STL structure with order statistics. /// -/// Complexity (Time): -/// insert -> O(log n) -/// erase -> O(log n) -/// find_by_order -> O(log n) -/// order_of_key -> O(log n) -/// Complexity (Space): O(n) +/// Time: +/// - insert: O(log n) +/// - erase: O(log n) +/// - find_by_order: O(log n) +/// - order_of_key: O(log n) +/// Space: O(n) #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> diff --git a/algorithms/structure/segment_tree.cpp b/algorithms/structure/segment_tree.cpp index 4014e047bdcedde5b84a3f43149fb1a1a5674c21..2fe760dad6cdfad4e1cfd1100e131a8664c13565 100644 --- a/algorithms/structure/segment_tree.cpp +++ b/algorithms/structure/segment_tree.cpp @@ -1,9 +1,9 @@ /// Segment Tree /// -/// Complexity (Time): -/// Update -> O(log n) -/// Query -> O(log n) -/// Complexity (Space): O(n) +/// Time: +/// - update: O(log n) +/// - query: O(log n) +/// Space: O(n) struct SegmentTree { int N; diff --git a/algorithms/structure/sqrt_decomposition.cpp b/algorithms/structure/sqrt_decomposition.cpp index 1fb9f273d26bc1bdb245412515d14950d74b991a..69cfd43e7fd48768404251e550ab43ffb95faf9a 100644 --- a/algorithms/structure/sqrt_decomposition.cpp +++ b/algorithms/structure/sqrt_decomposition.cpp @@ -1,10 +1,10 @@ /// Sqrt Decomposition /// -/// Complexity (time): -/// Preprocess -> O(n) -/// Query -> O(sqrt(n)) -/// Update -> O(1) -/// Complexity (space): O(n) +/// Time: +/// - preprocess: O(n) +/// - query: O(sqrt(n)) +/// - update: O(1) +/// Space: O(n) struct SqrtDecomposition { int block_size; diff --git a/algorithms/structure/trie.cpp b/algorithms/structure/trie.cpp index b6542bf25467d60fa6d76ba796a0fabc761076bb..a5053daeb4da554b4cbd30c4d1e8b0765cc0c711 100644 --- a/algorithms/structure/trie.cpp +++ b/algorithms/structure/trie.cpp @@ -1,9 +1,9 @@ /// Trie /// -/// Complexity (Time): -/// Insert -> O(m) -/// Search -> O(m) -/// Complexity (Space): O(alphabet_size * N) +/// Time: +/// - insert: O(M) +/// - search: O(M) +/// Space: O(alph * N) template <typename T> struct Trie { @@ -13,10 +13,10 @@ struct Trie { vector<vector<int>> trie; // Number of words (N) and number of letters per word - // (L), and number of letters in alphabet (alph). - Trie(int N, int L, int alph) : - ending(N * L), - trie(N * L, vector<int>(alph)) + // (M), and number of letters in alphabet (alph). + Trie(int N, int M, int alph) : + ending(N * M), + trie(N * M, vector<int>(alph)) { init(); } void init() { @@ -25,11 +25,17 @@ struct Trie { fill(all(i), -1); } - int idx(string x, int i) { return x[i] - 'a'; } - int idx(int x, int i) { return !!(x & (1 << i)); } + int len(T x) { + if constexpr(is_same_v<T,int>) + return 32; + return x.size(); + } - int len(int x) { return 32; } - int len(string x) { return x.size(); } + int idx(T x) { + if constexpr(is_same_v<T,int>) + return !!(x & (1 << i)); + return x[i] - 'a'; + } void insert(T x) { int node = 0; diff --git a/caderno.pdf b/caderno.pdf new file mode 100644 index 0000000000000000000000000000000000000000..745b2a84ac3c4bd0096dca4b93bf69495e665084 Binary files /dev/null and b/caderno.pdf differ