@@ -253,35 +253,35 @@ Additionally, each time a value is `update`'d, the new value has to be smaller t
253253Both significant limitations are because the $min$ operation together with the set of integers doesn't form a group, as there are no inverse elements.
254254
255255=== "C++"
256- ```{.cpp file=fenwick_min}
257- 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- }
256+ ``` {.cpp file=fenwick_min}
257+ struct FenwickTreeMin {
258+ vector<int > bit;
259+ int n;
260+ const int INF = (int)1e9;
266261
267- FenwickTreeMin(vector< int> a) : FenwickTreeMin(a.size() ) {
268- for (size_t i = 0; i < a.size(); i++)
269- update(i, a[i] );
270- }
262+ FenwickTreeMin(int n ) {
263+ this->n = n;
264+ bit.assign(n, INF );
265+ }
271266
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- }
267+ FenwickTreeMin (vector<int > a) : FenwickTreeMin(a.size()) {
268+ for (size_t i = 0; i < a.size(); i++)
269+ update(i, a[ i] );
270+ }
278271
279- void update(int idx, int val) {
280- for (; idx < n; idx = idx | (idx + 1))
281- bit[idx] = min(bit[idx], val);
282- }
283- };
284- ```
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+ }
283+ };
284+ ```
285285=== "Python"
286286 ```py
287287 class FenwickTreeMin:
@@ -436,39 +436,39 @@ As you can see, the main benefit of this approach is that the binary operations
436436The following implementation can be used like the other implementations, however it uses one-based indexing internally.
437437
438438=== "C++"
439- ```{.cpp file=fenwick_sum_onebased}
440- 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- }
439+ ```{.cpp file=fenwick_sum_onebased}
440+ struct FenwickTreeOneBasedIndexing {
441+ vector<int> bit; // binary indexed tree
442+ int n;
448443
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- }
444+ FenwickTreeOneBasedIndexing(int n) {
445+ this->n = n + 1;
446+ bit.assign(n + 1, 0);
447+ }
454448
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- }
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+ }
461454
462- int sum(int l, int r) {
463- return sum(r) - sum(l - 1);
464- }
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+ }
465461
466- void add(int idx, int delta) {
467- for (++idx; idx < n; idx += idx & -idx)
468- bit[idx] += delta;
469- }
470- };
471- ```
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+ }
470+ };
471+ ```
472472=== "Python"
473473 ```py
474474 class FenwickTreeOneBasedIndexing:
0 commit comments