< 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) 2026 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 sealed 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>
 525    public RangeSumQueryMutableSegmentTree(int[] nums)
 526    {
 527        _n = nums.Length;
 28
 529        _tree = new int[_n * 2];
 30
 4031        for (var i = 0; i < _n; i++)
 1532        {
 1533            _tree[i + _n] = nums[i];
 1534        }
 35
 3036        for (var i = _n - 1; i >= 1; i--)
 1037        {
 1038            var left = i * 2;
 1039            var right = left + 1;
 40
 1041            _tree[i] = _tree[left] + _tree[right];
 1042        }
 543    }
 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)
 752    {
 753        var i = index + _n;
 54
 755        _tree[i] = val;
 56
 1957        while (i > 1)
 1258        {
 1259            i /= 2;
 60
 1261            var left = i * 2;
 1262            var right = left + 1;
 63
 1264            _tree[i] = _tree[left] + _tree[right];
 1265        }
 766    }
 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)
 976    {
 977        var sum = 0;
 78
 979        var leftIndex = left + _n;
 980        var rightIndex = right + _n;
 81
 2382        while (leftIndex <= rightIndex)
 1483        {
 1484            if (leftIndex % 2 == 1)
 985            {
 986                sum += _tree[leftIndex];
 87
 988                leftIndex++;
 989            }
 90
 1491            if (rightIndex % 2 == 0)
 792            {
 793                sum += _tree[rightIndex];
 94
 795                rightIndex--;
 796            }
 97
 1498            leftIndex /= 2;
 1499            rightIndex /= 2;
 14100        }
 101
 9102        return sum;
 9103    }
 104}