// Main function
    public static void Main(string[] args)
    {
        int[]             freq = new int[] { 2, 1, 1, 3, 2, 3, 4, 5, 6, 7, 8, 9 };
        int               n    = freq.Length;
        BinaryIndexedTree tree = new BinaryIndexedTree();

        // Build fenwick tree from given array
        tree.constructBITree(freq, n);
        Console.WriteLine("Sum of elements in arr[0..5]" + " is = " + tree.getSum(5));
        freq[3] += 6;
        updateBIT(n, 3, 6);
        // Find sum after the value is updated
        Console.WriteLine("Sum of elements in arr[0..5]" + " after update is = " + tree.getSum(5));
    }
Beispiel #2
0
    // Driver code
    public static void Main(String [] args)
    {
        Console.WriteLine("Enter the number of elements .");
        int n = int.Parse(Console.ReadLine());

        int [] freq = new int[n];
        for (i = 0; i < n; i++)
        {
            freq[i] = int.Parse(Console.ReadLine());
        }

        BinaryIndexedTree tree = new BinaryIndexedTree();

        // Build fenwick tree from given array
        tree.constructBITree(freq, n);

        Console.WriteLine("Sum of elements in arr[0..5]" +
                          " is " + tree.getSum(5));

        // Let use test the update operation
        freq[3] += 6;
        // Update BIT for above change in arr[]
        updateBIT(n, 3, 6);
        // Find sum after the value is updated
        Console.WriteLine("Sum of elements in arr[0..5]" +
                          " after update is " + tree.getSum(5));
        //Sample input and output.
        int []            freq = { 5, 1, 0, 3, 2, 1, 4, 2, 6, 4, 8, 9 };
        int               n    = freq.Length;
        BinaryIndexedTree tree = new BinaryIndexedTree();

        // Building fenwick tree from given array
        tree.constructBITree(freq, n);
        Console.WriteLine("Sum of elements in arr[0..5]" + " is " + tree.getSum(5));
        freq[3] += 6;
        // Update BIT for above change in arr[]
        updateBIT(n, 3, 6);
        Console.WriteLine("Sum of elements in arr[0...5]" + "is" + "after update is " + tree.getSum(5));
    }