-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support text patch Signed-off-by: Marcos Candeia <[email protected]> * Allow set to null Signed-off-by: Marcos Candeia <[email protected]> --------- Signed-off-by: Marcos Candeia <[email protected]>
- Loading branch information
Showing
5 changed files
with
314 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
export class BinaryIndexedTree { | ||
bit: number[]; | ||
|
||
constructor(size: number) { | ||
this.bit = new Array(size + 1).fill(0); | ||
} | ||
|
||
// Updates the value at index i by adding delta to it | ||
update(idx: number, delta: number): void { | ||
idx++; // Convert 0-based indexing to 1-based indexing | ||
while (idx < this.bit.length) { | ||
this.bit[idx] += delta; | ||
idx += idx & -idx; // Move to next index | ||
} | ||
} | ||
|
||
// Returns the sum of values in the range [0, i] | ||
query(r: number): number { | ||
r++; // Convert 0-based indexing to 1-based indexing | ||
let ret = 0; | ||
while (r > 0) { | ||
ret += this.bit[r]; | ||
r -= r & -r; // Move to parent index | ||
} | ||
return ret; | ||
} | ||
|
||
// Returns the sum of values in the range [left, right] | ||
rangeQuery(left: number, right: number): number { | ||
return this.query(right) - this.query(left - 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.