From 3656fba7cd9e13f5be603c57512952424b9577ea Mon Sep 17 00:00:00 2001 From: Bruno Freitas Tissei <bft15@inf.ufpr.br> Date: Thu, 1 Feb 2018 13:32:27 -0200 Subject: [PATCH] Add bit Signed-off-by: Bruno Freitas Tissei <bft15@inf.ufpr.br> --- graph/bfs.cpp | 6 ++++++ graph/dfs.cpp | 6 ++++++ structure/bit.cpp | 25 +++++++++++++++++++++++++ structure/bit2d.cpp | 28 ++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 structure/bit.cpp create mode 100644 structure/bit2d.cpp diff --git a/graph/bfs.cpp b/graph/bfs.cpp index c9d9f0e..6730824 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 7a1680c..e01c0ca 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 0000000..73165d3 --- /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 0000000..917cd97 --- /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; +} -- GitLab