Exemplo n.º 1
0
        public void VerifiesRUPQBinaryIndexedTree()
        {
            var rand = new Random();

            for (int a = 0; a < _sourceArrays.Length; ++a)
            {
                var sourceArray = _sourceArrays[a];
                var rupqBinaryIndexedTree = new RUPQBinaryIndexedTree(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)
                    {
                        NaiveBinaryIndexedTreeAlternatives.RangeUpdate(sourceArray, startIndex, endIndex, delta: r);
                        rupqBinaryIndexedTree.RangeUpdate(startIndex, endIndex, delta: r);
                    }
                    else
                    {
                        var expected = NaiveBinaryIndexedTreeAlternatives.ValueQuery(sourceArray, firstIndex);
                        Assert.AreEqual(expected, rupqBinaryIndexedTree.ValueQuery(firstIndex));
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void RUPQRandomOperation()
        {
            var rupqBinaryIndexedTree = new RUPQBinaryIndexedTree(_array);

            for (int i = 0; i < _randomRangesCount; ++i)
            {
                Tuple<int, int> range = _randomRanges[i];

                if (range.Item1 % 2 == 0)
                {
                    int sum = 0;
                    for (int j = range.Item1; j <= range.Item2; ++j)
                    {
                        sum += rupqBinaryIndexedTree.ValueQuery(j);
                    }
                }
                else
                {
                    rupqBinaryIndexedTree.RangeUpdate(range.Item1, range.Item2, 1);
                }
            }
        }
Exemplo n.º 3
0
        private void RUPQRangeUpdate()
        {
            var rupqBinaryIndexedTree = new RUPQBinaryIndexedTree(_array);

            for (int i = 0; i < _randomRangesCount; ++i)
            {
                Tuple<int, int> range = _randomRanges[i];

                rupqBinaryIndexedTree.RangeUpdate(range.Item1, range.Item2, 1);
            }
        }
Exemplo n.º 4
0
        private void RUPQSumQuery()
        {
            var rupqBinaryIndexedTree = new RUPQBinaryIndexedTree(_array);

            for (int i = 0; i < _randomRangesCount; ++i)
            {
                Tuple<int, int> range = _randomRanges[i];

                int sum = 0;
                for (int j = range.Item1; j <= range.Item2; ++j)
                {
                    sum += rupqBinaryIndexedTree.ValueQuery(j);
                }
            }
        }