< Summary

Information
Class: LeetCode.Algorithms.RangeSumQueryMutable.RangeSumQueryMutableSegmentTree
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\RangeSumQueryMutable\RangeSumQueryMutableSegmentTree.cs
Line coverage
100%
Covered lines: 47
Uncovered lines: 0
Coverable lines: 47
Total lines: 104
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%44100%
Update(...)100%22100%
SumRange(...)100%66100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\RangeSumQueryMutable\RangeSumQueryMutableSegmentTree.cs

#LineLine coverage
 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
 12namespace LeetCode.Algorithms.RangeSumQueryMutable;
 13
 14/// <inheritdoc />
 15public 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>
 125    public RangeSumQueryMutableSegmentTree(int[] nums)
 126    {
 127        _n = nums.Length;
 28
 129        _tree = new int[_n * 2];
 30
 831        for (var i = 0; i < _n; i++)
 332        {
 333            _tree[i + _n] = nums[i];
 334        }
 35
 636        for (var i = _n - 1; i >= 1; i--)
 237        {
 238            var left = i * 2;
 239            var right = left + 1;
 40
 241            _tree[i] = _tree[left] + _tree[right];
 242        }
 143    }
 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)
 152    {
 153        var i = index + _n;
 54
 155        _tree[i] = val;
 56
 357        while (i > 1)
 258        {
 259            i /= 2;
 60
 261            var left = i * 2;
 262            var right = left + 1;
 63
 264            _tree[i] = _tree[left] + _tree[right];
 265        }
 166    }
 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)
 276    {
 277        var sum = 0;
 78
 279        var leftIndex = left + _n;
 280        var rightIndex = right + _n;
 81
 682        while (leftIndex <= rightIndex)
 483        {
 484            if (leftIndex % 2 == 1)
 285            {
 286                sum += _tree[leftIndex];
 87
 288                leftIndex++;
 289            }
 90
 491            if (rightIndex % 2 == 0)
 292            {
 293                sum += _tree[rightIndex];
 94
 295                rightIndex--;
 296            }
 97
 498            leftIndex /= 2;
 499            rightIndex /= 2;
 4100        }
 101
 2102        return sum;
 2103    }
 104}