Given the following problem:
There is a sequence of k integers, named s for which there can be 2 operations,
1) Sum[i,j] - What is the value of s[i]+s[i+1]+...+s[j]?
2) Update[i,val] - Change the value of s[i] to val.
I am sure most people here have heard of using a cumulative frequency table/fenwick tree to optimize the complexity.
Now, if I don't want to query the sum but instead I want to perform the following:
Product[i,j] - What is the value of s[i] * s[i+1] * ... * s[j]?
The new problem seems trivial at first, at least for the first operation Product[i,j].
Assuming I am using a cummulative product table named f:
- At first thought, when we call Update[i,val], we should divide the cummulative products at f[z] for z from i -> j by the old value of s[i] then multiply by the new value.
But we will face 2 issues if the old value of s[i] is 0:
Division by 0. But this is easily tackled by checking if the old value of s[i] is 0.
The product of any real number with 0 is 0. This result will cause all other values from f[i] to f[j] to be 0. So we are unable to successfully perform Update[i,val]. This problem is not so trivial as it affects other values besides f[i].
Does anyone have any ideas how I could implement a cummulative product table that supports the 2 operations mentioned above?