8000 add linear construction for fenwick tree · cp-algorithms/cp-algorithms@1260771 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1260771

Browse files
committed
add linear construction for fenwick tree
closes #1043
1 parent a4d6447 commit 1260771

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/data_structures/fenwick.md

Lines changed: 19 additions & 1 deletion
< 8000 /div>
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ struct FenwickTree {
148148
bit.assign(n, 0);
149149
}
150150

151-
FenwickTree(vector<int> a) : FenwickTree(a.size()) {
151+
FenwickTree(vector<int> const &a) : FenwickTree(a.size()) {
152152
for (size_t i = 0; i < a.size(); i++)
153153
add(i, a[i]);
154154
}
@@ -171,6 +171,24 @@ struct FenwickTree {
171171
};
172172
```
173173

174+
### Linear construction
175+
176+
The above implementation requires $O(N \log N)$ time.
177+
It's possible to improve that to $O(N)$ time.
178+
179+
The idea is, that the number $a[i]$ at index $i$ will contribute to the range stored in $bit[i]$, and to all ranges that the index $i | (i + 1)$ contributes to.
180+
So by adding the numbers in order, you only have to push the current sum further to the next range, where it will then get pushed further to the next range, and so on.
181+
182+
```cpp
183+
FenwickTree(vector<int> const &a) : FenwickTree(a.size()){
184+
for (int i = 0; i < n; i++) {
185+
bit[i] += a[i];
186+
int r = i | (i + 1);
187+
if (r < n) bit[r] += bit[i];
188+
}
189+
}
190+
```
191+
174192
### Finding minimum of $[0, r]$ in one-dimensional array { data-toc-label='Finding minimum of <script type="math/tex">[0, r]</script> in one-dimensional array' }
175193

176194
It is obvious that there is no easy way of finding minimum of range $[l, r]$ using Fenwick tree, as Fenwick tree can only answer queries of type $[0, r]$.

0 commit comments

Comments
 (0)
0