diff --git a/graph/bfs.cpp b/graph/bfs.cpp index c9d9f0e4cce295d9f6a26234f60bd7f2a78ffaa3..673082432531c9367df5565ed2e1555f6fabe7cb 100644 --- a/graph/bfs.cpp +++ b/graph/bfs.cpp @@ -1,3 +1,9 @@ +/** + * Breadth First Search - BFS + * Complexity (Time): O(|V| + |E|) + * Complexity (Space): O(|V| + |E|) + */ + bool cont[MAX]; vector<int> graph[MAX]; diff --git a/graph/dfs.cpp b/graph/dfs.cpp index 7a1680cff4c89fd3e41bf83982ac41b786f8ed68..e01c0ca3ebfcf4f3454c53d6e97fa4543c15e1be 100644 --- a/graph/dfs.cpp +++ b/graph/dfs.cpp @@ -1,3 +1,9 @@ +/** + * Depth First Search - DFS + * Complexity (Time): O(|V| + |E|) + * Complexity (Space): O(|V| + |E|) + */ + bool cont[MAX]; vector<int> graph[MAX]; diff --git a/structure/bit.cpp b/structure/bit.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73165d35b594c0ac3b5f21e789a510f1b21821c4 --- /dev/null +++ b/structure/bit.cpp @@ -0,0 +1,25 @@ +/** + * Binary Indexed Tree - BIT + * Complexity (Time): + * Update -> O(log n) + * Query -> O(log n) + * + * Complexity (Space): O(n) + */ + +int tree[MAX]; + +// Perform query in array (tree) in the idx position +int query(int idx) { + int sum = 0; + for (; idx > 0; idx -= (idx & -idx)) + sum += tree[idx]; + + return sum; +} + +// Add a value (val) to a single position (idx) in the array (tree). +void update(int idx, int val) { + for (; idx <= MAX; idx += (idx & -idx)) + tree[idx] += val; +} diff --git a/structure/bit2d.cpp b/structure/bit2d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..917cd972929992695260d052a79aa1cb4f05269e --- /dev/null +++ b/structure/bit2d.cpp @@ -0,0 +1,28 @@ +/** + * Binary Indexed Tree 2D - BIT2D + * Complexity (Time): + * Update -> O(log^2 n) + * Query -> O(log^2 n) + * + * Complexity (Space): O(n^2) + */ + +int tree[MAXN][MAXM]; + +// Perform query in array (tree) in the (idx,idy) position +int query(int idx, int idy) { + int sum = 0, m; + for (; idx > 0; idx -= (idx & -idx)) + for (m = idy; m > 0; m -= (m & -m)) + sum += tree[idx][m]; + + return sum; +} + +// Add a value (val) to a single position (idx,idy) in the array (tree). +void update(int idx, int idy, int val) { + int m; + for (; idx <= MAXN; idx += (idx & -idx)) + for (m = idy; m <= MAXM; m += (m & -m)) + tree[idx][m] += val; +}