未验证 提交 d9fc8789 编写于 作者: M matgrz1993 提交者: GitHub

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: Ngithub-actions <${GITHUB_ACTOR}@users.noreply.github.com>
上级 0f5e8803
......@@ -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)
......
// Binary Indexed Tree.
/**
* @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>
using namespace std;
class Bit {
/**
* n --> No. of elements present in input array.
* bit[0..n] --> Array that represents Binary Indexed Tree.
*/
class FenwickTree {
int n;
vector<int> bit;
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:
Bit(vector<int>& arr) {
/** 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]);
}
}
Bit(int x) {
/** 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) {
// Add val at id
id++;
while (id <= n) {
bit[id] += val;
......@@ -30,8 +50,8 @@ class Bit {
}
}
/** Get prefix sum upto id */
int sum(int id) {
// Get prefix sum upto id.
id++;
int res = 0;
while (id > 0) {
......@@ -41,20 +61,22 @@ class Bit {
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;
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);
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;
}
#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;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册