Skip to content
Snippets Groups Projects
Commit 3656fba7 authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei
Browse files

Add bit

parent 4f53e640
No related branches found
No related tags found
No related merge requests found
/**
* Breadth First Search - BFS
* Complexity (Time): O(|V| + |E|)
* Complexity (Space): O(|V| + |E|)
*/
bool cont[MAX];
vector<int> graph[MAX];
......
/**
* Depth First Search - DFS
* Complexity (Time): O(|V| + |E|)
* Complexity (Space): O(|V| + |E|)
*/
bool cont[MAX];
vector<int> graph[MAX];
......
/**
* 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;
}
/**
* 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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment