Mark As Completed Discussion

Try this exercise. Fill in the missing part by typing it in.

To update a value in a Fenwick Tree, we need to make two changes: update the original array and update the prefix sum in the Fenwick Tree. Let's say we want to update the value at index i with a new value val.

In order to update the original array, we simply assign the new value val to arri.

TEXT/X-C++SRC
1int index = i;
2int value = val;
3arr[index] = value;

To update the prefix sum in the Fenwick Tree, we need to find the difference between the new value val and the old value arri. Let's call this difference diff. Then, we update the Fenwick Tree by adding diff to the prefix sum at each index that represents the range where the value at index i contributes.

TEXT/X-C++SRC
1void update(int BIT[], int n, int i, int val) {
2    while (i <= n) {
3        BIT[i] += val;
4        i += i & (-i);
5    }
6}

In the code snippet above, fill in the blank with a suitable term: _________ += val;

Write the missing line below.