Range Update - Query a Range Using Fenwick Tree

http://ayazdzulfikar.blogspot.in/2014/12/penggunaan-fenwick-tree-bit.html?showComment=1434865697025#c5391178275473818224

For example, they say that the value of a function or f (i) index-i is i ^ k, for k> = 0 and always remains on this question. This request looks like this:

Add an array of values โ€‹โ€‹[i], for all a <= i <= b as v Define a common array [i] f (i), for each a <= i <= b (remember the previous function for refinement of values)

To work on this matter, can be formed into Query (x) = m * g (x) - c,
where g (x) is f (1) + f (2) + ... + f (x). 

      

For this, we need to know the values โ€‹โ€‹of m and c. For this we need 2 separate LITTLE. See notes below for each update as ab v. To calculate the value m, which is almost identical to updating the range - Point Query. We can get the following observations for each value of i, which can be:

i <a, m = 0
a <= i <= b, m = v
b <i, m = 0

      

Using the following observation, it is clear that the Query Range-Point Query can be used for any of the BITs. To compute the value of c, we need to observe the possibility for each value of i, which can be:

i <a, then c = 0
a <= i <= b, then c = v * g (a - 1)
b <i, c = v * (g (b) - g (a - 1))

      

Again, we need Range Update - Point Query, but in a different BIT. Oiya, for a little help, I wrote the g (x) value for k <= 3 yes: p:

k = 0 -> x
k = 1 -> x * (x + 1) / 2
k = 2 -> x * (x + 1) * (2x + 1) / 6
k = 3 -> (x * (x + 1) / 2) ^ 2

      

Now, an example of the SPOJ problem is Horrible Requests. This problem is similar to those described, with k = 0. Note also that sometimes a question arises, which is quite extreme, where the function is not for one type k, but it may be some kind of polynomial form! For example, LA - Alien Abduction Again. To solve this problem, for each rank we make our own BIT m counter accordingly. BIT combined to clear the c counters, everything was fine.

How can we use this concept if:

For an array of integers A1, A2, ... AN.

Given x, y: add 1 ร— 2 to Ax, add 2 ร— 3 to Ax + 1, add 3 ร— 4 to Ax + 2, add 4 ร— 5 to Ax + 3, and so on up to Ay.

Then return the Sum of the range [Ax, Ay].

+3


source to share





All Articles