mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-04 11:09:52 +08:00
fix: Remove FenwickTree (#856)
* Remove FenwickTree
FenwickTree is the same Data Structure as Binary Indexed Tree located in file "range_queries/bit.cpp" so it could be removed.
* Fix cpplint errors
* docs: Update documentation
* docs: Update FenwickTree documentation
* updating DIRECTORY.md
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@@ -181,8 +181,7 @@
|
||||
* [Poisson Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/probability/poisson_dist.cpp)
|
||||
|
||||
## Range Queries
|
||||
* [Bit](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/range_queries/bit.cpp)
|
||||
* [Fenwicktree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/range_queries/fenwicktree.cpp)
|
||||
* [Fenwick Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/range_queries/fenwick_tree.cpp)
|
||||
* [Mo](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/range_queries/mo.cpp)
|
||||
* [Segtree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/range_queries/segtree.cpp)
|
||||
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
// Binary Indexed Tree.
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Bit {
|
||||
int n;
|
||||
vector<int> bit;
|
||||
inline int offset(int x) { return (x & (-x)); }
|
||||
|
||||
public:
|
||||
Bit(vector<int>& arr) {
|
||||
n = arr.size();
|
||||
bit.assign(n + 1, 0);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
update(i, arr[i]);
|
||||
}
|
||||
}
|
||||
Bit(int x) {
|
||||
n = x;
|
||||
bit.assign(n + 1, 0);
|
||||
}
|
||||
|
||||
void update(int id, int val) {
|
||||
// Add val at id
|
||||
id++;
|
||||
while (id <= n) {
|
||||
bit[id] += val;
|
||||
id += offset(id);
|
||||
}
|
||||
}
|
||||
|
||||
int sum(int id) {
|
||||
// Get prefix sum upto id.
|
||||
id++;
|
||||
int res = 0;
|
||||
while (id > 0) {
|
||||
res += bit[id];
|
||||
id -= offset(id);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int sum_range(int l, int r) { return sum(r) - sum(l - 1); }
|
||||
};
|
||||
|
||||
int main() {
|
||||
int n = 5;
|
||||
vector<int> arr = {1, 2, 3, 4, 5};
|
||||
Bit x(arr);
|
||||
|
||||
assert(x.sum_range(0, 0) == 1);
|
||||
assert(x.sum_range(0, 1) == 3);
|
||||
assert(x.sum_range(0, 2) == 6);
|
||||
x.update(0, 6);
|
||||
assert(x.sum_range(0, 0) == 6);
|
||||
assert(x.sum_range(0, 1) == 8);
|
||||
assert(x.sum_range(0, 2) == 11);
|
||||
return 0;
|
||||
}
|
||||
82
range_queries/fenwick_tree.cpp
Normal file
82
range_queries/fenwick_tree.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Fenwick tree
|
||||
*
|
||||
* A Fenwick tree or binary indexed tree is a data structure
|
||||
* that can efficiently update elements and calculate
|
||||
* prefix sums in a table of numbers.
|
||||
*/
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* n --> No. of elements present in input array.
|
||||
* bit[0..n] --> Array that represents Binary Indexed Tree.
|
||||
*/
|
||||
class FenwickTree {
|
||||
int n;
|
||||
std::vector<int> bit;
|
||||
|
||||
/** Returns the highest power of two which is not more than x */
|
||||
inline int offset(int x) { return (x & (-x)); }
|
||||
|
||||
public:
|
||||
/** Constructor
|
||||
* \param[in] arr --> Input array for which prefix sum is evaluated.
|
||||
*/
|
||||
explicit FenwickTree(const std::vector<int>& arr) {
|
||||
n = arr.size();
|
||||
bit.assign(n + 1, 0);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
update(i, arr[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/** Constructor
|
||||
* \param[in] x --> Size of array that represents Binary Indexed Tree.
|
||||
*/
|
||||
explicit FenwickTree(int x) {
|
||||
n = x;
|
||||
bit.assign(n + 1, 0);
|
||||
}
|
||||
|
||||
/** Add val at id */
|
||||
void update(int id, int val) {
|
||||
id++;
|
||||
while (id <= n) {
|
||||
bit[id] += val;
|
||||
id += offset(id);
|
||||
}
|
||||
}
|
||||
|
||||
/** Get prefix sum upto id */
|
||||
int sum(int id) {
|
||||
id++;
|
||||
int res = 0;
|
||||
while (id > 0) {
|
||||
res += bit[id];
|
||||
id -= offset(id);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/** Returns the prefix sum in range from l to r */
|
||||
int sum_range(int l, int r) { return sum(r) - sum(l - 1); }
|
||||
};
|
||||
|
||||
/** Main function */
|
||||
int main() {
|
||||
int n = 5;
|
||||
std::vector<int> arr = {1, 2, 3, 4, 5};
|
||||
FenwickTree fenwick_tree(arr);
|
||||
|
||||
assert(fenwick_tree.sum_range(0, 0) == 1);
|
||||
assert(fenwick_tree.sum_range(0, 1) == 3);
|
||||
assert(fenwick_tree.sum_range(0, 2) == 6);
|
||||
fenwick_tree.update(0, 6);
|
||||
assert(fenwick_tree.sum_range(0, 0) == 6);
|
||||
assert(fenwick_tree.sum_range(0, 1) == 8);
|
||||
assert(fenwick_tree.sum_range(0, 2) == 11);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
* ` lowbit(x) ` aims to find the last 1 in binary of a positive number
|
||||
* twos complement works good on this
|
||||
* also using ` x - (x & (x - 1)) `
|
||||
*/
|
||||
#define lowbit(x) (x & (-x))
|
||||
|
||||
const int maxn = 1e5 + 7;
|
||||
int tree[maxn] = {0},
|
||||
range; // segement of [1...range], notice it must be less than `maxn`
|
||||
|
||||
void update(int x, int c) {
|
||||
while (x <= range) {
|
||||
tree[x] += c;
|
||||
x += lowbit(x);
|
||||
}
|
||||
}
|
||||
int query(int x) {
|
||||
int ans = 0;
|
||||
while (x) {
|
||||
ans += tree[x];
|
||||
x -= lowbit(x);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
int query_segement(int l, int r) { return query(r) - query(l - 1); }
|
||||
|
||||
int main() {
|
||||
cin >> range;
|
||||
for (int i = 1; i <= range; i++) {
|
||||
int num;
|
||||
cin >> num;
|
||||
update(i, num);
|
||||
}
|
||||
int q;
|
||||
cin >> q;
|
||||
while (q--) {
|
||||
int op;
|
||||
cin >> op;
|
||||
if (op == 0) {
|
||||
int l, r;
|
||||
cin >> l >> r;
|
||||
cout << query_segement(l, r) << endl;
|
||||
} else {
|
||||
int x, c;
|
||||
cin >> x >> c;
|
||||
update(x, c);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user