Example #1
0
File: GSS3.cs Project: Dariasz/SPOJ
 public void Update(MaximumSumQueryObject updatedLeftChild, MaximumSumQueryObject updatedRightChild)
 {
     Sum                     = GetCombinedSum(updatedLeftChild, updatedRightChild);
     MaximumSum              = GetCombinedMaximumSum(updatedLeftChild, updatedRightChild);
     MaximumLeftStartingSum  = GetCombinedMaximumLeftStartingSum(updatedLeftChild, updatedRightChild);
     MaximumRightStartingSum = GetCombinedMaximumRightStartingSum(updatedLeftChild, updatedRightChild);
 }
Example #2
0
File: GSS3.cs Project: Dariasz/SPOJ
    }                                                 // [ ... <-]

    public MaximumSumQueryObject Combine(MaximumSumQueryObject rightAdjacentObject)
    => new MaximumSumQueryObject
    {
        SegmentStartIndex = SegmentStartIndex,
        SegmentEndIndex   = rightAdjacentObject.SegmentEndIndex,
        Sum                     = GetCombinedSum(this, rightAdjacentObject),
        MaximumSum              = GetCombinedMaximumSum(this, rightAdjacentObject),
        MaximumLeftStartingSum  = GetCombinedMaximumLeftStartingSum(this, rightAdjacentObject),
        MaximumRightStartingSum = GetCombinedMaximumRightStartingSum(this, rightAdjacentObject)
    };
Example #3
0
    private void Build(int treeArrayIndex, int segmentStartIndex, int segmentEndIndex)
    {
        if (segmentStartIndex == segmentEndIndex)
        {
            _treeArray[treeArrayIndex] = new MaximumSumQueryObject(segmentStartIndex, _sourceArray[segmentStartIndex]);
            return;
        }

        int leftChildTreeArrayIndex = 2 * treeArrayIndex + 1;
        int rightChildTreeArrayIndex = leftChildTreeArrayIndex + 1;

        Build(leftChildTreeArrayIndex, segmentStartIndex, (segmentStartIndex + segmentEndIndex) / 2);
        Build(rightChildTreeArrayIndex, (segmentStartIndex + segmentEndIndex) / 2 + 1, segmentEndIndex);

        _treeArray[treeArrayIndex] = _treeArray[leftChildTreeArrayIndex].Combine(_treeArray[rightChildTreeArrayIndex]);
    }
Example #4
0
File: GSS1.cs Project: Dariasz/SPOJ
    private void Build(int treeArrayIndex, int segmentStartIndex, int segmentEndIndex)
    {
        if (segmentStartIndex == segmentEndIndex)
        {
            _treeArray[treeArrayIndex] = new MaximumSumQueryObject(_sourceArray[segmentStartIndex]);
            return;
        }

        int leftChildTreeArrayIndex  = 2 * treeArrayIndex + 1;
        int rightChildTreeArrayIndex = leftChildTreeArrayIndex + 1;
        int leftChildSegmentEndIndex = (segmentStartIndex + segmentEndIndex) / 2;

        Build(leftChildTreeArrayIndex, segmentStartIndex, leftChildSegmentEndIndex);
        Build(rightChildTreeArrayIndex, leftChildSegmentEndIndex + 1, segmentEndIndex);

        _treeArray[treeArrayIndex] = _treeArray[leftChildTreeArrayIndex].Combine(_treeArray[rightChildTreeArrayIndex]);
    }
Example #5
0
File: GSS1.cs Project: Dariasz/SPOJ
    public MaximumSumQueryObject Combine(MaximumSumQueryObject rightAdjacentValue)
    => new MaximumSumQueryObject
    {
        // The sum is just the sum of both.
        Sum = Sum + rightAdjacentValue.Sum,

        // The maximum sum either intersects both segments, or is entirely in one.
        MaximumSum = Math.Max(
            MaximumRightStartingSum + rightAdjacentValue.MaximumLeftStartingSum,
            Math.Max(MaximumSum, rightAdjacentValue.MaximumSum)),

        // The maximum left starting sum starts at the left, and may or may not cross into the right.
        MaximumLeftStartingSum = Math.Max(
            Sum + rightAdjacentValue.MaximumLeftStartingSum,
            MaximumLeftStartingSum),

        // The maximum right starting sum starts at the right, and may or may not cross into the left.
        MaximumRightStartingSum = Math.Max(
            rightAdjacentValue.Sum + MaximumRightStartingSum,
            rightAdjacentValue.MaximumRightStartingSum)
    };
