Exemple #1
0
        private void VerifiesUpdates <TQueryObject>(Func <IReadOnlyList <int>, int, int, int> naiveVerifier)
            where TQueryObject : SegmentTreeQueryObject <TQueryObject, int>, new()
        {
            Func <int, int> updater = x => x + 1;

            for (int a = 0; a < _sourceArrays.Length; ++a)
            {
                var sourceArray             = _sourceArrays[a];
                var nodeBasedSegmentTree    = new NodeBasedSegmentTree <TQueryObject, int>(sourceArray);
                var arrayBasedSegmentTree   = new ArrayBasedSegmentTree <TQueryObject, int>(sourceArray);
                var nonRecursiveSegmentTree = new NonRecursiveSegmentTree <TQueryObject, int>(sourceArray);

                for (int i = 0; i < sourceArray.Length; ++i)
                {
                    for (int j = i; j < sourceArray.Length; ++j)
                    {
                        NaiveSegmentTreeAlternatives.Update(sourceArray, i, j, updater);
                        nodeBasedSegmentTree.Update(i, j, updater);
                        arrayBasedSegmentTree.Update(i, j, updater);
                        nonRecursiveSegmentTree.Update(i, j, updater);

                        int expected = naiveVerifier(sourceArray, i, j);
                        Assert.AreEqual(expected, nodeBasedSegmentTree.Query(i, j));
                        Assert.AreEqual(expected, arrayBasedSegmentTree.Query(i, j));
                        Assert.AreEqual(expected, nonRecursiveSegmentTree.Query(i, j));
                    }
                }
            }
        }
Exemple #2
0
        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));
                    }
                }
            }
        }
Exemple #3
0
 // https://blog.anudeep2011.com/heavy-light-decomposition/
 // https://www.geeksforgeeks.org/heavy-light-decomposition-set-2-implementation/
 public QTREE(int vertexCount, int[,] edges)
 {
     _edges                = edges;
     _tree                 = WeightedRootedTree.CreateFromEdges(vertexCount, rootID: 0, edges: edges);
     _eulerTour            = _tree.GetEulerTour();
     _eulerTourSegmentTree = new ArrayBasedSegmentTree <MinimumDepthQueryObject, Vertex>(_eulerTour);
     _tree.RunHLD();
     _hldBaseArraySegmentTree = new ArrayBasedSegmentTree <MaximumWeightQueryObject, Vertex>(_tree.HLDBaseArray);
 }
Exemple #4
0
 public LCA(int vertexCount, List <int>[] verticesChildren)
 {
     _tree        = RootedTree.CreateFromChildren(vertexCount, 0, verticesChildren);
     _eulerTour   = _tree.GetEulerTour();
     _segmentTree = new ArrayBasedSegmentTree(_eulerTour);
 }
Exemple #5
0
 public GSS1(int[] sourceArray)
 {
     _segmentTree = new ArrayBasedSegmentTree(sourceArray);
 }
Exemple #6
0
 public FREQUENT(int[] sourceArray)
 {
     _segmentTree = new ArrayBasedSegmentTree(sourceArray);
 }
Exemple #7
0
 public GSS3(IReadOnlyList <int> sourceArray)
 {
     _segmentTree = new ArrayBasedSegmentTree(sourceArray);
 }
Exemple #8
0
 public RPLN(int[] scores)
 {
     _segmentTree = new ArrayBasedSegmentTree(scores);
 }
Exemple #9
0
 public BRCKTS(string brackets)
 {
     _brackets    = brackets;
     _segmentTree = new ArrayBasedSegmentTree(brackets);
 }
Exemple #10
0
 public GSS3(IReadOnlyList<int> sourceArray)
 {
     _segmentTree = new ArrayBasedSegmentTree(sourceArray);
 }