|
| 1 | +<header> |
| 2 | + <h1 align="center"> |
| 3 | + <a href="https://github.com/guanghechen/algorithm.ts/tree/main/packages/binary-index-tree#readme">@algorithm.ts/binary-index-tree</a> |
| 4 | + </h1> |
| 5 | + <div align="center"> |
| 6 | + <a href="https://www.npmjs.com/package/@algorithm.ts/binary-index-tree"> |
| 7 | + <img |
| 8 | + alt="Npm Version" |
| 9 | + src="https://img.shields.io/npm/v/@algorithm.ts/binary-index-tree.svg" |
| 10 | + /> |
| 11 | + </a> |
| 12 | + <a href="https://www.npmjs.com/package/@algorithm.ts/binary-index-tree"> |
| 13 | + <img |
| 14 | + alt="Npm Download" |
| 15 | + src="https://img.shields.io/npm/dm/@algorithm.ts/binary-index-tree.svg" |
| 16 | + /> |
| 17 | + </a> |
| 18 | + <a href="https://www.npmjs.com/package/@algorithm.ts/binary-index-tree"> |
| 19 | + <img |
| 20 | + alt="Npm License" |
| 21 | + src="https://img.shields.io/npm/l/@algorithm.ts/binary-index-tree.svg" |
| 22 | + /> |
| 23 | + </a> |
| 24 | + <a href="#install"> |
| 25 | + <img |
| 26 | + alt="Module Formats: cjs, esm" |
| 27 | + src="https://img.shields.io/badge/module_formats-cjs%2C%20esm-green.svg" |
| 28 | + /> |
| 29 | + </a> |
| 30 | + <a href="https://github.com/nodejs/node"> |
| 31 | + <img |
| 32 | + alt="Node.js Version" |
| 33 | + src="https://img.shields.io/node/v/@algorithm.ts/binary-index-tree" |
| 34 | + /> |
| 35 | + </a> |
| 36 | + <a href="https://github.com/facebook/jest"> |
| 37 | + <img |
| 38 | + alt="Tested with Jest" |
| 39 | + src="https://img.shields.io/badge/tested_with-jest-9c465e.svg" |
| 40 | + /> |
| 41 | + </a> |
| 42 | + <a href="https://github.com/prettier/prettier"> |
| 43 | + <img |
| 44 | + alt="Code Style: prettier" |
| 45 | + src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square" |
| 46 | + /> |
| 47 | + </a> |
| 48 | + </div> |
| 49 | +</header> |
| 50 | +<br/> |
| 51 | + |
| 52 | + |
| 53 | +**树状数组** 的 Typescript 实现。 |
| 54 | + |
| 55 | +树状数组是一个树形的数组结构,用于高效地维护前缀和。通常有两种操作方式: |
| 56 | + |
| 57 | +1. 单点更新,区间查询: 修改数列中某个元素的数值,以及求解某个位置开始的前缀和; |
| 58 | + 求解任意区间 $[L, R]$ 的和可以拆成求解区间 $[1,R]$ 的和与区间 $[1,L-1]$ 的和 |
| 59 | + 之差,即转换成求解两个前缀和的问题。 |
| 60 | + |
| 61 | +2. 区间更新,单点查询: 给数列中前 $x$ 个元素的值同时加上某个值,以及求解数列中 |
| 62 | + 任意位置上的元素当前值。同样地,如果要给任意区间 $[L, R]$ 加上一个共同的值 $x$, |
| 63 | + 可以先给 $[1,R]$ 中的元素同时加上 $x$,再给 $[1,L-1]$ 中的元素同时加上 $-x$。 |
| 64 | + |
| 65 | +上述操作全是在 $O(\log N)$ 的均摊复杂度下完成。 |
| 66 | + |
| 67 | +树状数组能解决的问题是线段树的子集,但是其相比于线段树拥有更小的复杂度常数,以及 |
| 68 | +更简单的实现且更易理解。 |
| 69 | + |
| 70 | + |
| 71 | +## Install |
| 72 | + |
| 73 | +* npm |
| 74 | + |
| 75 | + ```bash |
| 76 | + npm install --save @algorithm.ts/binary-index-tree |
| 77 | + ``` |
| 78 | + |
| 79 | +* yarn |
| 80 | + |
| 81 | + ```bash |
| 82 | + yarn add @algorithm.ts/binary-index-tree |
| 83 | + ``` |
| 84 | + |
| 85 | +## Usage |
| 86 | + |
| 87 | +### 单点更新,区间查询 |
| 88 | + |
| 89 | +* Solve numbers: |
| 90 | + |
| 91 | + ```typescript {3} |
| 92 | + import { createBinaryIndexTree1 } from '@algorithm.ts/binary-index-tree' |
| 93 | + |
| 94 | + const MAX_N = 10 |
| 95 | + const bit = createBinaryIndexTree1<number>(MAX_N, 0) |
| 96 | + bit.init(MAX_N) |
| 97 | + |
| 98 | + // Add 10 on the 2th element. |
| 99 | + bit.add(2, 10) |
| 100 | + |
| 101 | + // Get the prefix sums. |
| 102 | + bit.query(1) // => 0 |
| 103 | + bit.query(2) // => 10 |
| 104 | + bit.query(/* any integer between [2, 10] */) // => 10 |
| 105 | + |
| 106 | + // Add 7 on the 4th element. |
| 107 | + bit.add(4, 7) |
| 108 | + |
| 109 | + // Get the prefix sums. |
| 110 | + bit.query(1) // => 0 |
| 111 | + bit.query(2) // => 10 |
| 112 | + bit.query(3) // => 10 |
| 113 | + bit.query(4) // => 17 |
| 114 | + bit.query(/* any integer between [4, 10] */) // => 17 |
| 115 | + ``` |
| 116 | + |
| 117 | +* Solve bigint: |
| 118 | + |
| 119 | + ```typescript {6} |
| 120 | + import { createBinaryIndexTree1 } from '@algorithm.ts/binary-index-tree' |
| 121 | + |
| 122 | + const MAX_N = 10 |
| 123 | + // Please note that the second parameter is `0n`, which represents the zero |
| 124 | + // element of bigint, and 0 is passed-in in the above example. |
| 125 | + const bit = createBinaryIndexTree1<number>(MAX_N, 0n) |
| 126 | + bit.init(MAX_N) |
| 127 | + |
| 128 | + // Add 10n on the 2th element. |
| 129 | + bit.add(2, 10n) |
| 130 | + |
| 131 | + // Get the prefix sums. |
| 132 | + bit.query(1) // => 0n |
| 133 | + bit.query(2) // => 10n |
| 134 | + bit.query(/* any integer between [2, 10] */) // => 10n |
| 135 | + |
| 136 | + // Add 7n on the 4th element. |
| 137 | + bit.add(4, 7) |
| 138 | + |
| 139 | + // Get the prefix sums. |
| 140 | + bit.query(1) // => 0n |
| 141 | + bit.query(2) // => 10n |
| 142 | + bit.query(3) // => 10n |
| 143 | + bit.query(4) // => 17n |
| 144 | + bit.query(/* any integer between [4, 10] */) // => 17n |
| 145 | + ``` |
| 146 | + |
| 147 | +### 区间更新,单点查询 |
| 148 | + |
| 149 | +* Solve numbers: |
| 150 | + |
| 151 | + ```typescript {3} |
| 152 | + import { createBinaryIndexTree2 } from '@algorithm.ts/binary-index-tree' |
| 153 | + |
| 154 | + const MAX_N = 10 |
| 155 | + const bit = createBinaryIndexTree2<number>(MAX_N, 0) |
| 156 | + bit.init(MAX_N) |
| 157 | + |
| 158 | + // Add 10 on the first two elements. |
| 159 | + bit.add(2, 10) |
| 160 | + |
| 161 | + // Get the value of x-st element. |
| 162 | + bit.query(1) // => 10 |
| 163 | + bit.query(2) // => 10 |
| 164 | + bit.query(/* any integer between [3, 10] */) // => 0 |
| 165 | + |
| 166 | + // Add 7 on the first four elements. |
| 167 | + bit.add(4, 7) |
| 168 | + |
| 169 | + // Get the value of x-st element. |
| 170 | + bit.query(1) // => 17 |
| 171 | + bit.query(2) // => 17 |
| 172 | + bit.query(3) // => 17 |
| 173 | + bit.query(4) // => 17 |
| 174 | + bit.query(/* any integer between [5, 10] */) // => 0 |
| 175 | + ``` |
| 176 | + |
| 177 | +* Solve bigint: |
| 178 | + |
| 179 | + ```typescript {6} |
| 180 | + import { createBinaryIndexTree2 } from '@algorithm.ts/binary-index-tree' |
| 181 | + |
| 182 | + const MAX_N = 10 |
| 183 | + // Please note that the second parameter is `0n`, which represents the zero |
| 184 | + // element of bigint, and 0 is passed-in in the above example. |
| 185 | + const bit = createBinaryIndexTree2<number>(MAX_N, 0n) |
| 186 | + bit.init(MAX_N) |
| 187 | + |
| 188 | + // Add 10 on the first two elements. |
| 189 | + bit.add(2, 10n) |
| 190 | + |
| 191 | + // Get the value of x-st element. |
| 192 | + bit.query(1) // => 10n |
| 193 | + bit.query(2) // => 10n |
| 194 | + bit.query(/* any integer between [3, 10] */) // => 0n |
| 195 | + |
| 196 | + // Add 7 on the first four elements. |
| 197 | + bit.add(4, 7) |
| 198 | + |
| 199 | + // Get the value of x-st element. |
| 200 | + bit.query(1) // => 17n |
| 201 | + bit.query(2) // => 17n |
| 202 | + bit.query(3) // => 17n |
| 203 | + bit.query(4) // => 17n |
| 204 | + bit.query(/* any integer between [5, 10] */) // => 0n |
| 205 | + ``` |
| 206 | + |
| 207 | + |
| 208 | +## Related |
| 209 | + |
| 210 | + |
| 211 | +[homepage]: https://github.com/guanghechen/algorithm.ts/tree/main/packages/binary-index-tree#readme |
| 212 | +[binary-index-tree]: https://me.guanghechen.com/post/algorithm/shuffle/#heading-binary-index-tree |
0 commit comments