diff --git a/solution/0300-0399/0370.Range Addition/README.md b/solution/0300-0399/0370.Range Addition/README.md index 107a47d8f9599..20ddbdccebd7e 100644 --- a/solution/0300-0399/0370.Range Addition/README.md +++ b/solution/0300-0399/0370.Range Addition/README.md @@ -101,12 +101,16 @@ class Solution { public: vector getModifiedArray(int length, vector>& updates) { vector d(length); - for (auto& e : updates) { + for (const auto& e : updates) { int l = e[0], r = e[1], c = e[2]; d[l] += c; - if (r + 1 < length) d[r + 1] -= c; + if (r + 1 < length) { + d[r + 1] -= c; + } + } + for (int i = 1; i < length; ++i) { + d[i] += d[i - 1]; } - for (int i = 1; i < length; ++i) d[i] += d[i - 1]; return d; } }; @@ -131,6 +135,24 @@ func getModifiedArray(length int, updates [][]int) []int { } ``` +#### TypeScript + +```ts +function getModifiedArray(length: number, updates: number[][]): number[] { + const d: number[] = Array(length).fill(0); + for (const [l, r, c] of updates) { + d[l] += c; + if (r + 1 < length) { + d[r + 1] -= c; + } + } + for (let i = 1; i < length; ++i) { + d[i] += d[i - 1]; + } + return d; +} +``` + #### JavaScript ```js @@ -140,7 +162,7 @@ func getModifiedArray(length int, updates [][]int) []int { * @return {number[]} */ var getModifiedArray = function (length, updates) { - const d = new Array(length).fill(0); + const d = Array(length).fill(0); for (const [l, r, c] of updates) { d[l] += c; if (r + 1 < length) { @@ -177,82 +199,74 @@ var getModifiedArray = function (length, updates) { ```python class BinaryIndexedTree: - def __init__(self, n): + __slots__ = "n", "c" + + def __init__(self, n: int): self.n = n self.c = [0] * (n + 1) - @staticmethod - def lowbit(x): - return x & -x - - def update(self, x, delta): + def update(self, x: int, delta: int) -> None: while x <= self.n: self.c[x] += delta - x += BinaryIndexedTree.lowbit(x) + x += x & -x - def query(self, x): + def query(self, x: int) -> int: s = 0 while x: s += self.c[x] - x -= BinaryIndexedTree.lowbit(x) + x -= x & -x return s class Solution: def getModifiedArray(self, length: int, updates: List[List[int]]) -> List[int]: tree = BinaryIndexedTree(length) - for start, end, inc in updates: - tree.update(start + 1, inc) - tree.update(end + 2, -inc) + for l, r, c in updates: + tree.update(l + 1, c) + tree.update(r + 2, -c) return [tree.query(i + 1) for i in range(length)] ``` #### Java ```java -class Solution { - public int[] getModifiedArray(int length, int[][] updates) { - BinaryIndexedTree tree = new BinaryIndexedTree(length); - for (int[] e : updates) { - int start = e[0], end = e[1], inc = e[2]; - tree.update(start + 1, inc); - tree.update(end + 2, -inc); - } - int[] ans = new int[length]; - for (int i = 0; i < length; ++i) { - ans[i] = tree.query(i + 1); - } - return ans; - } -} - class BinaryIndexedTree { private int n; private int[] c; public BinaryIndexedTree(int n) { this.n = n; - c = new int[n + 1]; + this.c = new int[n + 1]; } public void update(int x, int delta) { - while (x <= n) { + for (; x <= n; x += x & -x) { c[x] += delta; - x += lowbit(x); } } public int query(int x) { int s = 0; - while (x > 0) { + for (; x > 0; x -= x & -x) { s += c[x]; - x -= lowbit(x); } return s; } +} - public static int lowbit(int x) { - return x & -x; +class Solution { + public int[] getModifiedArray(int length, int[][] updates) { + var tree = new BinaryIndexedTree(length); + for (var e : updates) { + int l = e[0], r = e[1], c = e[2]; + tree.update(l + 1, c); + tree.update(r + 2, -c); + } + int[] ans = new int[length]; + for (int i = 0; i < length; ++i) { + ans[i] = tree.query(i + 1); + } + return ans; } } ``` @@ -261,46 +275,43 @@ class BinaryIndexedTree { ```cpp class BinaryIndexedTree { -public: +private: int n; vector c; - BinaryIndexedTree(int _n) - : n(_n) - , c(_n + 1) {} +public: + BinaryIndexedTree(int n) + : n(n) + , c(n + 1) {} void update(int x, int delta) { - while (x <= n) { + for (; x <= n; x += x & -x) { c[x] += delta; - x += lowbit(x); } } int query(int x) { int s = 0; - while (x > 0) { + for (; x > 0; x -= x & -x) { s += c[x]; - x -= lowbit(x); } return s; } - - int lowbit(int x) { - return x & -x; - } }; class Solution { public: vector getModifiedArray(int length, vector>& updates) { BinaryIndexedTree* tree = new BinaryIndexedTree(length); - for (auto& e : updates) { - int start = e[0], end = e[1], inc = e[2]; - tree->update(start + 1, inc); - tree->update(end + 2, -inc); + for (const auto& e : updates) { + int l = e[0], r = e[1], c = e[2]; + tree->update(l + 1, c); + tree->update(r + 2, -c); } vector ans; - for (int i = 0; i < length; ++i) ans.push_back(tree->query(i + 1)); + for (int i = 0; i < length; ++i) { + ans.push_back(tree->query(i + 1)); + } return ans; } }; @@ -314,46 +325,116 @@ type BinaryIndexedTree struct { c []int } -func newBinaryIndexedTree(n int) *BinaryIndexedTree { - c := make([]int, n+1) - return &BinaryIndexedTree{n, c} +func NewBinaryIndexedTree(n int) *BinaryIndexedTree { + return &BinaryIndexedTree{n: n, c: make([]int, n+1)} } -func (this *BinaryIndexedTree) lowbit(x int) int { - return x & -x -} - -func (this *BinaryIndexedTree) update(x, delta int) { - for x <= this.n { - this.c[x] += delta - x += this.lowbit(x) +func (bit *BinaryIndexedTree) update(x, delta int) { + for ; x <= bit.n; x += x & -x { + bit.c[x] += delta } } -func (this *BinaryIndexedTree) query(x int) int { +func (bit *BinaryIndexedTree) query(x int) int { s := 0 - for x > 0 { - s += this.c[x] - x -= this.lowbit(x) + for ; x > 0; x -= x & -x { + s += bit.c[x] } return s } -func getModifiedArray(length int, updates [][]int) []int { - tree := newBinaryIndexedTree(length) +func getModifiedArray(length int, updates [][]int) (ans []int) { + bit := NewBinaryIndexedTree(length) for _, e := range updates { - start, end, inc := e[0], e[1], e[2] - tree.update(start+1, inc) - tree.update(end+2, -inc) + l, r, c := e[0], e[1], e[2] + bit.update(l+1, c) + bit.update(r+2, -c) } - ans := make([]int, length) - for i := range ans { - ans[i] = tree.query(i + 1) + for i := 1; i <= length; i++ { + ans = append(ans, bit.query(i)) } - return ans + return } ``` +#### TypeScript + +```ts +class BinaryIndexedTree { + private n: number; + private c: number[]; + + constructor(n: number) { + this.n = n; + this.c = Array(n + 1).fill(0); + } + + update(x: number, delta: number): void { + for (; x <= this.n; x += x & -x) { + this.c[x] += delta; + } + } + + query(x: number): number { + let s = 0; + for (; x > 0; x -= x & -x) { + s += this.c[x]; + } + return s; + } +} + +function getModifiedArray(length: number, updates: number[][]): number[] { + const bit = new BinaryIndexedTree(length); + for (const [l, r, c] of updates) { + bit.update(l + 1, c); + bit.update(r + 2, -c); + } + return Array.from({ length }, (_, i) => bit.query(i + 1)); +} +``` + +#### JavaScript + +```js +/** + * @param {number} length + * @param {number[][]} updates + * @return {number[]} + */ +var getModifiedArray = function (length, updates) { + class BinaryIndexedTree { + constructor(n) { + this.n = n; + this.c = Array(n + 1).fill(0); + } + + update(x, delta) { + while (x <= this.n) { + this.c[x] += delta; + x += x & -x; + } + } + + query(x) { + let s = 0; + while (x > 0) { + s += this.c[x]; + x -= x & -x; + } + return s; + } + } + + const bit = new BinaryIndexedTree(length); + for (const [l, r, c] of updates) { + bit.update(l + 1, c); + bit.update(r + 2, -c); + } + return Array.from({ length }, (_, i) => bit.query(i + 1)); +}; +``` + diff --git a/solution/0300-0399/0370.Range Addition/README_EN.md b/solution/0300-0399/0370.Range Addition/README_EN.md index 2c25b07ab487c..191ad3559961e 100644 --- a/solution/0300-0399/0370.Range Addition/README_EN.md +++ b/solution/0300-0399/0370.Range Addition/README_EN.md @@ -54,7 +54,13 @@ tags: -### Solution 1 +### Solution 1: Difference Array + +This is a template problem for difference arrays. + +We define $d$ as the difference array. To add $c$ to each number in the interval $[l,..r]$, we set $d[l] += c$ and $d[r+1] -= c$. Finally, we compute the prefix sum of the difference array to obtain the original array. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array. @@ -99,12 +105,16 @@ class Solution { public: vector getModifiedArray(int length, vector>& updates) { vector d(length); - for (auto& e : updates) { + for (const auto& e : updates) { int l = e[0], r = e[1], c = e[2]; d[l] += c; - if (r + 1 < length) d[r + 1] -= c; + if (r + 1 < length) { + d[r + 1] -= c; + } + } + for (int i = 1; i < length; ++i) { + d[i] += d[i - 1]; } - for (int i = 1; i < length; ++i) d[i] += d[i - 1]; return d; } }; @@ -129,6 +139,24 @@ func getModifiedArray(length int, updates [][]int) []int { } ``` +#### TypeScript + +```ts +function getModifiedArray(length: number, updates: number[][]): number[] { + const d: number[] = Array(length).fill(0); + for (const [l, r, c] of updates) { + d[l] += c; + if (r + 1 < length) { + d[r + 1] -= c; + } + } + for (let i = 1; i < length; ++i) { + d[i] += d[i - 1]; + } + return d; +} +``` + #### JavaScript ```js @@ -138,7 +166,7 @@ func getModifiedArray(length int, updates [][]int) []int { * @return {number[]} */ var getModifiedArray = function (length, updates) { - const d = new Array(length).fill(0); + const d = Array(length).fill(0); for (const [l, r, c] of updates) { d[l] += c; if (r + 1 < length) { @@ -158,7 +186,16 @@ var getModifiedArray = function (length, updates) { -### Solution 2 +### Solution 2: Binary Indexed Tree + Difference Array + +The time complexity is $O(n \times \log n)$. + +A Binary Indexed Tree (BIT), also known as a Fenwick Tree, can efficiently perform the following two operations: + +1. **Point Update** `update(x, delta)`: Add a value $delta$ to the number at position $x$ in the sequence. +2. **Prefix Sum Query** `query(x)`: Query the sum of the interval $[1, ... , x]$ in the sequence, i.e., the prefix sum up to position $x$. + +The time complexity for both operations is $O(\log n)$. @@ -166,82 +203,76 @@ var getModifiedArray = function (length, updates) { ```python class BinaryIndexedTree: - def __init__(self, n): + __slots__ = "n", "c" + + def __init__(self, n: int): self.n = n self.c = [0] * (n + 1) - @staticmethod - def lowbit(x): - return x & -x - - def update(self, x, delta): + def update(self, x: int, delta: int) -> None: while x <= self.n: self.c[x] += delta - x += BinaryIndexedTree.lowbit(x) + x += x & -x - def query(self, x): + def query(self, x: int) -> int: s = 0 while x: s += self.c[x] - x -= BinaryIndexedTree.lowbit(x) + x -= x & -x return s class Solution: def getModifiedArray(self, length: int, updates: List[List[int]]) -> List[int]: tree = BinaryIndexedTree(length) - for start, end, inc in updates: - tree.update(start + 1, inc) - tree.update(end + 2, -inc) + for l, r, c in updates: + tree.update(l + 1, c) + tree.update(r + 2, -c) return [tree.query(i + 1) for i in range(length)] ``` +l + #### Java ```java -class Solution { - public int[] getModifiedArray(int length, int[][] updates) { - BinaryIndexedTree tree = new BinaryIndexedTree(length); - for (int[] e : updates) { - int start = e[0], end = e[1], inc = e[2]; - tree.update(start + 1, inc); - tree.update(end + 2, -inc); - } - int[] ans = new int[length]; - for (int i = 0; i < length; ++i) { - ans[i] = tree.query(i + 1); - } - return ans; - } -} - class BinaryIndexedTree { private int n; private int[] c; public BinaryIndexedTree(int n) { this.n = n; - c = new int[n + 1]; + this.c = new int[n + 1]; } public void update(int x, int delta) { - while (x <= n) { + for (; x <= n; x += x & -x) { c[x] += delta; - x += lowbit(x); } } public int query(int x) { int s = 0; - while (x > 0) { + for (; x > 0; x -= x & -x) { s += c[x]; - x -= lowbit(x); } return s; } +} - public static int lowbit(int x) { - return x & -x; +class Solution { + public int[] getModifiedArray(int length, int[][] updates) { + var tree = new BinaryIndexedTree(length); + for (var e : updates) { + int l = e[0], r = e[1], c = e[2]; + tree.update(l + 1, c); + tree.update(r + 2, -c); + } + int[] ans = new int[length]; + for (int i = 0; i < length; ++i) { + ans[i] = tree.query(i + 1); + } + return ans; } } ``` @@ -250,46 +281,43 @@ class BinaryIndexedTree { ```cpp class BinaryIndexedTree { -public: +private: int n; vector c; - BinaryIndexedTree(int _n) - : n(_n) - , c(_n + 1) {} +public: + BinaryIndexedTree(int n) + : n(n) + , c(n + 1) {} void update(int x, int delta) { - while (x <= n) { + for (; x <= n; x += x & -x) { c[x] += delta; - x += lowbit(x); } } int query(int x) { int s = 0; - while (x > 0) { + for (; x > 0; x -= x & -x) { s += c[x]; - x -= lowbit(x); } return s; } - - int lowbit(int x) { - return x & -x; - } }; class Solution { public: vector getModifiedArray(int length, vector>& updates) { BinaryIndexedTree* tree = new BinaryIndexedTree(length); - for (auto& e : updates) { - int start = e[0], end = e[1], inc = e[2]; - tree->update(start + 1, inc); - tree->update(end + 2, -inc); + for (const auto& e : updates) { + int l = e[0], r = e[1], c = e[2]; + tree->update(l + 1, c); + tree->update(r + 2, -c); } vector ans; - for (int i = 0; i < length; ++i) ans.push_back(tree->query(i + 1)); + for (int i = 0; i < length; ++i) { + ans.push_back(tree->query(i + 1)); + } return ans; } }; @@ -303,46 +331,116 @@ type BinaryIndexedTree struct { c []int } -func newBinaryIndexedTree(n int) *BinaryIndexedTree { - c := make([]int, n+1) - return &BinaryIndexedTree{n, c} +func NewBinaryIndexedTree(n int) *BinaryIndexedTree { + return &BinaryIndexedTree{n: n, c: make([]int, n+1)} } -func (this *BinaryIndexedTree) lowbit(x int) int { - return x & -x -} - -func (this *BinaryIndexedTree) update(x, delta int) { - for x <= this.n { - this.c[x] += delta - x += this.lowbit(x) +func (bit *BinaryIndexedTree) update(x, delta int) { + for ; x <= bit.n; x += x & -x { + bit.c[x] += delta } } -func (this *BinaryIndexedTree) query(x int) int { +func (bit *BinaryIndexedTree) query(x int) int { s := 0 - for x > 0 { - s += this.c[x] - x -= this.lowbit(x) + for ; x > 0; x -= x & -x { + s += bit.c[x] } return s } -func getModifiedArray(length int, updates [][]int) []int { - tree := newBinaryIndexedTree(length) +func getModifiedArray(length int, updates [][]int) (ans []int) { + bit := NewBinaryIndexedTree(length) for _, e := range updates { - start, end, inc := e[0], e[1], e[2] - tree.update(start+1, inc) - tree.update(end+2, -inc) + l, r, c := e[0], e[1], e[2] + bit.update(l+1, c) + bit.update(r+2, -c) } - ans := make([]int, length) - for i := range ans { - ans[i] = tree.query(i + 1) + for i := 1; i <= length; i++ { + ans = append(ans, bit.query(i)) } - return ans + return } ``` +#### TypeScript + +```ts +class BinaryIndexedTree { + private n: number; + private c: number[]; + + constructor(n: number) { + this.n = n; + this.c = Array(n + 1).fill(0); + } + + update(x: number, delta: number): void { + for (; x <= this.n; x += x & -x) { + this.c[x] += delta; + } + } + + query(x: number): number { + let s = 0; + for (; x > 0; x -= x & -x) { + s += this.c[x]; + } + return s; + } +} + +function getModifiedArray(length: number, updates: number[][]): number[] { + const bit = new BinaryIndexedTree(length); + for (const [l, r, c] of updates) { + bit.update(l + 1, c); + bit.update(r + 2, -c); + } + return Array.from({ length }, (_, i) => bit.query(i + 1)); +} +``` + +#### JavaScript + +```js +/** + * @param {number} length + * @param {number[][]} updates + * @return {number[]} + */ +var getModifiedArray = function (length, updates) { + class BinaryIndexedTree { + constructor(n) { + this.n = n; + this.c = Array(n + 1).fill(0); + } + + update(x, delta) { + while (x <= this.n) { + this.c[x] += delta; + x += x & -x; + } + } + + query(x) { + let s = 0; + while (x > 0) { + s += this.c[x]; + x -= x & -x; + } + return s; + } + } + + const bit = new BinaryIndexedTree(length); + for (const [l, r, c] of updates) { + bit.update(l + 1, c); + bit.update(r + 2, -c); + } + return Array.from({ length }, (_, i) => bit.query(i + 1)); +}; +``` + diff --git a/solution/0300-0399/0370.Range Addition/Solution.cpp b/solution/0300-0399/0370.Range Addition/Solution.cpp index 15784241b62ee..3da2a9f884ca8 100644 --- a/solution/0300-0399/0370.Range Addition/Solution.cpp +++ b/solution/0300-0399/0370.Range Addition/Solution.cpp @@ -2,12 +2,16 @@ class Solution { public: vector getModifiedArray(int length, vector>& updates) { vector d(length); - for (auto& e : updates) { + for (const auto& e : updates) { int l = e[0], r = e[1], c = e[2]; d[l] += c; - if (r + 1 < length) d[r + 1] -= c; + if (r + 1 < length) { + d[r + 1] -= c; + } + } + for (int i = 1; i < length; ++i) { + d[i] += d[i - 1]; } - for (int i = 1; i < length; ++i) d[i] += d[i - 1]; return d; } -}; \ No newline at end of file +}; diff --git a/solution/0300-0399/0370.Range Addition/Solution.js b/solution/0300-0399/0370.Range Addition/Solution.js index 0be14fd405507..3eaff3488c50a 100644 --- a/solution/0300-0399/0370.Range Addition/Solution.js +++ b/solution/0300-0399/0370.Range Addition/Solution.js @@ -4,7 +4,7 @@ * @return {number[]} */ var getModifiedArray = function (length, updates) { - const d = new Array(length).fill(0); + const d = Array(length).fill(0); for (const [l, r, c] of updates) { d[l] += c; if (r + 1 < length) { diff --git a/solution/0300-0399/0370.Range Addition/Solution.ts b/solution/0300-0399/0370.Range Addition/Solution.ts new file mode 100644 index 0000000000000..9d5e778e7884c --- /dev/null +++ b/solution/0300-0399/0370.Range Addition/Solution.ts @@ -0,0 +1,13 @@ +function getModifiedArray(length: number, updates: number[][]): number[] { + const d: number[] = Array(length).fill(0); + for (const [l, r, c] of updates) { + d[l] += c; + if (r + 1 < length) { + d[r + 1] -= c; + } + } + for (let i = 1; i < length; ++i) { + d[i] += d[i - 1]; + } + return d; +} diff --git a/solution/0300-0399/0370.Range Addition/Solution2.cpp b/solution/0300-0399/0370.Range Addition/Solution2.cpp index 4e661b4191c48..9aa0c6fc41542 100644 --- a/solution/0300-0399/0370.Range Addition/Solution2.cpp +++ b/solution/0300-0399/0370.Range Addition/Solution2.cpp @@ -1,44 +1,41 @@ class BinaryIndexedTree { -public: +private: int n; vector c; - BinaryIndexedTree(int _n) - : n(_n) - , c(_n + 1) {} +public: + BinaryIndexedTree(int n) + : n(n) + , c(n + 1) {} void update(int x, int delta) { - while (x <= n) { + for (; x <= n; x += x & -x) { c[x] += delta; - x += lowbit(x); } } int query(int x) { int s = 0; - while (x > 0) { + for (; x > 0; x -= x & -x) { s += c[x]; - x -= lowbit(x); } return s; } - - int lowbit(int x) { - return x & -x; - } }; class Solution { public: vector getModifiedArray(int length, vector>& updates) { BinaryIndexedTree* tree = new BinaryIndexedTree(length); - for (auto& e : updates) { - int start = e[0], end = e[1], inc = e[2]; - tree->update(start + 1, inc); - tree->update(end + 2, -inc); + for (const auto& e : updates) { + int l = e[0], r = e[1], c = e[2]; + tree->update(l + 1, c); + tree->update(r + 2, -c); } vector ans; - for (int i = 0; i < length; ++i) ans.push_back(tree->query(i + 1)); + for (int i = 0; i < length; ++i) { + ans.push_back(tree->query(i + 1)); + } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/0300-0399/0370.Range Addition/Solution2.go b/solution/0300-0399/0370.Range Addition/Solution2.go index d5b3877030f7c..c53a1226550c6 100644 --- a/solution/0300-0399/0370.Range Addition/Solution2.go +++ b/solution/0300-0399/0370.Range Addition/Solution2.go @@ -3,41 +3,33 @@ type BinaryIndexedTree struct { c []int } -func newBinaryIndexedTree(n int) *BinaryIndexedTree { - c := make([]int, n+1) - return &BinaryIndexedTree{n, c} +func NewBinaryIndexedTree(n int) *BinaryIndexedTree { + return &BinaryIndexedTree{n: n, c: make([]int, n+1)} } -func (this *BinaryIndexedTree) lowbit(x int) int { - return x & -x -} - -func (this *BinaryIndexedTree) update(x, delta int) { - for x <= this.n { - this.c[x] += delta - x += this.lowbit(x) +func (bit *BinaryIndexedTree) update(x, delta int) { + for ; x <= bit.n; x += x & -x { + bit.c[x] += delta } } -func (this *BinaryIndexedTree) query(x int) int { +func (bit *BinaryIndexedTree) query(x int) int { s := 0 - for x > 0 { - s += this.c[x] - x -= this.lowbit(x) + for ; x > 0; x -= x & -x { + s += bit.c[x] } return s } -func getModifiedArray(length int, updates [][]int) []int { - tree := newBinaryIndexedTree(length) +func getModifiedArray(length int, updates [][]int) (ans []int) { + bit := NewBinaryIndexedTree(length) for _, e := range updates { - start, end, inc := e[0], e[1], e[2] - tree.update(start+1, inc) - tree.update(end+2, -inc) + l, r, c := e[0], e[1], e[2] + bit.update(l+1, c) + bit.update(r+2, -c) } - ans := make([]int, length) - for i := range ans { - ans[i] = tree.query(i + 1) + for i := 1; i <= length; i++ { + ans = append(ans, bit.query(i)) } - return ans -} \ No newline at end of file + return +} diff --git a/solution/0300-0399/0370.Range Addition/Solution2.java b/solution/0300-0399/0370.Range Addition/Solution2.java index a7c3194be1fca..567586dfb4fc0 100644 --- a/solution/0300-0399/0370.Range Addition/Solution2.java +++ b/solution/0300-0399/0370.Range Addition/Solution2.java @@ -1,45 +1,39 @@ -class Solution { - public int[] getModifiedArray(int length, int[][] updates) { - BinaryIndexedTree tree = new BinaryIndexedTree(length); - for (int[] e : updates) { - int start = e[0], end = e[1], inc = e[2]; - tree.update(start + 1, inc); - tree.update(end + 2, -inc); - } - int[] ans = new int[length]; - for (int i = 0; i < length; ++i) { - ans[i] = tree.query(i + 1); - } - return ans; - } -} - class BinaryIndexedTree { private int n; private int[] c; public BinaryIndexedTree(int n) { this.n = n; - c = new int[n + 1]; + this.c = new int[n + 1]; } public void update(int x, int delta) { - while (x <= n) { + for (; x <= n; x += x & -x) { c[x] += delta; - x += lowbit(x); } } public int query(int x) { int s = 0; - while (x > 0) { + for (; x > 0; x -= x & -x) { s += c[x]; - x -= lowbit(x); } return s; } +} - public static int lowbit(int x) { - return x & -x; +class Solution { + public int[] getModifiedArray(int length, int[][] updates) { + var tree = new BinaryIndexedTree(length); + for (var e : updates) { + int l = e[0], r = e[1], c = e[2]; + tree.update(l + 1, c); + tree.update(r + 2, -c); + } + int[] ans = new int[length]; + for (int i = 0; i < length; ++i) { + ans[i] = tree.query(i + 1); + } + return ans; } -} \ No newline at end of file +} diff --git a/solution/0300-0399/0370.Range Addition/Solution2.js b/solution/0300-0399/0370.Range Addition/Solution2.js new file mode 100644 index 0000000000000..17317452a1d12 --- /dev/null +++ b/solution/0300-0399/0370.Range Addition/Solution2.js @@ -0,0 +1,36 @@ +/** + * @param {number} length + * @param {number[][]} updates + * @return {number[]} + */ +var getModifiedArray = function (length, updates) { + class BinaryIndexedTree { + constructor(n) { + this.n = n; + this.c = Array(n + 1).fill(0); + } + + update(x, delta) { + while (x <= this.n) { + this.c[x] += delta; + x += x & -x; + } + } + + query(x) { + let s = 0; + while (x > 0) { + s += this.c[x]; + x -= x & -x; + } + return s; + } + } + + const bit = new BinaryIndexedTree(length); + for (const [l, r, c] of updates) { + bit.update(l + 1, c); + bit.update(r + 2, -c); + } + return Array.from({ length }, (_, i) => bit.query(i + 1)); +}; diff --git a/solution/0300-0399/0370.Range Addition/Solution2.py b/solution/0300-0399/0370.Range Addition/Solution2.py index ffdd09fd6c50f..2657081c764d2 100644 --- a/solution/0300-0399/0370.Range Addition/Solution2.py +++ b/solution/0300-0399/0370.Range Addition/Solution2.py @@ -1,29 +1,27 @@ class BinaryIndexedTree: - def __init__(self, n): + __slots__ = "n", "c" + + def __init__(self, n: int): self.n = n self.c = [0] * (n + 1) - @staticmethod - def lowbit(x): - return x & -x - - def update(self, x, delta): + def update(self, x: int, delta: int) -> None: while x <= self.n: self.c[x] += delta - x += BinaryIndexedTree.lowbit(x) + x += x & -x - def query(self, x): + def query(self, x: int) -> int: s = 0 while x: s += self.c[x] - x -= BinaryIndexedTree.lowbit(x) + x -= x & -x return s class Solution: def getModifiedArray(self, length: int, updates: List[List[int]]) -> List[int]: tree = BinaryIndexedTree(length) - for start, end, inc in updates: - tree.update(start + 1, inc) - tree.update(end + 2, -inc) + for l, r, c in updates: + tree.update(l + 1, c) + tree.update(r + 2, -c) return [tree.query(i + 1) for i in range(length)] diff --git a/solution/0300-0399/0370.Range Addition/Solution2.ts b/solution/0300-0399/0370.Range Addition/Solution2.ts new file mode 100644 index 0000000000000..590691a46a0d9 --- /dev/null +++ b/solution/0300-0399/0370.Range Addition/Solution2.ts @@ -0,0 +1,32 @@ +class BinaryIndexedTree { + private n: number; + private c: number[]; + + constructor(n: number) { + this.n = n; + this.c = Array(n + 1).fill(0); + } + + update(x: number, delta: number): void { + for (; x <= this.n; x += x & -x) { + this.c[x] += delta; + } + } + + query(x: number): number { + let s = 0; + for (; x > 0; x -= x & -x) { + s += this.c[x]; + } + return s; + } +} + +function getModifiedArray(length: number, updates: number[][]): number[] { + const bit = new BinaryIndexedTree(length); + for (const [l, r, c] of updates) { + bit.update(l + 1, c); + bit.update(r + 2, -c); + } + return Array.from({ length }, (_, i) => bit.query(i + 1)); +}