/** * 在 FenwickTree 的指定位置增加指定的值。 * * @param {number} position - FenwickTree 的位置。 * @param {number} value - 要增加的值。 * @return {FenwickTree} - 更新后的 FenwickTree 对象。 */ increase(position, value) { // 检查位置是否在允许的范围内 if (position < 1 || position > this.arraySize) { thrownewError("Position is out of allowed range"); }
// 从给定位置开始,通过增加当前索引的最低有效位来遍历 FenwickTree 数组 for (let i = position; i <= this.arraySize; i += i & -i) { // 通过添加指定的值来更新当前索引处的值 this.treeArray[i] += value; }
// 返回更新后的 FenwickTree 对象 returnthis; }
/** * 查询从索引1到指定位置的和。 * * @param {number} position * @return {number} */ query(position) { // 检查位置是否在允许的范围内 if (position < 1 || position > this.arraySize) { thrownewError("Position is out of allowed range"); }
let sum = 0; // 遍历树数组以计算和 for (let i = position; i > 0; i -= i & -i) { // 将当前索引处的值添加到和中 sum += this.treeArray[i]; }
return sum; }
/** * 查询从左索引到右索引的和。 * * @param {number} leftIndex - 左索引 * @param {number} rightIndex - 右索引 * @return {number} - 和 */ queryRange(leftIndex, rightIndex) { // 检查左索引是否大于右索引。 if (leftIndex > rightIndex) { // 如果左索引大于右索引,抛出错误。 thrownewError("Left index can not be greater than right one"); }
// 如果左索引等于1,返回从索引0到右索引的和。 if (leftIndex === 1) { returnthis.query(rightIndex); }