| 1 | #cxx . . $ `<file>/dwsys/BinaryIndexedTree.h`/ifndef _BinaryIndexedTree_h_ |
| 2 | #define _BinaryIndexedTree_h_cxx . . $ `dwsys/BinaryIndexedTree.h:2:9`! |
| 3 | /* BinaryIndexedTree.h |
| 4 | * |
| 5 | * Copyright (C) 2025 David Weenink |
| 6 | * |
| 7 | * This code is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "melder.h"cxx . . $ `<file>/melder/melder.h`/ |
| 22 | |
| 23 | class BinaryIndexedTreecxx . . $ BinaryIndexedTree# { |
| 24 | private: |
| 25 | autoINTVECcxx . . $ autoINTVEC# bitcxx . . $ BinaryIndexedTree#bit.; |
| 26 | public: |
| 27 | |
| 28 | BinaryIndexedTreecxx . . $ BinaryIndexedTree#BinaryIndexedTree(49f6e7a06ebc5aa8). () { } |
| 29 | |
| 30 | BinaryIndexedTreecxx . . $ BinaryIndexedTree#BinaryIndexedTree(1624ee8275b10bda). (integercxx . . $ integer# size) { |
| 31 | bitcxx . . $ BinaryIndexedTree#bit. =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). zero_INTVECcxx . . $ zero_INTVEC(dab5d3ef28d6a25). (size); |
| 32 | } |
| 33 | |
| 34 | /* |
| 35 | This elegant initialization in O(n) time I found at sdnr1's blog at |
| 36 | https://codeforces.com/blog/entry/63064 |
| 37 | */ |
| 38 | BinaryIndexedTreecxx . . $ BinaryIndexedTree#BinaryIndexedTree(b9374cd8054eb0f8). (constINTVECcxx . . $ constINTVEC# const& v) { |
| 39 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (v.size > 0); |
| 40 | bitcxx . . $ BinaryIndexedTree#bit. =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). zero_INTVECcxx . . $ zero_INTVEC(dab5d3ef28d6a25). (v.sizecxx . . $ constvector#size.); |
| 41 | for (integercxx . . $ integer# i = 1; i <= v.sizecxx . . $ constvector#size.; i ++) { |
| 42 | bitcxx . . $ BinaryIndexedTree#bit. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] += v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i]; |
| 43 | const integercxx . . $ integer# next = i + (i & -i); |
| 44 | if (next <= v.sizecxx . . $ constvector#size.) |
| 45 | bitcxx . . $ BinaryIndexedTree#bit. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).next] += bitcxx . . $ BinaryIndexedTree#bit. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // update bit if we put a new 'value' at 'index' |
| 50 | void updatecxx . . $ BinaryIndexedTree#update(a8896fd0f69df779). (integercxx . . $ integer# index, integercxx . . $ integer# value) { |
| 51 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (index <= bit.size); |
| 52 | for (integercxx . . $ integer# i = index; i <= bitcxx . . $ BinaryIndexedTree#bit..sizecxx . . $ autovector#size.; i += i & -i) |
| 53 | bitcxx . . $ BinaryIndexedTree#bit. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] += value; |
| 54 | } |
| 55 | |
| 56 | integercxx . . $ integer# querycxx . . $ BinaryIndexedTree#query(8ce0d1b217db7fd1). (integercxx . . $ integer# index) { |
| 57 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (index <= bit.size); |
| 58 | integercxx . . $ integer# sum = 0; |
| 59 | for (integercxx . . $ integer# i = index; i >= 1; i -= i & -i) |
| 60 | sum += bitcxx . . $ BinaryIndexedTree#bit. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; |
| 61 | return sum; |
| 62 | } |
| 63 | |
| 64 | integercxx . . $ integer# queryRangecxx . . $ BinaryIndexedTree#queryRange(d0a0e4c270da0eca). (integercxx . . $ integer# left, integercxx . . $ integer# right) { |
| 65 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (left <= right); |
| 66 | return querycxx . . $ BinaryIndexedTree#query(8ce0d1b217db7fd1). (right) - querycxx . . $ BinaryIndexedTree#query(8ce0d1b217db7fd1). (left - 1); |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | |
| 71 | #endif /* _BinaryIndexedTree_h_ */ |