@@ -167,34 +167,34 @@ You can create a Fenwick tree initialized with zeros, or you can convert an exis
167167=== "C++"
168168 ```{.cpp file=fenwick_sum}
169169 struct FenwickTree {
170- vector<int > bit; // binary indexed tree
171- int n;
172-
173- FenwickTree(int n) {
174- this->n = n;
175- bit.assign(n, 0);
176- }
177-
178- FenwickTree(vector<int> const &a) : FenwickTree(a.size()) {
179- for (size_t i = 0; i < a.size(); i++)
180- add(i, a[i]);
181- }
182-
183- int sum(int r) {
184- int ret = 0;
185- for (; r >= 0; r = (r & (r + 1)) - 1)
186- ret += bit[r];
187- return ret;
188- }
189-
190- int sum(int l, int r) {
191- return sum(r) - sum(l - 1);
192- }
193-
194- void add(int idx, int delta) {
195- for (; idx < n; idx = idx | (idx + 1))
196- bit[idx] += delta;
197- }
170+ vector<int > bit; // binary indexed tree
171+ int n;
172+
173+ FenwickTree(int n) {
174+ this->n = n;
175+ bit.assign(n, 0);
176+ }
177+
178+ FenwickTree(vector<int> const &a) : FenwickTree(a.size()) {
179+ for (size_t i = 0; i < a.size(); i++)
180+ add(i, a[i]);
181+ }
182+
183+ int sum(int r) {
184+ int ret = 0;
185+ for (; r >= 0; r = (r & (r + 1)) - 1)
186+ ret += bit[r];
187+ return ret;
188+ }
189+
190+ int sum(int l, int r) {
191+ return sum(r) - sum(l - 1);
192+ }
193+
194+ void add(int idx, int delta) {
195+ for (; idx < n; idx = idx | (idx + 1))
196+ bit[idx] += delta;
197+ }
198198 };
199199 ```
200200=== "Python"
@@ -255,31 +255,31 @@ Both significant limitations are because the $min$ operation together with the s
255255=== "C++"
256256 ```{.cpp file=fenwick_min}
257257 struct FenwickTreeMin {
258- vector<int> bit;
259- int n;
260- const int INF = (int)1e9;
261-
262- FenwickTreeMin(int n) {
263- this->n = n;
264- bit.assign(n, INF);
265- }
266-
267- FenwickTreeMin(vector<int> a) : FenwickTreeMin(a.size()) {
268- for (size_t i = 0; i < a.size(); i++)
269- update(i, a[i]);
270- }
271-
272- int getmin(int r) {
273- int ret = INF;
274- for (; r >= 0; r = (r & (r + 1)) - 1)
275- ret = min(ret, bit[r]);
276- return ret;
277- }
278-
279- void update(int idx, int val) {
280- for (; idx < n; idx = idx | (idx + 1))
281- bit[idx] = min(bit[idx], val);
282- }
258+ vector<int> bit;
259+ int n;
260+ const int INF = (int)1e9;
261+
262+ FenwickTreeMin(int n) {
263+ this->n = n;
264+ bit.assign(n, INF);
265+ }
266+
267+ FenwickTreeMin(vector<int> a) : FenwickTreeMin(a.size()) {
268+ for (size_t i = 0; i < a.size(); i++)
269+ update(i, a[i]);
270+ }
271+
272+ int getmin(int r) {
273+ int ret = INF;
274+ for (; r >= 0; r = (r & (r + 1)) - 1)
275+ ret = min(ret, bit[r]);
276+ return ret;
277+ }
278+
279+ void update(int idx, int val) {
280+ for (; idx < n; idx = idx | (idx + 1))
281+ bit[idx] = min(bit[idx], val);
282+ }
283283 };
284284 ```
285285=== "Python"
@@ -348,7 +348,7 @@ As claimed before, it is very easy to implement Fenwick Tree for multidimensiona
348348=== "Python"
349349 ```py
350350 class FenwickTree2D:
351-
351+
352352 def __init__(self, n: int, m: int) -> None:
353353 self.n = n
354354 self.m = m
@@ -438,41 +438,41 @@ The following implementation can be used like the other implementations, however
438438=== "C++"
439439 ```{.cpp file=fenwick_sum_onebased}
440440 struct FenwickTreeOneBasedIndexing {
441- vector<int> bit; // binary indexed tree
442- int n;
443-
444- FenwickTreeOneBasedIndexing(int n) {
445- this->n = n + 1;
446- bit.assign(n + 1, 0);
447- }
448-
449- FenwickTreeOneBasedIndexing(vector<int> a)
450- : FenwickTreeOneBasedIndexing(a.size()) {
451- for (size_t i = 0; i < a.size(); i++)
452- add(i, a[i]);
453- }
454-
455- int sum(int idx) {
456- int ret = 0;
457- for (++idx; idx > 0; idx -= idx & -idx)
458- ret += bit[idx];
459- return ret;
460- }
461-
462- int sum(int l, int r) {
463- return sum(r) - sum(l - 1);
464- }
465-
466- void add(int idx, int delta) {
467- for (++idx; idx < n; idx += idx & -idx)
468- bit[idx] += delta;
469- }
441+ vector<int> bit; // binary indexed tree
442+ int n;
443+
444+ FenwickTreeOneBasedIndexing(int n) {
445+ this->n = n + 1;
446+ bit.assign(n + 1, 0);
447+ }
448+
449+ FenwickTreeOneBasedIndexing(vector<int> a)
450+ : FenwickTreeOneBasedIndexing(a.size()) {
451+ for (size_t i = 0; i < a.size(); i++)
452+ add(i, a[i]);
453+ }
454+
455+ int sum(int idx) {
456+ int ret = 0;
457+ for (++idx; idx > 0; idx -= idx & -idx)
458+ ret += bit[idx];
459+ return ret;
460+ }
461+
462+ int sum(int l, int r) {
463+ return sum(r) - sum(l - 1);
464+ }
465+
466+ void add(int idx, int delta) {
467+ for (++idx; idx < n; idx += idx & -idx)
468+ bit[idx] += delta;
469+ }
470470 };
471471 ```
472472=== "Python"
473473 ```py
474474 class FenwickTreeOneBasedIndexing:
475-
475+
476476 def __init__(self, a: Union[int, List[int]]) -> None:
477477 if isinstance(a, int):
478478 self.n = a + 1
0 commit comments