diff --git a/algorithms/geometry/convex_hull.cpp b/algorithms/geometry/convex_hull.cpp index 4f4f262a758541d6226d34d8b09f0c161e12dbf9..961fd505a0951cddd8d4fbb0249d96a33068d069 100644 --- a/algorithms/geometry/convex_hull.cpp +++ b/algorithms/geometry/convex_hull.cpp @@ -1,26 +1,21 @@ -/** - * Convex Hull - * - * Complexity (Time): O(n log n) - * Complexity (Space): O(n) - */ +/// Convex Hull +/// +/// Complexity (Time): O(n log n) +/// Complexity (Space): O(n) typedef pair<double,double> dd; struct ConvexHull { - /** - * The three points are a counter-clockwise turn if cross > 0, clockwise if - * cross < 0, and collinear if cross = 0 - * @param a,b,c input points - */ + + /// The three points are a counter-clockwise turn if cross > 0, clockwise if + /// cross < 0, and collinear if cross = 0 + /// @param a,b,c input points double cross(dd a, dd b, dd c) { return (b.fi - a.fi) * (c.se - a.se) - (b.se - a.se) * (c.fi - a.fi); } - /** - * Finds, among v, the points that form a convex hull - * @param v vector of points - */ + /// Finds, among v, the points that form a convex hull + /// @param v vector of points int run(const vector<dd> &v) { int k = 0; vector<int> ans(v.size() * 2); diff --git a/algorithms/geometry/geometry_functions.cpp b/algorithms/geometry/geometry_functions.cpp index 0984d21092ec610ea469e9af326c4fd96bab4d97..82a944cd83ea17fac1e1e461db71e412111097c5 100644 --- a/algorithms/geometry/geometry_functions.cpp +++ b/algorithms/geometry/geometry_functions.cpp @@ -1,6 +1,4 @@ -/** - * Geometry functions - */ +/// Geometry functions template <typename T> struct Point { @@ -14,35 +12,27 @@ struct Point { 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 - */ + /// 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; } - /** - * Returns cosine value between this and p. - */ + /// Returns cosine value between this and p. T cosine(Point p) { return (dot(p) / (sqrt(dot(*this)) * sqrt(p.dot(p)))); } - /** - * Returns sine value between this and p. - */ + /// Returns sine value between this and p. T sine(Point p) { return (cross(p) / (sqrt(dot(*this)) * sqrt(p.dot(p)))); } - /** - * Finds orientation of ordered triplet (a,b,c). - * 0 - Colinear; - * 1 - Clockwise; - * 2 - Counterclockwise. - */ + /// Finds orientation of ordered triplet (a,b,c). + /// 0 - Colinear; + /// 1 - Clockwise; + /// 2 - Counterclockwise. static int orientation(Point a, Point b, Point c) { T val = (b - a).cross(c - b); @@ -59,18 +49,14 @@ 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)); } - /** - * Checks if point p is on the segment. - */ + /// 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) && @@ -78,9 +64,7 @@ struct Segment { p.y >= min(a.y, b.y)); } - /** - * Checks if segment intersects with s. - */ + /// Checks if segment intersects with s. bool intersect(Segment s) { int o1 = Point::orientation( a, b, s.a); int o2 = Point::orientation( a, b, s.b); @@ -106,15 +90,11 @@ struct Polygon { Polygon() {} Polygon(vector<Point> vertices) : vertices(vertices) {} - /** - * Adds a vertex to the polygon. - */ + /// Adds a vertex to the polygon. void add_point(Point p) { vertices.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) diff --git a/algorithms/graph/articulations_bridges.cpp b/algorithms/graph/articulations_bridges.cpp index fdd4bae43b5d14dd2d6508ce8fbc883db935f337..d92ff848d069457e1dd2726144a802ce67bab44c 100644 --- a/algorithms/graph/articulations_bridges.cpp +++ b/algorithms/graph/articulations_bridges.cpp @@ -1,9 +1,7 @@ -/** - * Tarjan - Articulations and Bridges - * - * Complexity (Time): O(V + E) - * Complexity (Space): O(V + E) - */ +/// Tarjan - Articulations and Bridges +/// +/// Complexity (Time): O(V + E) +/// Complexity (Space): O(V + E) vector<int> graph[MAX]; @@ -11,10 +9,10 @@ struct Tarjan { int N; vector<int> vis, par, L, low; - // Answer vector with bridges (edges) + /// Answer vector with bridges (edges) vector<ii> brid; - // Answer vector with articulations (vertices) + /// Answer vector with articulations (vertices) vector<int> arti; Tarjan(int N) : @@ -27,10 +25,8 @@ struct Tarjan { fill(all(par), -1); } - /** - * Finds all articulations and bridges in the graph. - * @param x root vertex - */ + /// Finds all articulations and bridges in the graph. + /// @param x root vertex void dfs(int x) { int child = 0; vis[x] = 1; @@ -68,9 +64,7 @@ struct Tarjan { } } - /** - * Applies tarjan algorithm and format articulations vector. - */ + /// Applies tarjan algorithm and format articulations vector. void run() { // Apply tarjan in every component diff --git a/algorithms/graph/bellman_ford.cpp b/algorithms/graph/bellman_ford.cpp index 9554f9201c1e9e32480db979829fb1dbacbfc856..6deb54a5113976c700f059b4119f814355d11f3f 100644 --- a/algorithms/graph/bellman_ford.cpp +++ b/algorithms/graph/bellman_ford.cpp @@ -1,9 +1,7 @@ -/** - * Bellman-Ford - * - * Complexity (Time): O(V*E) - * Complexity (Space): O(V + E) - */ +/// Bellman-Ford +/// +/// Complexity (Time): O(V*E) +/// Complexity (Space): O(V + E) struct Edge { int u, v, w; @@ -27,11 +25,9 @@ struct BellmanFord { fill(all(dist), inf); } - /** - * Returns distance between s and d in a graph with N vertices - * using bellman_ford. - * @param s,d origin and destination vertices - */ + /// Returns distance between s and d in a graph with N vertices + /// using bellman_ford. + /// @param s,d origin and destination vertices int run(int s, int d) { dist[s] = 0; diff --git a/algorithms/graph/bfs.cpp b/algorithms/graph/bfs.cpp index e57b55dce84e6f3c7e2996095b41864ea73691e5..6736fa46d72accaab9d40c65c4be47496efa07e0 100644 --- a/algorithms/graph/bfs.cpp +++ b/algorithms/graph/bfs.cpp @@ -1,9 +1,7 @@ -/** - * Breadth First Search (BFS) - * - * Complexity (Time): O(V + E) - * Complexity (Space): O(V + E) - */ +/// Breadth First Search (BFS) +/// +/// Complexity (Time): O(V + E) +/// Complexity (Space): O(V + E) vector<int> graph[MAX]; @@ -19,10 +17,8 @@ struct BFS { fill(all(vis), 0); } - /** - * Applies BFS on graph. - * @param x starting vertex - */ + /// Applies BFS on graph. + /// @param x starting vertex void run(int x) { queue<int> Q; diff --git a/algorithms/graph/bipartite_match.cpp b/algorithms/graph/bipartite_match.cpp index f5bb7f084926d7a9b091664c9ea1d688b464ec24..304c7dd8ac9df8739dbae68b8491b22a81b9ab21 100644 --- a/algorithms/graph/bipartite_match.cpp +++ b/algorithms/graph/bipartite_match.cpp @@ -1,9 +1,7 @@ -/** - * Bipartite Matching - * - * Complexity (Time): O(V*E) - * Complexity (Space): O(V*E) - */ +/// Bipartite Matching +/// +/// Complexity (Time): O(V*E) +/// Complexity (Space): O(V*E) vector<int> graph[MAX]; @@ -20,10 +18,8 @@ struct BipartiteMatching { fill(all(match), -1); } - /** - * Finds match for x. - * @param x starting vertex - */ + /// Finds match for x. + /// @param x starting vertex int dfs(int x) { if (vis[x]) return 0; @@ -38,11 +34,9 @@ struct BipartiteMatching { return 0; } - /** - * Returns number of left elements in matching and fills match array with - * the match itself (match[right_i] = left_i). - * @param n number of vertices on the left - */ + /// Returns number of left elements in matching and fills match array with + /// the match itself (match[right_i] = left_i). + /// @param n number of vertices on the left int run() { int ans = 0; diff --git a/algorithms/graph/dfs.cpp b/algorithms/graph/dfs.cpp index c0d9dc576987131f4cacc8c2ebb8670900205c51..a1e6fd6a17394daca5ee882928c0d54aff53800d 100644 --- a/algorithms/graph/dfs.cpp +++ b/algorithms/graph/dfs.cpp @@ -1,9 +1,7 @@ -/** - * Depth First Search (DFS) - * - * Complexity (Time): O(V + E) - * Complexity (Space): O(V + E) - */ +/// Depth First Search (DFS) +/// +/// Complexity (Time): O(V + E) +/// Complexity (Space): O(V + E) vector<int> graph[MAX]; @@ -19,10 +17,8 @@ struct DFS { fill(all(vis), 0); } - /** - * Applies DFS on graph. - * @param x starting vertex - */ + /// Applies DFS on graph. + /// @param x starting vertex void run(int x) { vis[x] = true; for (auto i : graph[x]) diff --git a/algorithms/graph/dijkstra.cpp b/algorithms/graph/dijkstra.cpp index 01f639d95958ed845749b338b0557e19f464332d..a9e80b2c0be1bb6c57cfbd642f63e74fd1aa2a47 100644 --- a/algorithms/graph/dijkstra.cpp +++ b/algorithms/graph/dijkstra.cpp @@ -1,9 +1,7 @@ -/** - * Dijkstra - * - * Complexity (Time): O(E + V log V) - * Complexity (Space): O(V + E) - */ +/// Dijkstra +/// +/// Complexity (Time): O(E + V log V) +/// Complexity (Space): O(V + E) vector<int> graph[MAX]; @@ -20,10 +18,8 @@ struct Dijkstra { fill(all(dist), inf); } - /** - * Returns shortest distance from s to d - * @param s,d origin and destination vertices - */ + /// Returns shortest distance from s to d + /// @param s,d origin and destination vertices int run(int s, int d) { set<ii> pq; diff --git a/algorithms/graph/dinic.cpp b/algorithms/graph/dinic.cpp index caf4862fc087f5fc0228b723285a331270b8f7ee..9f6f315f2f185763d5386ebf5b9ab1dc21cc642d 100644 --- a/algorithms/graph/dinic.cpp +++ b/algorithms/graph/dinic.cpp @@ -1,9 +1,7 @@ -/** - * Dinic's - * - * Complexity (Time): O(E*V^2) - * Complexity (Space): O(V + E) - */ +/// Dinic's +/// +/// Complexity (Time): O(E*V^2) +/// Complexity (Space): O(V + E) struct Dinic { struct Edge { @@ -22,11 +20,9 @@ struct Dinic { N(N), depth(N), start(N), graph(N) {} - /** - * Adds edge to the graph. - * @param s,t origin and destination of edge - * @param c capacity of edge - */ + /// Adds edge to the graph. + /// @param s,t origin and destination of edge + /// @param c capacity of edge void add_edge(int s, int t, int c) { Edge forward(t, 0, c, graph[t].size()); Edge backward(s, 0, 0, graph[s].size()); @@ -35,11 +31,9 @@ struct Dinic { graph[t].pb(backward); } - /** - * Calculates depth of each vertex from source, considering only - * edges with remaining capacity. - * @param s,t source and sink of flow graph - */ + /// Calculates depth of each vertex from source, considering only + /// edges with remaining capacity. + /// @param s,t source and sink of flow graph bool bfs(int s, int t) { queue<int> Q; Q.push(s); @@ -61,11 +55,9 @@ struct Dinic { return depth[t] != -1; } - /** - * Finds bottleneck flow and add to the edges belonging to the paths found. - * @param s,t source and sink of flow graph - * @param flow minimum flow so far - */ + /// Finds bottleneck flow and add to the edges belonging to the paths found. + /// @param s,t source and sink of flow graph + /// @param flow minimum flow so far int dfs(int s, int t, int flow) { if (s == t) return flow; @@ -87,10 +79,8 @@ struct Dinic { return 0; } - /** - * Returns maximum flow. - * @param s,t source and sink of flow graph - */ + /// Returns maximum flow. + /// @param s,t source and sink of flow graph int run(int s, int t) { int ans = 0; diff --git a/algorithms/graph/edmonds_karp.cpp b/algorithms/graph/edmonds_karp.cpp index e9bdb31cdff335a67374b0bbe0678230ff69c44f..a9f6a159fafa351d91123e9150ecda22b7dcf44f 100644 --- a/algorithms/graph/edmonds_karp.cpp +++ b/algorithms/graph/edmonds_karp.cpp @@ -1,9 +1,7 @@ -/** - * Edmonds-Karp - * - * Complexity (time): O(V*E^2) - * Complexity (space): O(V^2) - */ +/// Edmonds-Karp +/// +/// Complexity (time): O(V*E^2) +/// Complexity (space): O(V^2) int rg[MAX][MAX]; int graph[MAX][MAX]; @@ -20,11 +18,9 @@ struct EdmondsKarp { fill(all(vis), 0); } - /** - * Finds if there's a path between s and t using non-full - * residual edges. - * @param s,t source and sink of flow graph - */ + /// Finds if there's a path between s and t using non-full + /// residual edges. + /// @param s,t source and sink of flow graph bool bfs(int s, int t) { queue<int> Q; Q.push(s); @@ -48,10 +44,8 @@ struct EdmondsKarp { return false; } - /** - * Returns maximum flow. - * @param s,t source and sink of flow graph - */ + /// Returns maximum flow. + /// @param s,t source and sink of flow graph 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 d93b55655891a74288f24375a4ea15884584c5b4..67affe34d57044fd3d97bd10cd8d266737449317 100644 --- a/algorithms/graph/floyd_warshall.cpp +++ b/algorithms/graph/floyd_warshall.cpp @@ -1,9 +1,7 @@ -/** - * Floyd Warshall Algorithm - * - * Complexity (Time): O(V^3) - * Complexity (Space): O(V^2) - */ +/// Floyd Warshall Algorithm +/// +/// Complexity (Time): O(V^3) +/// Complexity (Space): O(V^2) int dist[MAX][MAX]; int graph[MAX][MAX]; @@ -15,9 +13,7 @@ struct FloydWarshall { N(N) {} - /** - * Computes shortest path between all pairs of vertices. - */ + /// Computes shortest path between all pairs of vertices. 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 6ffcae0a0f3bca1ce69c4c019ff0f9b574b67e2d..aff839cee61f8dcb095b8cd8c644c6f5f8d25e12 100644 --- a/algorithms/graph/ford_fulkerson.cpp +++ b/algorithms/graph/ford_fulkerson.cpp @@ -1,9 +1,7 @@ -/** - * Ford-Fulkerson - * - * Complexity (time): O(Ef) - * Complexity (space): O(V^2) - */ +/// Ford-Fulkerson +/// +/// Complexity (time): O(Ef) +/// Complexity (space): O(V^2) int rg[MAX][MAX]; int graph[MAX][MAX]; @@ -20,11 +18,9 @@ struct FordFulkerson { fill(all(vis), 0); } - /** - * Finds if there's a path between s and t using non-full - * residual edges. - * @param s,t source and sink of flow graph - */ + /// Finds if there's a path between s and t using non-full + /// residual edges. + /// @param s,t source and sink of flow graph bool dfs(int s, int t) { vis[s] = true; if (s == t) @@ -41,10 +37,8 @@ struct FordFulkerson { return false; } - /** - * Returns maximum flow. - * @param s,t source and sink of flow graph - */ + /// Returns maximum flow. + /// @param s,t source and sink of flow graph 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 2ba0ffef1e25b8057d17fbc1ebbbf1184bc83642..598d35817d5df074d51e997374c004d2f0b79d2d 100644 --- a/algorithms/graph/hopcroft_karp.cpp +++ b/algorithms/graph/hopcroft_karp.cpp @@ -1,9 +1,7 @@ -/** - * Hopcroft-Karp - * - * Complexity (Time): O(E*sqrt(V)) - * Complexity (Space): O(V + E) - */ +/// Hopcroft-Karp +/// +/// Complexity (Time): O(E*sqrt(V)) +/// Complexity (Space): O(V + E) vector<int> graph[MAX]; @@ -22,11 +20,9 @@ struct HopcroftKarp { fill(all(matchR), 0); } - /** - * Builds an alternating level graph rooted at unmatched vertices in L - * using BFS, and returns whether there is an augmenting path in the graph - * or not. - */ + /// Builds an alternating level graph rooted at unmatched vertices in L + /// using BFS, and returns whether there is an augmenting path in the graph + /// or not. bool bfs() { queue<int> Q; @@ -57,11 +53,9 @@ struct HopcroftKarp { return (dist[0] != inf); } - /** - * Augments path starting in l if there is one, returns - * whether a path was augmented or not. - * @param l initial free vertex on the left - */ + /// Augments path starting in l if there is one, returns + /// whether a path was augmented or not. + /// @param l initial free vertex on the left bool dfs(int l) { if (l == 0) return true; @@ -80,9 +74,7 @@ struct HopcroftKarp { return false; } - /** - * Returns number of matched vertices on the left (matched edges). - */ + /// Returns number of matched vertices on the left (matched edges). int run() { int ans = 0; diff --git a/algorithms/graph/kosaraju.cpp b/algorithms/graph/kosaraju.cpp index 72af95464743277aa85cba37cae76e8335eb1e89..efcbbc6a132bcf9e5728fc944b9cbab4b8436247 100644 --- a/algorithms/graph/kosaraju.cpp +++ b/algorithms/graph/kosaraju.cpp @@ -1,9 +1,7 @@ -/** - * Kosaraju - * - * Complexity (Time): O(V + E) - * Complexity (Space): O(V + E) - */ +/// Kosaraju +/// +/// Complexity (Time): O(V + E) +/// Complexity (Space): O(V + E) vector<int> graph[MAX]; vector<int> transp[MAX]; @@ -21,10 +19,8 @@ struct Kosaraju { fill(all(vis), 0); } - /** - * Traverses a SCC. - * @param x initial vertex - */ + /// Traverses a SCC. + /// @param x initial vertex void dfs(int x) { vis[x] = true; @@ -33,10 +29,8 @@ struct Kosaraju { dfs(i); } - /** - * Fills stack with DFS starting points to find SCC. - * @param x initial vertex - */ + /// Fills stack with DFS starting points to find SCC. + /// @param x initial vertex void fill_stack(int x) { vis[x] = true; @@ -47,9 +41,7 @@ struct Kosaraju { S.push(x); } - /** - * Returns number of SCC of a graph. - */ + /// Returns number of SCC of a graph. int run() { int scc = 0; diff --git a/algorithms/graph/kruskal.cpp b/algorithms/graph/kruskal.cpp index 26b8c941eaa66b71c7647412ca9310e90a82a97a..e12f0f98a2b86893c6f62248fe23144a7c303e6c 100644 --- a/algorithms/graph/kruskal.cpp +++ b/algorithms/graph/kruskal.cpp @@ -1,11 +1,9 @@ -/** - * Kruskal - * - * Complexity (Time): O (E log V) - * Complexity (Space): O(E) - * - * #include <structure/disjoint_set> - */ +/// Kruskal +/// +/// Complexity (Time): O (E log V) +/// Complexity (Space): O(E) +/// +/// #include <structure/disjoint_set> typedef pair<ii,int> iii; vector<iii> edges; @@ -18,10 +16,8 @@ struct Kruskal { N(N), ds(N) {} - /** - * Returns value of MST. - * @param mst[out] vector with edges of computed MST - */ + /// Returns value of MST. + /// @param mst[out] vector with edges of computed MST int run(vector<iii> &mst) { // Sort by weight of the edges diff --git a/algorithms/graph/lca.cpp b/algorithms/graph/lca.cpp index 136a2c051095c20b64175eba8278a581a01f4a9a..d2d68bf598d5f86b2af73e2d936c9a40a6251c9b 100644 --- a/algorithms/graph/lca.cpp +++ b/algorithms/graph/lca.cpp @@ -1,15 +1,13 @@ -/** - * Lowest Common Ancestor - LCA - * - * Complexity (Time): - * - preprocess: O(V log V) - * - query: O(log V) - * Complexity (Space): O(V + E + V log V) - * - * OBS: * = return sum path to LCA - * ** = return max value on path to LCA - * *** = used in both * and ** - */ +/// Lowest Common Ancestor - LCA +/// +/// Complexity (Time): +/// - preprocess: O(V log V) +/// - query: O(log V) +/// Complexity (Space): O(V + E + V log V) +/// +/// OBS: * = return sum path to LCA +/// ** = return max value on path to LCA +/// *** = used in both * and ** #define MAXLOG 20 //log2(MAX) @@ -21,10 +19,8 @@ vector<int> graph[MAX]; //*** vector<ii> struct LCA { - /** - * Performs DFS while filling h, par, and cost. - * @param v root of the tree - */ + /// Performs DFS while filling h, par, and cost. + /// @param v root of the tree void dfs(int v, int p = -1, int c = 0) { par[v][0] = p; //*** cost[v][0] = c; @@ -44,20 +40,16 @@ struct LCA { dfs(u.fi, v, u.se); } - /** - * Preprocess tree. - * @param v root of the tree - */ + /// Preprocess tree. + /// @param v root of the tree void preprocess(int v) { mset(par, -1); //*** mset(cost, 0); dfs(v); } - /** - * Returns LCA (or sum or max). - * @param p,q query nodes - */ + /// Returns LCA (or sum or max). + /// @param p,q query nodes int query(int p, int q) { //*** int ans = 0; diff --git a/algorithms/graph/prim.cpp b/algorithms/graph/prim.cpp index 308367ce2c6bdbbad98fb0a2a71f711e1d0a374d..ac51f93395228180773920bbb224d86ff8b34f4f 100644 --- a/algorithms/graph/prim.cpp +++ b/algorithms/graph/prim.cpp @@ -1,9 +1,7 @@ -/** - * Prim - * - * Complexity (Time): O(E log E) - * Complexity (Space): O(V + E) - */ +/// Prim +/// +/// Complexity (Time): O(E log E) +/// Complexity (Space): O(V + E) vector<ii> graph[MAX]; @@ -19,9 +17,7 @@ struct Prim { fill(all(vis), 0); } - /** - * Returns value of MST of graph. - */ + /// Returns value of MST of graph. int run() { init(); vis[0] = true; diff --git a/algorithms/graph/tarjan.cpp b/algorithms/graph/tarjan.cpp index e390a760e50e6c215f3b641cc905315be2c9c8d4..ce9b170f0b86a05ed984f67c5d61dd17b70389f9 100644 --- a/algorithms/graph/tarjan.cpp +++ b/algorithms/graph/tarjan.cpp @@ -1,14 +1,12 @@ -/** - * Tarjan - Strongly Connected Components (SCC) - * - * Complexity (Time): O(V + E) - * Complexity (Space): O(V + E) - */ +/// Tarjan - Strongly Connected Components (SCC) +/// +/// Complexity (Time): O(V + E) +/// Complexity (Space): O(V + E) vector<int> graph[MAX]; -// scc[i] contains the i-th SCC stored as a vector with the ids of the -// vertices in it +/// scc[i] contains the i-th SCC stored as a vector with the ids of the +/// vertices in it vector<int> scc[MAX]; struct Tarjan { @@ -26,10 +24,8 @@ struct Tarjan { fill(all(vis), 0); } - /** - * Fills SCC with strongly connected components of graph. - * @param x any vertex - */ + /// Fills SCC with strongly connected components of graph. + /// @param x any vertex void dfs(int x) { id[x] = low[x] = ind++; vis[x] = 1; @@ -64,9 +60,7 @@ struct Tarjan { } } - /** - * Returns number of SCCs. - */ + /// Returns number of SCCs. int run() { init(); ncomp = ind = 0; diff --git a/algorithms/graph/topological_sort.cpp b/algorithms/graph/topological_sort.cpp index ed3459ee4be31853d396fee5278e1507ea1df1a1..04ca9e0560d0c9209c0395da6de424390c34771f 100644 --- a/algorithms/graph/topological_sort.cpp +++ b/algorithms/graph/topological_sort.cpp @@ -1,9 +1,7 @@ -/** - * Topological Sort - * - * Complexity (Time): O(V + E) - * Complexity (Space): O(V + E) - */ +/// Topological Sort +/// +/// Complexity (Time): O(V + E) +/// Complexity (Space): O(V + E) vector<int> graph[MAX]; @@ -20,10 +18,8 @@ struct TopologicalSort { fill(all(cont), 0); } - /** - * Fills stack and check for cycles. - * @param x any vertex - */ + /// Fills stack and check for cycles. + /// @param x any vertex bool dfs(int x) { cont[x] = 1; @@ -40,10 +36,8 @@ struct TopologicalSort { return false; } - /** - * Returns whether graph contains cycle or not. - * @param[out] tsort topological sort of the graph - */ + /// Returns whether graph contains cycle or not. + /// @param[out] tsort topological sort of the graph bool topological_sort(vector<int> &tsort) { init(); diff --git a/algorithms/math/binary_exponentiation.cpp b/algorithms/math/binary_exponentiation.cpp index 58f8b026fd26660801213f607527e3cb95aeb719..c3cf0fd3271afe689de8dcca9e0e4b36c0862393 100644 --- a/algorithms/math/binary_exponentiation.cpp +++ b/algorithms/math/binary_exponentiation.cpp @@ -1,15 +1,11 @@ -/** - * Binary Exponentiation - * - * Complexity (Time): O(log n) - * Complexity (Space): O(1) - */ +/// Binary Exponentiation +/// +/// Complexity (Time): O(log n) +/// Complexity (Space): O(1) -/** - * Returns x^n in O(log n) time - * @param x number - * @param n exponent - */ +/// Returns x^n in O(log n) time +/// @param x number +/// @param n exponent template <typename T> T fast_pow(T x, ll n) { T ans = 1; diff --git a/algorithms/math/fft.cpp b/algorithms/math/fft.cpp index cc105a48219ce42328afa9ec3a831200f48b1d42..f8e019342e2a5aa4023381792bc3830e113bb6f5 100644 --- a/algorithms/math/fft.cpp +++ b/algorithms/math/fft.cpp @@ -1,10 +1,7 @@ -/** - * Fast Fourier Transform (FFT) - * - * Complexity (Time): O(N log N) - * Complexity (Space): O(N) - */ - +/// Fast Fourier Transform (FFT) +/// +/// Complexity (Time): O(N log N) +/// Complexity (Space): O(N) struct FFT { struct Complex { @@ -29,10 +26,8 @@ struct FFT { vector<int> rev = {0, 1}; vector<Complex> roots = {{0, 0}, {1, 0}}; - /** - * Initializes reversed-bit vector (rev) and roots of unity vector (roots) - * @param nbase log2 of size of vectors. - */ + /// Initializes reversed-bit vector (rev) and roots of unity vector (roots) + /// @param nbase log2 of size of vectors. void init(int nbase) { rev.resize(1 << nbase); roots.resize(1 << nbase); @@ -54,10 +49,8 @@ struct FFT { } } - /** - * Applies FFT on a vector. - * @param a input vector - */ + /// Applies FFT on a vector. + /// @param a input vector void fft(vector<Complex> &a) { int n = a.size(); @@ -77,10 +70,8 @@ struct FFT { } } - /** - * Executes vector multiplication in O(n log n). - * @param a, b input vectors - */ + /// Executes vector multiplication in O(n log n). + /// @param a, b input vectors vector<int> multiply(const vector<int> &a, const vector<int> &b) { int nbase, need = a.size() + b.size() + 1; diff --git a/algorithms/math/linear_recurrence.cpp b/algorithms/math/linear_recurrence.cpp index 9c3c37b61fd86dcbdb771f81f78557b0f11071f9..baaf5247971bb882264dd2e0dab491618015c880 100644 --- a/algorithms/math/linear_recurrence.cpp +++ b/algorithms/math/linear_recurrence.cpp @@ -1,30 +1,26 @@ -/** - * Linear Recurrence - * - * Complexity (Time): O(log n) - * Complexity (Space): O(1) - * - * #include "math/binary_exponentiation.cpp" - * #include "math/matrix.cpp" - */ +/// Linear Recurrence +/// +/// Complexity (Time): O(log n) +/// Complexity (Space): O(1) +/// +/// #include "math/binary_exponentiation.cpp" +/// #include "math/matrix.cpp" -/** - * 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] - */ +/// 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 f33351a79b7e3c6b55222f8f5daf8ab6d00e782d..ac1c43d185861dd13148ef589c3a169367b114ab 100644 --- a/algorithms/math/matrix.cpp +++ b/algorithms/math/matrix.cpp @@ -1,6 +1,4 @@ -/** - * Matrix - */ +/// Matrix template <typename T> struct matrix { diff --git a/algorithms/math/modular_multiplicative_inverse.cpp b/algorithms/math/modular_multiplicative_inverse.cpp index 1b43fe2b1f908c0154b97a8174fec3811634f7f7..57ae87574153039a8c00450fc7f93e63e616b8d9 100644 --- a/algorithms/math/modular_multiplicative_inverse.cpp +++ b/algorithms/math/modular_multiplicative_inverse.cpp @@ -1,28 +1,22 @@ -/** - * Modular Multiplicative Inverse - * - * Complexity (Time): O(log m) - * Complexity (Space): O(1) - */ - -// ========== Fermat's Little Theorem ========== -// Used when m is prime -// #include "binary_exponentiation.cpp" - -/** - * Finds x where: a*x === 1 (mod MOD) - */ +/// Modular Multiplicative Inverse +/// +/// Complexity (Time): O(log m) +/// Complexity (Space): O(1) + +/// ========== Fermat's Little Theorem ========== +/// Used when m is prime +/// #include "binary_exponentiation.cpp" + +/// Finds x where: a*x === 1 (mod MOD) ll mod_inverse(ll a) { return fast_pow(a, MOD - 2); } -// ========== Extended Euclidean Algorithm ========== -// Used when m and a are coprime +/// ========== Extended Euclidean Algorithm ========== +/// Used when m and a are coprime -/** - * Returns gcd(a, b) and find x, y where: a*x + b*y = gcd(a, b) - */ +/// Returns gcd(a, b) and find x, y where: a*x + b*y = gcd(a, b) ll gcd_extended(ll a, ll b, ll &x, ll &y) { if (!a) { x = 0; @@ -39,9 +33,7 @@ ll gcd_extended(ll a, ll b, ll &x, ll &y) { return g; } -/** - * Finds x where: a*x === 1 (mod MOD) - */ +/// Finds x where: a*x === 1 (mod MOD) ll mod_inverse(ll a) { ll x, y; ll g = gcd_extended(a, MOD, x, y); diff --git a/algorithms/math/sieve_of_eratosthenes.cpp b/algorithms/math/sieve_of_eratosthenes.cpp index 64b926851767b39791cfdd2dae49600b7e87a9c3..d90f8afb9482c6ad7437a5053dcb52f0b00ef42f 100644 --- a/algorithms/math/sieve_of_eratosthenes.cpp +++ b/algorithms/math/sieve_of_eratosthenes.cpp @@ -1,13 +1,9 @@ -/** - * Sieve of Eratosthenes - * - * Complexity (Time): O(n*log(log(n))) - * Complexity (Space): O(n) - */ +/// Sieve of Eratosthenes +/// +/// Complexity (Time): O(n*log(log(n))) +/// Complexity (Space): O(n) -/** - * Returns vector of primes less than or equal to n - */ +/// Returns vector of primes less than or equal to n struct Sieve { int N; vector<int> is_prime; @@ -20,9 +16,7 @@ struct Sieve { fill(all(is_prime), 1); } - /** - * Returns vector of primes less than N - */ + /// Returns vector of primes less than N vector<int> run() { vector<int> primes; init(); diff --git a/algorithms/paradigm/edit_distance.cpp b/algorithms/paradigm/edit_distance.cpp index a1edb780089d315d478f6e403af32684d9f02720..57eddec5d6d5f3e69254dd6bf16af534aa715eab 100644 --- a/algorithms/paradigm/edit_distance.cpp +++ b/algorithms/paradigm/edit_distance.cpp @@ -1,16 +1,12 @@ -/** - * Edit Distance - * - * Complexity (Time): O(m*n) - * Complexity (Space): O(m*n) - */ +/// Edit Distance +/// +/// Complexity (Time): O(m*n) +/// Complexity (Space): O(m*n) int dp[MAX][MAX]; -/** - * Returns edit distance (Levenshtein distance) between a and b - * @param a,b input strings - */ +/// Returns edit distance (Levenshtein distance) between a and b +/// @param a,b input strings int edit_distance(string a, string b) { int n = a.size(); int m = b.size(); diff --git a/algorithms/paradigm/kadane.cpp b/algorithms/paradigm/kadane.cpp index 627a3f2ea14a274a0f1f4c6acdc3abcd4d98150b..e69765dfdd1eb630320d054ccd8329844d0ca446 100644 --- a/algorithms/paradigm/kadane.cpp +++ b/algorithms/paradigm/kadane.cpp @@ -1,15 +1,11 @@ -/** - * Kadane - * - * Complexity (Time): O(n + m) - * Complexity (Space): O(n + m) - */ +/// Kadane +/// +/// Complexity (Time): O(n + m) +/// Complexity (Space): O(n + m) -/** - * Returns the largest sum of a contiguous subarray - * @param[in] v input vector - * @param[out] start,end start and end index of said subarray - */ +/// Returns the largest sum of a contiguous subarray +/// @param[in] v input vector +/// @param[out] start,end start and end index of said subarray int kadane(const vector<int> &v, int &start, int &end) { int n = v.size(), s = 0; diff --git a/algorithms/paradigm/lis.cpp b/algorithms/paradigm/lis.cpp index 8c69ed12ab9f70f1cac9dfbb6034da73bdb8f776..8290e0640acff65ec3467ece711fa3c1c50e5093 100644 --- a/algorithms/paradigm/lis.cpp +++ b/algorithms/paradigm/lis.cpp @@ -1,14 +1,10 @@ -/** - * Longest Increasing Subsequence (LIS) - * - * Complexity (Time): O(n^2) - * Complexity (Space): O(n) - */ +/// Longest Increasing Subsequence (LIS) +/// +/// Complexity (Time): O(n^2) +/// Complexity (Space): O(n) -/** - * Returns the length of the longest increasing subsequence - * @param v input vector - */ +/// Returns the length of the longest increasing subsequence +/// @param v input vector int lis(vector<int> v) { int n = v.size(); diff --git a/algorithms/paradigm/ternary_search.cpp b/algorithms/paradigm/ternary_search.cpp index 84786526556cba681872d0a25624e03a6aa3e087..b4254903c992fb96e1bf2ef283289f67e2789a3f 100644 --- a/algorithms/paradigm/ternary_search.cpp +++ b/algorithms/paradigm/ternary_search.cpp @@ -1,23 +1,17 @@ -/** - * Ternary Search - * - * Complexity (Time): O(log n) - * Complexity (Space): O(1) - */ +/// Ternary Search +/// +/// Complexity (Time): O(log n) +/// Complexity (Space): O(1) #define EPS 1e-6 -/** - * Unimodal function - */ +/// Unimodal function double f(double x) { return x * x; } -/** - * Executes ternary search to find maximum or minimum - * @param l,r boundaries of search - */ +/// Executes ternary search to find maximum or minimum +/// @param l,r boundaries of search double ternary_search(double l, double r) { double rt, lt; diff --git a/algorithms/string/kmp.cpp b/algorithms/string/kmp.cpp index 80cbf8123a6c019c93b846eae210dea32fa830af..654f71094d9496819515fbecba9c4c62af626e1b 100644 --- a/algorithms/string/kmp.cpp +++ b/algorithms/string/kmp.cpp @@ -1,20 +1,16 @@ -/** - * Knuth-Morris-Pratt (KMP) - * - * Complexity (Time): - * preprocess -> O(m) - * search -> O(n) - * Complexity (Space): O(n + m) - */ +/// Knuth-Morris-Pratt (KMP) +/// +/// Complexity (Time): +/// preprocess -> O(m) +/// search -> O(n) +/// Complexity (Space): O(n + m) int table[MAX]; -/** - * Builds the table where table[i] is the longest prefix of - * patt[0..i] which is - * also a sufix of patt[0..i] - * @param patt pattern to be found - */ +/// Builds the table where table[i] is the longest prefix of +/// patt[0..i] which is +/// also a sufix of patt[0..i] +/// @param patt pattern to be found void preprocess(string patt) { int i = 1, len = 0; @@ -28,12 +24,10 @@ void preprocess(string patt) { } } -/** - * Searches for occurrences of patt in txt and add indexes of - * matches to occurs - * @param patt pattern to be found - * @param txt text - */ +/// Searches for occurrences of patt in txt and add indexes of +/// matches to occurs +/// @param patt pattern to be found +/// @param txt text void search(string patt, string txt) { int i = 0, j = 0; vector<int> occurs; diff --git a/algorithms/structure/avl.cpp b/algorithms/structure/avl.cpp index 2fa017ce3b18ff7a7ea1eb9407ce2fcdefee711d..b62bdcf20489e1d2c7d5966de7a280a9f9d2885c 100644 --- a/algorithms/structure/avl.cpp +++ b/algorithms/structure/avl.cpp @@ -1,9 +1,7 @@ -/** - * AVL tree - * - * Complexity (Time): O(log n) - * Complexity (Space): O(n) - */ +/// AVL tree +/// +/// Complexity (Time): O(log n) +/// Complexity (Space): O(n) struct AVL { struct Node { @@ -15,36 +13,28 @@ struct AVL { left(nullptr), right(nullptr) {} - /** - * Change height based on the information on the left and right. - */ + /// Change height based on the information on the left and right. void fix_height() { int lh = (left == nullptr) ? 0 : left->height; int rh = (right == nullptr) ? 0 : right->height; height = max(lh, rh) + 1; } - /** - * Change size based on the information on the left and right. - */ + /// Change size based on the information on the left and right. void fix_size() { int ls = (left == nullptr) ? 0 : left->size; int rs = (right == nullptr) ? 0 : right->size; size = ls + rs + 1; } - /** - * Fix both height and size. - */ + /// Fix both height and size. void fix_state() { fix_height(); fix_size(); } - /** - * Returns difference between the height on the left and the - * height on the right - */ + /// Returns difference between the height on the left and the + /// height on the right int get_balance() { int lh = (left == nullptr) ? 0 : left->height; int rh = (right == nullptr) ? 0 : right->height; @@ -58,10 +48,8 @@ struct AVL { root(nullptr) {} - /** - * Rotates node to right. - * @param node input node - */ + /// Rotates node to right. + /// @param node input node Node *rotate_right(Node *node) { Node *aux1 = node->left; Node *aux2 = aux1->right; @@ -75,10 +63,8 @@ struct AVL { return aux1; } - /** - * Rotates node to left. - * @param node input node - */ + /// Rotates node to left. + /// @param node input node Node *rotate_left(Node *node) { Node *aux1 = node->right; Node *aux2 = aux1->left; @@ -92,11 +78,9 @@ struct AVL { return aux1; } - /** - * Inserts key recursively in AVL and returns new root. - * @param node root - * @param key value to be inserted - */ + /// Inserts key recursively in AVL and returns new root. + /// @param node root + /// @param key value to be inserted Node *insert(Node *node, int key) { if (node == nullptr) { @@ -136,10 +120,8 @@ struct AVL { return node; } - /** - * Insert new key. - * @param key value to be inserted - */ + /// Insert new key. + /// @param key value to be inserted void insert(int key) { root = insert(root, key); } diff --git a/algorithms/structure/ball_tree.cpp b/algorithms/structure/ball_tree.cpp index 3f07ed6eaa2a633435eb5ac99666a13d41429190..9a439e23cb81488176b5c9f1dae3a04109112630 100644 --- a/algorithms/structure/ball_tree.cpp +++ b/algorithms/structure/ball_tree.cpp @@ -1,14 +1,11 @@ -/** - * Balltree - * - * Complexity (Time): O(n log n) - * Complexity (Space): O(n) - */ +/// Balltree +/// +/// Complexity (Time): O(n log n) +/// Complexity (Space): O(n) #define x first #define y second - struct BallTree { typedef pair<double, double> point; @@ -24,16 +21,12 @@ struct BallTree { build(points); } - /** - * Returns distance between point a and b - */ + /// Returns distance between point a and b double distance(point &a, point &b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } - /** - * Finds furthest point from center and returns <distance,index> of that point - */ + /// Finds furthest point from center and returns <distance,index> of that point pair<double, int> get_radius(point ¢er, vector<point> &ps) { int ind = 0; double dist, radius = -1.0; @@ -50,11 +43,9 @@ struct BallTree { return pair<double, int>(radius, ind); } - /** - * Finds average point and pretends it's the center of the given set of points - * @param ps vector of points - * @param[out] center center point - */ + /// Finds average point and pretends it's the center of the given set of points + /// @param ps vector of points + /// @param[out] center center point void get_center(const vector<point> &ps, point ¢er) { center.x = center.y = 0; @@ -67,15 +58,13 @@ struct BallTree { center.y /= (double) ps.size(); } - /** - * Splits the set of points in closer to ps[lind] and closer to ps[rind], - * where lind is returned by get_radius and rind is the furthest points - * from ps[lind] - * @param ps vector of points - * @param[out] left leftmost point - * @param[out] right rightmost point - * @param lind index of left point - */ + /// Splits the set of points in closer to ps[lind] and closer to ps[rind], + /// where lind is returned by get_radius and rind is the furthest points + /// from ps[lind] + /// @param ps vector of points + /// @param[out] left leftmost point + /// @param[out] right rightmost point + /// @param lind index of left point void partition(const vector<point> &ps, vector<point> &left, vector<point> &right, int lind) { int rind = 0; double dist, grt = -1.0; @@ -111,9 +100,7 @@ struct BallTree { } } - /** - * Builds ball-tree recursively. - */ + /// Builds ball-tree recursively. Node *build(vector<point> &ps) { if (ps.size() == 0) return nullptr; @@ -144,13 +131,11 @@ struct BallTree { return n; } - /** - * Search the ball-tree recursively - * @param n root - * @param t query point - * @param pq initially empty multiset (will contain the answer after execution) - * @param k number of nearest neighbors - */ + /// Search the ball-tree recursively + /// @param n root + /// @param t query point + /// @param pq initially empty multiset (will contain the answer after execution) + /// @param k number of nearest neighbors void search(Node *n, point t, multiset<double> &pq, int &k) { if (n->left == nullptr && n->right == nullptr) { double dist = distance(t, n->center); diff --git a/algorithms/structure/bit.cpp b/algorithms/structure/bit.cpp index 30e645c83d676b17d51848bd726349ef37e2a10f..e2473e0a611fc47727fe1a7cbc366fbfbd9543be 100644 --- a/algorithms/structure/bit.cpp +++ b/algorithms/structure/bit.cpp @@ -1,11 +1,9 @@ -/** - * Binary Indexed Tree (BIT) - * - * Complexity (Time): - * Update -> O(log n) - * Query -> O(log n) - * Complexity (Space): O(n) - */ +/// Binary Indexed Tree (BIT) +/// +/// Complexity (Time): +/// Update -> O(log n) +/// Query -> O(log n) +/// Complexity (Space): O(n) struct BIT { int N; @@ -19,9 +17,7 @@ struct BIT { fill(all(tree), 0); } - /** - * Performs query in array (tree) in the idx position. - */ + /// Performs query in array (tree) in the idx position. int query(int idx) { int sum = 0; for (; idx > 0; idx -= (idx & -idx)) @@ -30,9 +26,7 @@ struct BIT { return sum; } - /** - * Adds a value (val) to a single position (idx) in the array (tree). - */ + /// Adds a value (val) to a single position (idx) in the array (tree). void update(int idx, int val) { for (; idx < N; idx += (idx & -idx)) tree[idx] += val; diff --git a/algorithms/structure/bit2d.cpp b/algorithms/structure/bit2d.cpp index d8ce41ea8233cdc3c231627ee0dbe05288698695..1c0f9ca50a7ca269228e87fae7255fb19bbf94f6 100644 --- a/algorithms/structure/bit2d.cpp +++ b/algorithms/structure/bit2d.cpp @@ -1,11 +1,9 @@ -/** - * Binary Indexed Tree 2D (BIT2D) - * - * Complexity (Time): - * Update -> O(log^2 n) - * Query -> O(log^2 n) - * Complexity (Space): O(n^2) - */ +/// Binary Indexed Tree 2D (BIT2D) +/// +/// Complexity (Time): +/// Update -> O(log^2 n) +/// Query -> O(log^2 n) +/// Complexity (Space): O(n^2) struct BIT2D { int N, M; @@ -20,9 +18,7 @@ struct BIT2D { fill(all(i), 0); } - /** - * Performs query in array (tree) in the (idx,idy) position. - */ + /// Performs query in array (tree) in the (idx,idy) position. int query(int idx, int idy) { int sum = 0; for (; idx > 0; idx -= (idx & -idx)) @@ -32,9 +28,7 @@ struct BIT2D { return sum; } - /** - * Adds a value (val) to a single position (idx,idy) in the array (tree). - */ + /// Adds a value (val) to a single position (idx,idy) in the array (tree). void update(int idx, int idy, int val) { for (; idx < N; idx += (idx & -idx)) for (int m = idy; m < M; m += (m & -m)) diff --git a/algorithms/structure/bitmask.cpp b/algorithms/structure/bitmask.cpp index 2506f97d14e0e8296a2fff444db88d1f6ab5677f..956cd98ea408b5d7379569bd57fa7ebc5669251d 100644 --- a/algorithms/structure/bitmask.cpp +++ b/algorithms/structure/bitmask.cpp @@ -1,9 +1,7 @@ -/** - * Bitmask - * - * Complexity (Time): O(1) - * Complexity (Space): O(1) - */ +/// Bitmask +/// +/// Complexity (Time): O(1) +/// Complexity (Space): O(1) struct Bitmask { ll state; @@ -12,51 +10,37 @@ struct Bitmask { state(state) {} - /** - * Sets bit in position pos (0 to 1). - */ + /// Sets bit in position pos (0 to 1). void set(int pos) { state |= (1 << pos); } - /** - * Sets all bits in a bitmask with size n. - */ + /// Sets all bits in a bitmask with size n. void set_all(int n) { state = (1 << n) - 1; } - /** - * Unsets bit in position pos (1 to 0). - */ + /// Unsets bit in position pos (1 to 0). void unset(int pos) { state &= ~(1 << pos); } - /** - * Unsets all bits. - */ + /// Unsets all bits. void unset_all() { state = 0; } - /** - * Gets value of bit in position pos. - */ + /// Gets value of bit in position pos. int get(int pos) { return state & (1 << pos); } - /** - * Toggles value in position pos. - */ + /// Toggles value in position pos. void toggle(int pos) { state ^= (1 << pos); } - /** - * Gets position of least significant 1. - */ + /// Gets position of least significant 1. int least_significant_one() { return state & (-state); } diff --git a/algorithms/structure/disjoint_set.cpp b/algorithms/structure/disjoint_set.cpp index 59a9a4741b96ad05a421a93ad1e154c88caa8e4a..be2dd242c1e0a8a9cd47483e25bc9b33027010fc 100644 --- a/algorithms/structure/disjoint_set.cpp +++ b/algorithms/structure/disjoint_set.cpp @@ -1,12 +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) - */ +/// Disjoint-set +/// +/// Complexity (Time): O(1) +/// make_set -> O(1) +/// find_set -> O(a(n)) +/// union_set -> O(a(n)) +/// Complexity (Space): O(n) struct DisjointSet { int N; @@ -16,27 +14,21 @@ struct DisjointSet { N(N), h(N), par(N), size(N) {} - /** - * Initializes element x. - */ + /// Initializes element x. void make_set(int x) { par[x] = x; h[x] = 0; size[x] = 1; } - /** - * Returns set index from element x. - */ + /// Returns set index from element x. int find_set(int x) { if (par[x] != x) par[x] = find_set(par[x]); return par[x]; } - /** - * Makes x and y belong to the same set. - */ + /// Makes x and y belong to the same set. void union_set(int x, int y) { x = find_set(x); y = find_set(y); diff --git a/algorithms/structure/lazy_segment_tree.cpp b/algorithms/structure/lazy_segment_tree.cpp index fced1ab2ff81f3f0128e164c477a57de8d1df18a..aee0052205d7085b377f23d314c11e0dd3748ac5 100644 --- a/algorithms/structure/lazy_segment_tree.cpp +++ b/algorithms/structure/lazy_segment_tree.cpp @@ -1,12 +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) - */ +/// 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) struct LazySegmentTree { int N; @@ -25,10 +23,8 @@ struct LazySegmentTree { inline int left(int x) { return (x << 1); } inline int right(int x) { return (x << 1) + 1; } - /** - * Builds tree with elements from v (0-indexed). - * @param v vector containing initial values - */ + /// Builds tree with elements from v (0-indexed). + /// @param v vector containing initial values void build(const vector<int> &v, int node = 1, int a = 0, int b = N - 1) { if (a > b) return; @@ -43,12 +39,10 @@ struct LazySegmentTree { tree[node] = tree[node * 2] + tree[node * 2 + 1]; } - /** - * Propagates value to tree and through lazy tree. - * @param node node to change - * @param a,b range of node - * @param val value to be pushed - */ + /// Propagates value to tree and through lazy tree. + /// @param node node to change + /// @param a,b range of node + /// @param val value to be pushed void push(int node, int a, int b, int val) { tree[node] += val; // tree[node] += (b - a + 1) * val; (for Range Sum Query) @@ -61,11 +55,9 @@ struct LazySegmentTree { lazy[node] = 0; } - /** - * Updates segment [i,j] by adding value val. - * @param i,j range - * @param val value to be added - */ + /// Updates segment [i,j] by adding value val. + /// @param i,j range + /// @param val value to be added void update(int i, int j, int val, int node = 1, int a = 0, int b = N - 1) { if (lazy[node] != 0) push(node, a, b, lazy[node]); @@ -83,10 +75,8 @@ struct LazySegmentTree { tree[node] = tree[node * 2] + tree[node * 2 + 1]; } - /** - * Returns sum of [i,j]. - * @param i,j range - */ + /// Returns sum of [i,j]. + /// @param i,j range int query(int i, int j, int node = 1, int a = 0, int b = N - 1) { if (a > b || a > j || b < i) return 0; diff --git a/algorithms/structure/policy_tree.cpp b/algorithms/structure/policy_tree.cpp index 16c53941de22b85ee75580416df49828e03d8932..eba528e505136937fa5b2cf2f2449904f880ff83 100644 --- a/algorithms/structure/policy_tree.cpp +++ b/algorithms/structure/policy_tree.cpp @@ -1,15 +1,13 @@ -/** - * Policy Tree - * - * 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) - */ +/// Policy Tree +/// +/// 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) #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> @@ -24,9 +22,7 @@ typedef tree< tree_order_statistics_node_update > set_t; -/** - * Demonstration of operations. - */ +/// Demonstration of operations. void operations() { set_t S; diff --git a/algorithms/structure/segment_tree.cpp b/algorithms/structure/segment_tree.cpp index 7ffed8fe40fe2032c8976597f5920a478f5bd906..568384cd2b4d293d15697dc461380da9cf8b2cbc 100644 --- a/algorithms/structure/segment_tree.cpp +++ b/algorithms/structure/segment_tree.cpp @@ -1,12 +1,10 @@ -/** - * Segment Tree - * - * Complexity (Time): - * Build -> O(n log n) - * Update -> O(log n) - * Query -> O(log n) - * Complexity (Space): O(n) - */ +/// Segment Tree +/// +/// Complexity (Time): +/// Build -> O(n log n) +/// Update -> O(log n) +/// Query -> O(log n) +/// Complexity (Space): O(n) struct SegmentTree { int N; @@ -24,10 +22,8 @@ struct SegmentTree { inline int left(int x) { return (x << 1); } inline int right(int x) { return (x << 1) + 1; } - /** - * Builds tree with elements from v (0-indexed). - * @param v vector containing initial values - */ + /// Builds tree with elements from v (0-indexed). + /// @param v vector containing initial values void build(const vector<int> &v, int node = 1, int a = 0, int b = N - 1) { if (a > b) return; @@ -42,11 +38,9 @@ struct SegmentTree { tree[node] = tree[node * 2] + tree[node * 2 + 1]; } - /** - * Updates position idx by adding value val. - * @param idx position - * @param val value to be added - */ + /// Updates position idx by adding value val. + /// @param idx position + /// @param val value to be added void update(int idx, int val, int node = 1, int a = 0, int b = N - 1) { if (a > b || a > idx || b < idx) return; @@ -61,10 +55,8 @@ struct SegmentTree { tree[node] = tree[node * 2] + tree[node * 2 + 1]; } - /** - * Returns sum of [i,j]. - * @param i,j range - */ + /// Returns sum of [i,j]. + /// @param i,j range int query(int i, int j, int node = 1, int a = 0, int b = N - 1) { if (a > b || a > j || b < i) return 0; diff --git a/algorithms/structure/sqrt_decomposition.cpp b/algorithms/structure/sqrt_decomposition.cpp index 92e58ceca10bcb2017bd080e41838172fe4eb399..b37a5823f60a089f2055ac2b5589d6ad496e8449 100644 --- a/algorithms/structure/sqrt_decomposition.cpp +++ b/algorithms/structure/sqrt_decomposition.cpp @@ -1,31 +1,25 @@ -/** - * Sqrt Decomposition - * - * Complexity (time): - * Preprocess -> O(n) - * Query -> O(sqrt(n)) - * Update -> O(1) - * Complexity (space): O(n) - */ +/// Sqrt Decomposition +/// +/// Complexity (time): +/// Preprocess -> O(n) +/// Query -> O(sqrt(n)) +/// Update -> O(1) +/// Complexity (space): O(n) int v[MAX]; int block[MAX]; int block_size; -/** - * Update v[idx] with val. - * @param idx index of v - * @param val new value of v[idx] - */ +/// Update v[idx] with val. +/// @param idx index of v +/// @param val new value of v[idx] void update(int idx, int val) { block[idx / block_size] += val - v[idx]; v[idx] = val; } -/** - * Range sum query of v[l..r]. - * @param l,r range - */ +/// Range sum query of v[l..r]. +/// @param l,r range int query(int l, int r) { int ans = 0; @@ -44,11 +38,9 @@ int query(int l, int r) { return ans; } -/** - * Fills block array with necessary data to perform update and query in - * less than linear time. - * @param n number of elements of v - */ +/// Fills block array with necessary data to perform update and query in +/// less than linear time. +/// @param n number of elements of v void preprocess(int n) { block_size = sqrt(n); diff --git a/algorithms/structure/trie.cpp b/algorithms/structure/trie.cpp index 4bae5fc141930b7f2e7c999b6c1319c258bf32ad..785bcff1e618e6269e38d330eb51f0a3bd785620 100644 --- a/algorithms/structure/trie.cpp +++ b/algorithms/structure/trie.cpp @@ -1,18 +1,16 @@ -/** - * Trie - * - * Complexity (Time): - * Insert -> O(m) - * Search -> O(m) - * Complexity (Space): O(alphabet_size * N) - */ - -// ========== Trie for String ========== +/// Trie +/// +/// Complexity (Time): +/// Insert -> O(m) +/// Search -> O(m) +/// Complexity (Space): O(alphabet_size * N) + +/// ========== Trie for String ========== struct Trie { int N, states; - // ending[i] is true when the node i represents the last - // char of a string contained in the Trie + /// ending[i] is true when the node i represents the last + /// char of a string contained in the Trie vector<int> ending; vector<vector<int>> trie; @@ -26,9 +24,7 @@ struct Trie { fill(all(i), -1); } - /** - * Inserts word into the Trie. - */ + /// Inserts word into the Trie. void insert(string word) { int node = 0; @@ -43,9 +39,7 @@ struct Trie { ending[node] = true; } - /** - * Returns true if word is in the Trie. - */ + /// Returns true if word is in the Trie. bool search(string word) { int node = 0; @@ -60,12 +54,12 @@ struct Trie { }; -// ========== Trie for Integer ========== +/// ========== Trie for Integer ========== struct Trie { int N, states; - // ending[i] is true when the node i represents the last - // bit of an integer contained in the Trie + /// ending[i] is true when the node i represents the last + /// bit of an integer contained in the Trie vector<int> ending; vector<vector<int>> trie; @@ -79,9 +73,7 @@ struct Trie { fill(all(i), -1); } - /** - * Inserts x into the Trie. - */ + /// Inserts x into the Trie. void insert(int x) { int node = 0; @@ -96,9 +88,7 @@ struct Trie { ending[node] = true; } - /** - * Returns true if x is in the Trie. - */ + /// Returns true if x is in the Trie. bool search(int x) { int node = 0; diff --git a/contests/ICPC_LA17/C.cpp b/contests/ICPC_LA17/C.cpp index ca3d8cab7bfdf414f4bb7b7a8b0a3e5c197cfe51..2e49e7140491dcd570b655e78d77d2c5ef95c969 100644 --- a/contests/ICPC_LA17/C.cpp +++ b/contests/ICPC_LA17/C.cpp @@ -16,8 +16,8 @@ using namespace std; -typedef long long ll; -typedef pair<int,int> ii; +using ll = long long; +using ii = pair<int,int>; int main() { ios::sync_with_stdio(0); diff --git a/contests/ICPC_LA17/E.cpp b/contests/ICPC_LA17/E.cpp index e6575a3af3fe880483a990d96d54604c9dd043a1..8eaa2c7bf98bc582e7aad8085f4f700249dd2866 100644 --- a/contests/ICPC_LA17/E.cpp +++ b/contests/ICPC_LA17/E.cpp @@ -17,8 +17,8 @@ using namespace std; -typedef long long ll; -typedef pair<int,int> ii; +using ll = long long; +using ii = pair<int,int>; int n; string s; diff --git a/contests/ICPC_LA17/F.cpp b/contests/ICPC_LA17/F.cpp new file mode 100644 index 0000000000000000000000000000000000000000..52ca75196676a64be7197a709236f1926cf7c1b0 --- /dev/null +++ b/contests/ICPC_LA17/F.cpp @@ -0,0 +1,49 @@ +#include <bits/stdc++.h> + +#define EPS 1e-6 +#define MOD 1000000007 +#define inf 0x3f3f3f3f +#define llinf 0x3f3f3f3f3f3f3f3f + +#define fi first +#define se second +#define pb push_back +#define ende '\n' + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define mset(x, y) memset(&x, (y), sizeof(x)) + +using namespace std; + +using ll = long long; +using ii = pair<ll,ll>; +using iii = pair<ii,ll>; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + map<ii,ll> M; + int n; cin >> n; + for (int i = 0; i < n; ++i) { + ll a, b, c; cin >> a >> b >> c; + M[ii(a, b)] += c; + } + + vector<iii> v; + for (auto i : M) { + v.pb(iii(ii(i.fi.se, i.fi.se), i.se)); + } + + return 0; +} + +2 8 13 +1 4 12 +2 1 16 + + +1 4 12 +2 8 13 +2 1 16 diff --git a/contests/ICPC_LA17/H.cpp b/contests/ICPC_LA17/H.cpp index f4449e79a226cd9599ce9bf1105d2059cb913929..18994d25f4690b3f3369ee2f2514a6f90eb58957 100644 --- a/contests/ICPC_LA17/H.cpp +++ b/contests/ICPC_LA17/H.cpp @@ -16,8 +16,8 @@ using namespace std; -typedef long long ll; -typedef pair<int,int> ii; +using ll = long long; +using ii = pair<int,int>; int main() { ios::sync_with_stdio(0); diff --git a/contests/ICPC_LA17/I.cpp b/contests/ICPC_LA17/I.cpp index c49145e12a492ca9ec1292a045c78f0015d83167..2ccdb39856c8c5701de2cd853e8bc2eccc3a27c1 100644 --- a/contests/ICPC_LA17/I.cpp +++ b/contests/ICPC_LA17/I.cpp @@ -17,9 +17,9 @@ using namespace std; -typedef long long ll; -typedef pair<int,int> ii; -typedef pair<ii,int> iii; +using ll = long long; +using ii = pair<int,int>; +using iii = pair<ii,int>; map<ii, bool> mst; vector<ii> graph[MAX]; diff --git a/contests/ICPC_LA17/J.cpp b/contests/ICPC_LA17/J.cpp index 48ecb03fba07abdbf57c8661f043b4ea0ae5f563..d8ced42ce9e5e22e94738bd32e93540a200f7934 100644 --- a/contests/ICPC_LA17/J.cpp +++ b/contests/ICPC_LA17/J.cpp @@ -16,19 +16,18 @@ using namespace std; -typedef long long ll; -typedef pair<int,int> ii; +using ll = long long; +using ii = pair<int,int>; int main() { ios::sync_with_stdio(0); cin.tie(0); string s; cin >> s; - int n = s.size(); - - int r = 0; + int r = 0, n = s.size(); for (int i = 0; i < n; ++i) r += (s[i] == 'R'); + if (r == n) return cout << n - 1 << ende, 0;