diff --git a/1-data-structure/fenwick_tree.cpp b/refcode/1-ds/fenwick_tree.hpp similarity index 97% rename from 1-data-structure/fenwick_tree.cpp rename to refcode/1-ds/fenwick_tree.hpp index 2af83ac..ace7525 100644 --- a/1-data-structure/fenwick_tree.cpp +++ b/refcode/1-ds/fenwick_tree.hpp @@ -1,5 +1,5 @@ #include "../common/common.hpp" - +namespace refcode { // 1. Fenwick Tree struct Fenwick { // 0-indexed int flag, cnt; // array size @@ -23,7 +23,7 @@ struct Fenwick { // 0-indexed } void modify(int p, ll value) { // set value at position p add(p, value - arr[p]); - }; + } ll query(int x) { ll ret = 0; while (x >= 0) ret += t[x], x = (x & (x + 1)) - 1; @@ -43,10 +43,9 @@ struct Fenwick { // 0-indexed } return l; } -} fw; - +}; // 2. Fenwick Tree Range Update Point Query -struct Fenwick { // 1-indexed +struct FenwickRUPQ { // 1-indexed int flag; vector t; void build(int N) { @@ -62,8 +61,7 @@ struct Fenwick { // 1-indexed for (; x; x ^= x & -x) ret += t[x]; return ret; } -} fw; - +}; // 3. 2D Fenwick Tree // INPUT: Given an 2D array of integers of size N * M. // Can modify the value of the (x, y)th element. @@ -130,4 +128,5 @@ struct Fenwick2D { // 0-indexed if (sx && sy) ret += query(sx - 1, sy - 1); return ret; } -} fw2d; \ No newline at end of file +}; +} // namespace refcode \ No newline at end of file diff --git a/1-data-structure/li_chao_tree.cpp b/refcode/1-ds/li_chao_tree.hpp similarity index 93% rename from 1-data-structure/li_chao_tree.cpp rename to refcode/1-ds/li_chao_tree.hpp index 728add8..4fc8b3c 100644 --- a/1-data-structure/li_chao_tree.cpp +++ b/refcode/1-ds/li_chao_tree.hpp @@ -1,14 +1,13 @@ #include "../common/common.hpp" - +namespace refcode { // INPUT: Initially, a 2d plane in which no linear function exists is given. // Two types of queries are given. // 1 a b : The linear function f(x) = ax + b is added. // 2 x : Find the max(f(x)) among the linear functions given so far. // OUTPUT: For each query 2 x, output the max(f(x)) among the linear functions given so far. // TIME COMPLEXITY: O(qlogq) -#define Line pair - -const Line e = {0, -1e18}; +using Line = pair; +constexpr Line e = {0, -1e18}; struct LiChaoTree { ll f(Line l, ll x) { return l.first * x + l.second; } struct Node { @@ -56,4 +55,5 @@ struct LiChaoTree { else ret = max(ret, query(x, t[n].r)); return ret; } -} lct; \ No newline at end of file +}; +} // namespace refcode \ No newline at end of file diff --git a/1-data-structure/merge_sort_tree.cpp b/refcode/1-ds/merge_sort_tree.hpp similarity index 94% rename from 1-data-structure/merge_sort_tree.cpp rename to refcode/1-ds/merge_sort_tree.hpp index b3c3644..7f330ac 100644 --- a/1-data-structure/merge_sort_tree.cpp +++ b/refcode/1-ds/merge_sort_tree.hpp @@ -1,10 +1,10 @@ #include "../common/common.hpp" - +namespace refcode { +constexpr int MAX_MST = 1 << 17; // 1. Merge Sort Tree // INPUT: Given a sequence A_1, A_2, ..., A_n of length n. Given a query (i, j, k). // OUTPUT: For each query (i, j, k), output the number of elements greater than k among elements A_i, A_{i+1}, ..., A_j. // TIME COMPLEXITY: O(nlogn) for initialize Merge Sort Tree, O(log^2n) for each query. -const int MAX_MST = 1 << 17; struct MergeSortTree { vector t[MAX_MST << 1]; void build(const vector &arr) { @@ -22,14 +22,12 @@ struct MergeSortTree { int mid = (nl + nr) >> 1; return query(l, r, k, n << 1, nl, mid) + query(l, r, k, n << 1 | 1, mid + 1, nr); } -} mstree; - +}; // 2. Iterative Merge Sort Tree // INPUT: Given a sequence A_1, A_2, ..., A_n of length n. Given a query (i, j, k). // OUTPUT: For each query (i, j, k), output the number of elements greater than k among elements A_i, A_{i+1}, ..., A_j. // TIME COMPLEXITY: O(nlogn) for initialize Merge Sort Tree, O(log^2n) for each query. -const int MAX_MST = 1 << 17; -struct MergeSortTree { +struct MergeSortTreeIter { vector t[MAX_MST << 1]; void build(const vector &arr) { for (int i = 0; i < sz(arr); i++) @@ -49,4 +47,5 @@ struct MergeSortTree { } return ret; } -} mstree; \ No newline at end of file +}; +} // namespace refcode \ No newline at end of file diff --git a/1-data-structure/segment_tree.cpp b/refcode/1-ds/segment_tree.hpp similarity index 95% rename from 1-data-structure/segment_tree.cpp rename to refcode/1-ds/segment_tree.hpp index 13dc4a9..9149260 100644 --- a/1-data-structure/segment_tree.cpp +++ b/refcode/1-ds/segment_tree.hpp @@ -1,7 +1,7 @@ #include "../common/common.hpp" - +namespace refcode { +int flag; // array size // 1. Segment Tree -int flag; // array size struct Seg { // 1-indexed vector t; void build(int n) { @@ -19,12 +19,11 @@ struct Seg { // 1-indexed int mid = (nl + nr) / 2; return query(l, r, n << 1, nl, mid) + query(l, r, n << 1 | 1, mid + 1, nr); } -} seg; - +}; // 2. Iterative Segment Tree -const int MAXN = 1010101; // limit for array size -struct Seg { // 0-indexed - int n; // array size +constexpr int MAXN = 1010101; // limit for array size +struct SegIter { // 0-indexed + int n; // array size ll t[2 * MAXN]; void build(int N) { n = N; @@ -42,11 +41,9 @@ struct Seg { // 0-indexed } return ret; } -} seg; - +}; // 3. k-th Segment Tree -int flag; // array size -struct Seg { +struct SegKth { vector t; void build(int n) { for (flag = 1; flag < n; flag <<= 1); @@ -61,11 +58,9 @@ struct Seg { if (k <= t[n << 1]) return kth(k, n << 1); else return kth(k - t[n << 1], n << 1 | 1); } -} seg; - +}; // 4. Segment Tree with Lazy Propagation -int flag; // array size -struct Seg { // 1-indexed +struct SegLazy { // 1-indexed vector t, lazy; void build(int n) { for (flag = 1; flag < n; flag <<= 1); @@ -105,8 +100,7 @@ struct Seg { // 1-indexed lazy[n] = 0; } } -} seg; - +}; // 5. Persistent Segment Tree // TIME COMPLEXITY: O(n) for initialize PST, O(logn) for each query. // SPACE COMPLEXITY: O(nlogm). @@ -153,7 +147,6 @@ struct PST { // 1-indexed t[n2].val = val; return; } - int mid = (l + r) >> 1; if (p <= mid) { t[n2].r = t[n1].r; @@ -184,17 +177,14 @@ struct PST { // 1-indexed assert(n < sz(root)); return query(l, r, root[n], 1, flag); } -} pst; - +}; // 6. Dynamic Segment Tree -const int MAXL = 1, MAXR = 1000000; - +constexpr int MAXL = 1, MAXR = 1000000; struct Node { ll x; int l, r; }; - -struct Dyseg { +struct DySeg { vector t = {{0, -1, -1}, {0, -1, -1}}; void modify(int p, ll x, int n = 1, int nl = MAXL, int nr = MAXR) { if (p < nl || nr < p) return; @@ -237,4 +227,5 @@ struct Dyseg { } return ret; } -} dyseg; +}; +} // namespace refcode \ No newline at end of file diff --git a/1-data-structure/union_find.cpp b/refcode/1-ds/union_find.hpp similarity index 75% rename from 1-data-structure/union_find.cpp rename to refcode/1-ds/union_find.hpp index a63d151..79791ed 100644 --- a/1-data-structure/union_find.cpp +++ b/refcode/1-ds/union_find.hpp @@ -1,10 +1,10 @@ #include "../common/common.hpp" - +namespace refcode { struct UF { vector uf; - void build(int N) { + void build(int n) { uf.clear(); - uf.resize(N + 1, -1); + uf.resize(n + 1, -1); } int find(int v) { if (uf[v] < 0) return v; @@ -16,4 +16,5 @@ struct UF { uf[U] += uf[V]; uf[V] = U; } -} uf; \ No newline at end of file +}; +} // namespace refcode \ No newline at end of file diff --git a/2-graph/bcc.cpp b/refcode/2-graph/bcc.cpp similarity index 100% rename from 2-graph/bcc.cpp rename to refcode/2-graph/bcc.cpp diff --git a/2-graph/dijkstra_bellman_ford_floyd_warshall.cpp b/refcode/2-graph/dijkstra_bellman_ford_floyd_warshall.cpp similarity index 100% rename from 2-graph/dijkstra_bellman_ford_floyd_warshall.cpp rename to refcode/2-graph/dijkstra_bellman_ford_floyd_warshall.cpp diff --git a/2-graph/euler_circuit.cpp b/refcode/2-graph/euler_circuit.cpp similarity index 100% rename from 2-graph/euler_circuit.cpp rename to refcode/2-graph/euler_circuit.cpp diff --git a/2-graph/kth_shortest_path.cpp b/refcode/2-graph/kth_shortest_path.cpp similarity index 100% rename from 2-graph/kth_shortest_path.cpp rename to refcode/2-graph/kth_shortest_path.cpp diff --git a/2-graph/offline_dynamic_connectivity.cpp b/refcode/2-graph/offline_dynamic_connectivity.cpp similarity index 100% rename from 2-graph/offline_dynamic_connectivity.cpp rename to refcode/2-graph/offline_dynamic_connectivity.cpp diff --git a/2-graph/scc_2_sat.cpp b/refcode/2-graph/scc_2_sat.cpp similarity index 100% rename from 2-graph/scc_2_sat.cpp rename to refcode/2-graph/scc_2_sat.cpp diff --git a/2-graph/tree_cd.cpp b/refcode/2-graph/tree_cd.cpp similarity index 100% rename from 2-graph/tree_cd.cpp rename to refcode/2-graph/tree_cd.cpp diff --git a/2-graph/tree_composition.cpp b/refcode/2-graph/tree_composition.cpp similarity index 100% rename from 2-graph/tree_composition.cpp rename to refcode/2-graph/tree_composition.cpp diff --git a/2-graph/tree_exchange_argument.cpp b/refcode/2-graph/tree_exchange_argument.cpp similarity index 100% rename from 2-graph/tree_exchange_argument.cpp rename to refcode/2-graph/tree_exchange_argument.cpp diff --git a/2-graph/tree_hld.cpp b/refcode/2-graph/tree_hld.cpp similarity index 100% rename from 2-graph/tree_hld.cpp rename to refcode/2-graph/tree_hld.cpp diff --git a/2-graph/tree_lca_sparse_table.cpp b/refcode/2-graph/tree_lca_sparse_table.cpp similarity index 100% rename from 2-graph/tree_lca_sparse_table.cpp rename to refcode/2-graph/tree_lca_sparse_table.cpp diff --git a/3-network-flow/bipartite_matching.cpp b/refcode/3-flow/bipartite_matching.cpp similarity index 100% rename from 3-network-flow/bipartite_matching.cpp rename to refcode/3-flow/bipartite_matching.cpp diff --git a/3-network-flow/dinics_algorithm.cpp b/refcode/3-flow/dinics_algorithm.cpp similarity index 100% rename from 3-network-flow/dinics_algorithm.cpp rename to refcode/3-flow/dinics_algorithm.cpp diff --git a/3-network-flow/hopcroft_karp_algorithm.cpp b/refcode/3-flow/hopcroft_karp_algorithm.cpp similarity index 100% rename from 3-network-flow/hopcroft_karp_algorithm.cpp rename to refcode/3-flow/hopcroft_karp_algorithm.cpp diff --git a/3-network-flow/maximum_flow.cpp b/refcode/3-flow/maximum_flow.cpp similarity index 100% rename from 3-network-flow/maximum_flow.cpp rename to refcode/3-flow/maximum_flow.cpp diff --git a/3-network-flow/mcmf.cpp b/refcode/3-flow/mcmf.cpp similarity index 100% rename from 3-network-flow/mcmf.cpp rename to refcode/3-flow/mcmf.cpp diff --git a/4-string/aho_corasick.cpp b/refcode/4-string/aho_corasick.cpp similarity index 100% rename from 4-string/aho_corasick.cpp rename to refcode/4-string/aho_corasick.cpp diff --git a/4-string/kmp_algorithm.cpp b/refcode/4-string/kmp_algorithm.cpp similarity index 100% rename from 4-string/kmp_algorithm.cpp rename to refcode/4-string/kmp_algorithm.cpp diff --git a/4-string/manachers_algorithm.cpp b/refcode/4-string/manachers_algorithm.cpp similarity index 100% rename from 4-string/manachers_algorithm.cpp rename to refcode/4-string/manachers_algorithm.cpp diff --git a/4-string/rabin_karp_algorithm.cpp b/refcode/4-string/rabin_karp_algorithm.cpp similarity index 100% rename from 4-string/rabin_karp_algorithm.cpp rename to refcode/4-string/rabin_karp_algorithm.cpp diff --git a/4-string/suffix_array.cpp b/refcode/4-string/suffix_array.cpp similarity index 100% rename from 4-string/suffix_array.cpp rename to refcode/4-string/suffix_array.cpp diff --git a/4-string/trie.cpp b/refcode/4-string/trie.cpp similarity index 100% rename from 4-string/trie.cpp rename to refcode/4-string/trie.cpp diff --git a/4-string/z_algorithm.cpp b/refcode/4-string/z_algorithm.cpp similarity index 100% rename from 4-string/z_algorithm.cpp rename to refcode/4-string/z_algorithm.cpp diff --git a/5-geometry/bulldozer_trick.cpp b/refcode/5-geometry/bulldozer_trick.cpp similarity index 100% rename from 5-geometry/bulldozer_trick.cpp rename to refcode/5-geometry/bulldozer_trick.cpp diff --git a/5-geometry/ccw_algorithm.cpp b/refcode/5-geometry/ccw_algorithm.cpp similarity index 100% rename from 5-geometry/ccw_algorithm.cpp rename to refcode/5-geometry/ccw_algorithm.cpp diff --git a/5-geometry/convex_hull.cpp b/refcode/5-geometry/convex_hull.cpp similarity index 100% rename from 5-geometry/convex_hull.cpp rename to refcode/5-geometry/convex_hull.cpp diff --git a/5-geometry/minimum_enclosing_circle.cpp b/refcode/5-geometry/minimum_enclosing_circle.cpp similarity index 100% rename from 5-geometry/minimum_enclosing_circle.cpp rename to refcode/5-geometry/minimum_enclosing_circle.cpp diff --git a/5-geometry/ray_casting.cpp b/refcode/5-geometry/ray_casting.cpp similarity index 100% rename from 5-geometry/ray_casting.cpp rename to refcode/5-geometry/ray_casting.cpp diff --git a/5-geometry/rotating_callipers.cpp b/refcode/5-geometry/rotating_callipers.cpp similarity index 100% rename from 5-geometry/rotating_callipers.cpp rename to refcode/5-geometry/rotating_callipers.cpp diff --git a/5-geometry/sort_by_angular.cpp b/refcode/5-geometry/sort_by_angular.cpp similarity index 100% rename from 5-geometry/sort_by_angular.cpp rename to refcode/5-geometry/sort_by_angular.cpp diff --git a/6-math/basic_sqrt_time_algorithms.cpp b/refcode/6-math/basic_sqrt_time_algorithms.cpp similarity index 100% rename from 6-math/basic_sqrt_time_algorithms.cpp rename to refcode/6-math/basic_sqrt_time_algorithms.cpp diff --git a/6-math/binomial_coefficient.cpp b/refcode/6-math/binomial_coefficient.cpp similarity index 100% rename from 6-math/binomial_coefficient.cpp rename to refcode/6-math/binomial_coefficient.cpp diff --git a/6-math/catalan_number_derangement_number.cpp b/refcode/6-math/catalan_number_derangement_number.cpp similarity index 100% rename from 6-math/catalan_number_derangement_number.cpp rename to refcode/6-math/catalan_number_derangement_number.cpp diff --git a/6-math/chinese_remainder_theorem.cpp b/refcode/6-math/chinese_remainder_theorem.cpp similarity index 100% rename from 6-math/chinese_remainder_theorem.cpp rename to refcode/6-math/chinese_remainder_theorem.cpp diff --git a/6-math/euclidean_algorithms.cpp b/refcode/6-math/euclidean_algorithms.cpp similarity index 100% rename from 6-math/euclidean_algorithms.cpp rename to refcode/6-math/euclidean_algorithms.cpp diff --git a/6-math/eulers_phi_function.cpp b/refcode/6-math/eulers_phi_function.cpp similarity index 100% rename from 6-math/eulers_phi_function.cpp rename to refcode/6-math/eulers_phi_function.cpp diff --git a/6-math/fft.cpp b/refcode/6-math/fft.cpp similarity index 100% rename from 6-math/fft.cpp rename to refcode/6-math/fft.cpp diff --git a/6-math/gauss_jordan_elimination.cpp b/refcode/6-math/gauss_jordan_elimination.cpp similarity index 100% rename from 6-math/gauss_jordan_elimination.cpp rename to refcode/6-math/gauss_jordan_elimination.cpp diff --git a/6-math/matrix.cpp b/refcode/6-math/matrix.cpp similarity index 100% rename from 6-math/matrix.cpp rename to refcode/6-math/matrix.cpp diff --git a/6-math/miller_rabin_pollard_rho.cpp b/refcode/6-math/miller_rabin_pollard_rho.cpp similarity index 100% rename from 6-math/miller_rabin_pollard_rho.cpp rename to refcode/6-math/miller_rabin_pollard_rho.cpp diff --git a/6-math/mobius.cpp b/refcode/6-math/mobius.cpp similarity index 100% rename from 6-math/mobius.cpp rename to refcode/6-math/mobius.cpp diff --git a/6-math/sieve.cpp b/refcode/6-math/sieve.cpp similarity index 100% rename from 6-math/sieve.cpp rename to refcode/6-math/sieve.cpp diff --git a/7-misc/dp_opt.cpp b/refcode/7-misc/dp_opt.cpp similarity index 100% rename from 7-misc/dp_opt.cpp rename to refcode/7-misc/dp_opt.cpp diff --git a/7-misc/fraction_data_type.cpp b/refcode/7-misc/fraction_data_type.cpp similarity index 100% rename from 7-misc/fraction_data_type.cpp rename to refcode/7-misc/fraction_data_type.cpp diff --git a/7-misc/kitamasa.cpp b/refcode/7-misc/kitamasa.cpp similarity index 100% rename from 7-misc/kitamasa.cpp rename to refcode/7-misc/kitamasa.cpp diff --git a/7-misc/lis_in_o_nlogn.cpp b/refcode/7-misc/lis_in_o_nlogn.cpp similarity index 100% rename from 7-misc/lis_in_o_nlogn.cpp rename to refcode/7-misc/lis_in_o_nlogn.cpp diff --git a/7-misc/random.cpp b/refcode/7-misc/random.cpp similarity index 100% rename from 7-misc/random.cpp rename to refcode/7-misc/random.cpp diff --git a/7-misc/rotation_matrix_manhattan_distance_chebyshev_distance.txt b/refcode/7-misc/rotation_matrix_manhattan_distance_chebyshev_distance.txt similarity index 100% rename from 7-misc/rotation_matrix_manhattan_distance_chebyshev_distance.txt rename to refcode/7-misc/rotation_matrix_manhattan_distance_chebyshev_distance.txt diff --git a/7-misc/simd.cpp b/refcode/7-misc/simd.cpp similarity index 100% rename from 7-misc/simd.cpp rename to refcode/7-misc/simd.cpp diff --git a/7-misc/sqrt_decomposition_mos_algorithm.cpp b/refcode/7-misc/sqrt_decomposition_mos_algorithm.cpp similarity index 100% rename from 7-misc/sqrt_decomposition_mos_algorithm.cpp rename to refcode/7-misc/sqrt_decomposition_mos_algorithm.cpp diff --git a/7-misc/system_of_difference_constraints.cpp b/refcode/7-misc/system_of_difference_constraints.cpp similarity index 100% rename from 7-misc/system_of_difference_constraints.cpp rename to refcode/7-misc/system_of_difference_constraints.cpp diff --git a/7-misc/ternary_search.cpp b/refcode/7-misc/ternary_search.cpp similarity index 100% rename from 7-misc/ternary_search.cpp rename to refcode/7-misc/ternary_search.cpp diff --git a/common/common.hpp b/refcode/common/common.hpp similarity index 100% rename from common/common.hpp rename to refcode/common/common.hpp index 3321f91..aa84e02 100644 --- a/common/common.hpp +++ b/refcode/common/common.hpp @@ -7,7 +7,7 @@ using ll = long long; using pii = pair; using pll = pair; -#define sz(x) (int)(x).size() -#define all(x) (x).begin(), (x).end() #define fr first #define sc second +#define sz(x) (int)(x).size() +#define all(x) (x).begin(), (x).end() diff --git a/test/.gitkeep b/test/.gitkeep new file mode 100644 index 0000000..e69de29