-
Notifications
You must be signed in to change notification settings - Fork 1
/
MinSegmentTree.cs
41 lines (32 loc) · 1.23 KB
/
MinSegmentTree.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
namespace AlgorithmsAndDataStructures.DataStructures.SegmentTree
{
public class MinSegmentTree : AbstractSegmentTree
{
public MinSegmentTree(int[] input)
:base(input, Math.Min)
{
}
protected override int DummyValue { get; set; } = int.MaxValue;
public void Update(int index, int value)
{
UpdateInternal(0, 0, OriginalInputLength - 1, index, value);
}
private int UpdateInternal(int currentPosition, int currentStart, int currentEnd, int index, int value)
{
if (currentStart == currentEnd && currentStart == index)
{
Tree[currentPosition] = value;
return Tree[currentPosition];
}
if (index > currentEnd || index < currentStart)
{
return Tree[currentPosition];
}
var middle = currentStart + (currentEnd - currentStart) / 2;
Tree[currentPosition] = Math.Min(UpdateInternal(currentPosition * 2 + 1, currentStart, middle, index, value),
UpdateInternal(currentPosition * 2 + 2, middle + 1, currentEnd, index, value));
return Tree[currentPosition];
}
}
}