public void LazySumSegmentTree_ForRandomOperations() { var rand = new Random(); for (int a = 0; a < _sourceArrays.Length; ++a) { var sourceArray = _sourceArrays[a]; var lazySumSegmentTree = new LazySumSegmentTree(sourceArray); var arrayBasedSegmentTree = new ArrayBasedSegmentTree <SumQueryObject, int>(sourceArray); for (int r = 0; r < 1000; ++r) { int firstIndex = rand.Next(0, sourceArray.Length); int secondIndex = rand.Next(0, sourceArray.Length); int startIndex = Math.Min(firstIndex, secondIndex); int endIndex = Math.Max(firstIndex, secondIndex); int mode = rand.Next(2); if (mode == 0) { NaiveSegmentTreeAlternatives.Update(sourceArray, startIndex, endIndex, x => x + r); lazySumSegmentTree.Update(startIndex, endIndex, rangeAddition: r); arrayBasedSegmentTree.Update(startIndex, endIndex, x => x + r); } else { int expected = NaiveSegmentTreeAlternatives.SumQuery(sourceArray, startIndex, endIndex); Assert.AreEqual(expected, lazySumSegmentTree.SumQuery(startIndex, endIndex)); Assert.AreEqual(expected, arrayBasedSegmentTree.Query(startIndex, endIndex)); } } } }
private void SegmentTreeRangeUpdate() { var segmentTree = new LazySumSegmentTree(_array); for (int i = 0; i < _randomRangesCount; ++i) { Tuple <int, int> range = _randomRanges[i]; segmentTree.Update(range.Item1, range.Item2, 1); } }
private void SegmentTreeRandomOperation() { var segmentTree = new LazySumSegmentTree(_array); for (int i = 0; i < _randomRangesCount; ++i) { Tuple <int, int> range = _randomRanges[i]; if (range.Item1 % 2 == 0) { segmentTree.SumQuery(range.Item1, range.Item2); } else { segmentTree.Update(range.Item1, range.Item2, 1); } } }
public void Update(int updateStartIndex, int updateEndIndex, int rangeAddition) => _segmentTree.Update(updateStartIndex, updateEndIndex, rangeAddition);