Skip to content

Commit 705bd35

Browse files
authored
Merge pull request #407 from xirc/binary-indexed-tree
Redesign Binary Indexed Tree
2 parents f34b773 + 51dfeb1 commit 705bd35

18 files changed

+358
-780
lines changed

aoj/DSL/DSL2B/main_bit.cpp

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,71 @@
1-
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_B
1+
// https://onlinejudge.u-aizu.ac.jp/problems/DSL_2_B
22

3-
#include <vector>
4-
#include <functional>
3+
#include <bits/stdc++.h>
54

6-
template <class T = long long>
5+
template <class T = int64_t>
76
class BinaryIndexedTree {
87
public:
9-
using F = std::function<T(const T&, const T&)>;
8+
using F = std::function<T(T const&, T const&)>;
109

1110
protected:
12-
int N;
11+
size_t N;
1312
std::vector<T> bit;
14-
T id;
15-
F plus;
16-
F minus;
13+
T empty;
14+
F combine_func;
15+
F remove_func;
1716

1817
public:
19-
// O(N)
18+
// Time: O(N)
2019
BinaryIndexedTree(
21-
int n,
22-
T id = T(),
23-
F plus = std::plus<T>(),
24-
F minus = std::minus<T>()
20+
size_t n = 0,
21+
T empty = T(),
22+
F combine = std::plus<T>(),
23+
F remove = std::minus<T>()
2524
)
2625
: N(n+1)
27-
, bit(n+1, id)
28-
, id(id)
29-
, plus(plus)
30-
, minus(minus)
26+
, bit(n+1, empty)
27+
, empty(empty)
28+
, combine_func(combine)
29+
, remove_func(remove)
3130
{
3231
// Do nothing
3332
}
34-
// O(1)
35-
int size() {
33+
// Time: O(1)
34+
size_t size() const {
3635
return N - 1;
3736
}
38-
// Sum of array[0..index)
39-
// O(logN)
37+
// Fold elements of array[0..index)
4038
// index = [0,N]
41-
T sum(int index) {
42-
if (index < 0 || index > N) throw;
43-
T ans = id;
39+
// Time: O(logN)
40+
T fold(size_t index) const {
41+
if (index > N) throw std::out_of_range("index");
42+
T ans = empty;
4443
for (; index > 0; index -= index & -index) {
45-
ans = plus(ans, bit[index]);
44+
ans = combine_func(ans, bit[index]);
4645
}
4746
return ans;
4847
}
49-
// Sum of array[l, r)
50-
// O(logN)
51-
T sum(int l, int r) {
52-
if (l > r) throw;
53-
return minus(sum(r), sum(l));
48+
// Fold elements of array[l, r)
49+
// l = [0,N]
50+
// r = [l,N]
51+
// Time: O(logN)
52+
T fold(size_t l, size_t r) const {
53+
if (l > N) throw std::out_of_range("l");
54+
if (r < l || r > N) throw std::out_of_range("r");
55+
return remove_func(fold(r), fold(l));
5456
}
55-
// Add value to array[index]
56-
// O(logN)
57+
// Combine given value to array[index]
5758
// index = [0,N)
58-
void add(int index, const T& value) {
59-
if (index < 0 || index >= N) throw;
59+
// Time: O(logN)
60+
void combine(size_t index, T const& value) {
61+
if (index >= N) throw std::out_of_range("index");
6062
for (++index; index < N; index += index & -index) {
61-
bit[index] = plus(bit[index], value);
63+
bit[index] = combine_func(bit[index], value);
6264
}
6365
}
64-
// Set value to array[index]
65-
// O(logN)
66-
// index = [0,N)
67-
void set(int index, const T& value) {
68-
if (index < 0 || index >= N) throw;
69-
T new_value = minus(value, sum(index, index+1));
70-
add(index, new_value);
71-
}
7266
};
7367

7468

75-
#include <iostream>
76-
7769
using namespace std;
7870

7971
int main() {
@@ -89,10 +81,10 @@ int main() {
8981
cin >> c >> x >> y;
9082
if (c == 0) {
9183
--x;
92-
bit.add(x, y);
84+
bit.combine(x, y);
9385
} else if (c == 1) {
9486
--x, --y;
95-
cout << bit.sum(x, y+1) << endl;
87+
cout << bit.fold(x, y+1) << endl;
9688
} else throw;
9789
}
9890

lib/cpalgo/ds/README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,20 @@ a.k.a Union Find Tree
88
- [道路の老朽化対策について - AtCoder ABC040D](https://atcoder.jp/contests/abc040/tasks/abc040_d)
99

1010
## Binary Indexed Tree
11-
🚧
12-
a.k.a Fenwick Tree
11+
12+
Binary Indexed Tree (Fenwick Tree) is a data structure.
13+
- It can calculate a cumulative function in the given range `[l,r)` in `O(logN)`.
14+
- It can update an element of A in `O(logN)`.
15+
16+
- [Binary Indexed Tree | C++ code](binary_indexed_tree.hpp)
17+
- [Binary Indexed Tree 2D | C++ code](binary_indexed_tree_2d.hpp)
18+
19+
### References in English
20+
- [Binary Indexed Trees](https://www.topcoder.com/thrive/articles/Binary%20Indexed%20Trees)
21+
- [Fenwick Tree - Competitive Programming Algorithms](https://cp-algorithms.com/data_structures/fenwick.html)
22+
23+
### References in Japanese
24+
- [Binary Indexed Tree のはなし](http://hos.ac/slides/20140319_bit.pdf)
1325

1426
## Minimum Queue
1527
🚧WIP

lib/cpalgo/ds/binary-indexed-tree-2d-sparse.hpp

Lines changed: 0 additions & 109 deletions
This file was deleted.

lib/cpalgo/ds/binary-indexed-tree-2d.hpp

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)