コード例 #1
0
        private int _query(SegmentTreeNode node, int start, int end)
        {
            if (start <= node.Start && node.End <= end)
            {
                return(node.Value);
            }
            int retValue = 0;

            if (SegmentTreeType == SegmentTreeType.Max)
            {
                retValue = int.MinValue;
            }
            else if (SegmentTreeType == SegmentTreeType.Min)
            {
                retValue = int.MaxValue;
            }
            if (start <= (node.Start + node.End) / 2)
            {
                retValue = getValueByType(retValue, _query(node.LeftChild, start, end));
            }
            if ((node.Start + node.End) / 2 + 1 <= end)
            {
                retValue = getValueByType(retValue, _query(node.RightChild, start, end));
            }
            return(retValue);
        }
コード例 #2
0
 public SegmentTreeNode BuildTree(int[] inputArray)
 {
     if (inputArray == null || inputArray.Length == 0)
     {
         return(null);
     }
     Root = _build(0, inputArray.Length - 1, inputArray);
     return(Root);
 }
コード例 #3
0
        private SegmentTreeNode _build(int start, int end, int[] array)
        {
            SegmentTreeNode node = new SegmentTreeNode(start, end, array[start]);

            if (start == end)
            {
                return(node);
            }

            node.LeftChild  = _build(start, (start + end) / 2, array);
            node.RightChild = _build((start + end) / 2 + 1, end, array);
            node.Value      = getValueByType(node.LeftChild.Value, node.RightChild.Value);
            return(node);
        }
コード例 #4
0
 private void _update(SegmentTreeNode node, int index, int value)
 {
     if (node.Start == node.End && node.Start == index)
     {
         node.Value = value;
         return;
     }
     if (index <= (node.Start + node.End) / 2)
     {
         _update(node.LeftChild, index, value);
     }
     else
     {
         _update(node.RightChild, index, value);
     }
     node.Value = getValueByType(node.LeftChild.Value, node.RightChild.Value);
 }