| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.RangeSumQueryMutable; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class RangeSumQueryMutableSegmentTree : IRangeSumQueryMutable |
| | 16 | | { |
| | 17 | | private readonly int _n; |
| | 18 | | private readonly int[] _tree; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Time complexity - O(n) |
| | 22 | | /// Space complexity - O(n) |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="nums"></param> |
| 1 | 25 | | public RangeSumQueryMutableSegmentTree(int[] nums) |
| 1 | 26 | | { |
| 1 | 27 | | _n = nums.Length; |
| | 28 | |
|
| 1 | 29 | | _tree = new int[_n * 2]; |
| | 30 | |
|
| 8 | 31 | | for (var i = 0; i < _n; i++) |
| 3 | 32 | | { |
| 3 | 33 | | _tree[i + _n] = nums[i]; |
| 3 | 34 | | } |
| | 35 | |
|
| 6 | 36 | | for (var i = _n - 1; i >= 1; i--) |
| 2 | 37 | | { |
| 2 | 38 | | var left = i * 2; |
| 2 | 39 | | var right = left + 1; |
| | 40 | |
|
| 2 | 41 | | _tree[i] = _tree[left] + _tree[right]; |
| 2 | 42 | | } |
| 1 | 43 | | } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Time complexity - O(log n) |
| | 47 | | /// Space complexity - O(1) |
| | 48 | | /// </summary> |
| | 49 | | /// <param name="index"></param> |
| | 50 | | /// <param name="val"></param> |
| | 51 | | public void Update(int index, int val) |
| 1 | 52 | | { |
| 1 | 53 | | var i = index + _n; |
| | 54 | |
|
| 1 | 55 | | _tree[i] = val; |
| | 56 | |
|
| 3 | 57 | | while (i > 1) |
| 2 | 58 | | { |
| 2 | 59 | | i /= 2; |
| | 60 | |
|
| 2 | 61 | | var left = i * 2; |
| 2 | 62 | | var right = left + 1; |
| | 63 | |
|
| 2 | 64 | | _tree[i] = _tree[left] + _tree[right]; |
| 2 | 65 | | } |
| 1 | 66 | | } |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// Time complexity - O(log n) |
| | 70 | | /// Space complexity - O(1) |
| | 71 | | /// </summary> |
| | 72 | | /// <param name="left"></param> |
| | 73 | | /// <param name="right"></param> |
| | 74 | | /// <returns></returns> |
| | 75 | | public int SumRange(int left, int right) |
| 2 | 76 | | { |
| 2 | 77 | | var sum = 0; |
| | 78 | |
|
| 2 | 79 | | var leftIndex = left + _n; |
| 2 | 80 | | var rightIndex = right + _n; |
| | 81 | |
|
| 6 | 82 | | while (leftIndex <= rightIndex) |
| 4 | 83 | | { |
| 4 | 84 | | if (leftIndex % 2 == 1) |
| 2 | 85 | | { |
| 2 | 86 | | sum += _tree[leftIndex]; |
| | 87 | |
|
| 2 | 88 | | leftIndex++; |
| 2 | 89 | | } |
| | 90 | |
|
| 4 | 91 | | if (rightIndex % 2 == 0) |
| 2 | 92 | | { |
| 2 | 93 | | sum += _tree[rightIndex]; |
| | 94 | |
|
| 2 | 95 | | rightIndex--; |
| 2 | 96 | | } |
| | 97 | |
|
| 4 | 98 | | leftIndex /= 2; |
| 4 | 99 | | rightIndex /= 2; |
| 4 | 100 | | } |
| | 101 | |
|
| 2 | 102 | | return sum; |
| 2 | 103 | | } |
| | 104 | | } |