-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAbstractSegmentTree.cs
More file actions
67 lines (50 loc) · 2.3 KB
/
AbstractSegmentTree.cs
File metadata and controls
67 lines (50 loc) · 2.3 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
namespace AlgorithmsAndDataStructures.DataStructures.SegmentTree;
public abstract class AbstractSegmentTree
{
private readonly Func<int, int, int> segmentAggregate;
protected AbstractSegmentTree(int[] input, Func<int, int, int> segmentAggregate)
{
if (input is null) throw new ArgumentNullException(nameof(input));
var x = (int)Math.Ceiling(Math.Log(input.Length) / Math.Log(2));
var treeSize = 2 * (int)Math.Pow(2, x) - 1;
Tree = new int[treeSize];
this.segmentAggregate = segmentAggregate;
Build(input, 0, input.Length - 1, 0);
OriginalInput = input;
OriginalInputLength = OriginalInput.Length;
}
#pragma warning disable CA1819 // Properties should not return arrays
protected int[] Tree { get; }
#pragma warning restore CA1819 // Properties should not return arrays
#pragma warning disable CA1819 // Properties should not return arrays
protected int[] OriginalInput { get; }
#pragma warning restore CA1819 // Properties should not return arrays
protected int OriginalInputLength { get; }
protected abstract int DummyValue { get; set; }
private int Build(IReadOnlyList<int> input, int start, int end, int current)
{
if (start == end)
{
Tree[current] = input[end];
return Tree[current];
}
var middle = start + (end - start) / 2;
Tree[current] = segmentAggregate(Build(input, start, middle, current * 2 + 1),
Build(input, middle + 1, end, current * 2 + 2));
return Tree[current];
}
public int GetSegmentValue(int start, int end)
{
return GetSegmentValueInternal(0, 0, OriginalInput.Length - 1, start, end);
}
private int GetSegmentValueInternal(int currentPosition, int currentStart, int currentEnd, int start, int end)
{
if (currentStart >= start && currentEnd <= end) return Tree[currentPosition];
if (currentEnd < start || currentStart > end) return DummyValue;
var middle = currentStart + (currentEnd - currentStart) / 2;
return segmentAggregate(GetSegmentValueInternal(currentPosition * 2 + 1, currentStart, middle, start, end),
GetSegmentValueInternal(currentPosition * 2 + 2, middle + 1, currentEnd, start, end));
}
}