Example #6
0
File: GSS3.cs Project: Dariasz/SPOJ
 private static int GetCombinedMaximumRightStartingSum(MaximumSumQueryObject leftAdjacentObject, MaximumSumQueryObject rightAdjacentObject)
 // The maximum right starting sum starts at the right, and may or may not cross into the left.
 => Math.Max(
     rightAdjacentObject.Sum + leftAdjacentObject.MaximumRightStartingSum,
     rightAdjacentObject.MaximumRightStartingSum);
Example #7
0
File: GSS3.cs Project: Dariasz/SPOJ
 private static int GetCombinedMaximumSum(MaximumSumQueryObject leftAdjacentObject, MaximumSumQueryObject rightAdjacentObject)
 // The maximum sum either intersects both segments, or is entirely in one.
 => Math.Max(
     leftAdjacentObject.MaximumRightStartingSum + rightAdjacentObject.MaximumLeftStartingSum,
     Math.Max(leftAdjacentObject.MaximumSum, rightAdjacentObject.MaximumSum));
Example #8
0
File: GSS3.cs Project: Dariasz/SPOJ
 private static int GetCombinedSum(MaximumSumQueryObject leftAdjacentObject, MaximumSumQueryObject rightAdjacentObject)
 // The sum is just the sum of both.
 => leftAdjacentObject.Sum + rightAdjacentObject.Sum;
Example #9
0
 private static int GetCombinedMaximumRightStartingSum(MaximumSumQueryObject leftAdjacentObject, MaximumSumQueryObject rightAdjacentObject)
     // The maximum right starting sum starts at the right, and may or may not cross into the left.
     => Math.Max(
         rightAdjacentObject.Sum + leftAdjacentObject.MaximumRightStartingSum,
         rightAdjacentObject.MaximumRightStartingSum);
Example #10
0
 private static int GetCombinedMaximumSum(MaximumSumQueryObject leftAdjacentObject, MaximumSumQueryObject rightAdjacentObject)
     // The maximum sum either intersects both segments, or is entirely in one.
     => Math.Max(
         leftAdjacentObject.MaximumRightStartingSum + rightAdjacentObject.MaximumLeftStartingSum,
         Math.Max(leftAdjacentObject.MaximumSum, rightAdjacentObject.MaximumSum));
Example #11
0
 private static int GetCombinedSum(MaximumSumQueryObject leftAdjacentObject, MaximumSumQueryObject rightAdjacentObject)
     // The sum is just the sum of both.
     => leftAdjacentObject.Sum + rightAdjacentObject.Sum;
Example #12
0
 public void Update(MaximumSumQueryObject updatedLeftChild, MaximumSumQueryObject updatedRightChild)
 {
     Sum = GetCombinedSum(updatedLeftChild, updatedRightChild);
     MaximumSum = GetCombinedMaximumSum(updatedLeftChild, updatedRightChild);
     MaximumLeftStartingSum = GetCombinedMaximumLeftStartingSum(updatedLeftChild, updatedRightChild);
     MaximumRightStartingSum = GetCombinedMaximumRightStartingSum(updatedLeftChild, updatedRightChild);
 }
Example #13
0
    private int MaximumRightStartingSum { get; set; } // [ ... <-]

    public MaximumSumQueryObject Combine(MaximumSumQueryObject rightAdjacentObject)
        => new MaximumSumQueryObject()
        {
            SegmentStartIndex = SegmentStartIndex,
            SegmentEndIndex = rightAdjacentObject.SegmentEndIndex,
            Sum = GetCombinedSum(this, rightAdjacentObject),
            MaximumSum = GetCombinedMaximumSum(this, rightAdjacentObject),
            MaximumLeftStartingSum = GetCombinedMaximumLeftStartingSum(this, rightAdjacentObject),
            MaximumRightStartingSum = GetCombinedMaximumRightStartingSum(this, rightAdjacentObject)
        };