FIBQ - editorial



Author: Sunny Aggarwal
Tester: Sergey Kulik
Editorialist: Mugurel Ionut Andreica




Fibonacci Numbers, Segment Trees


Given an array A consisting of N elements, Chef asked you to process following two types of queries on this array accurately and efficiently.

  • C X Y: Change the value of X-th element of array to Y i.e A[X] = Y.
  • Q L R: Compute the function F over the subarray defined by the elements of array A in the range L to R, both inclusive.

The function F(S) is defined as F(S)=(\sum_{W\subseteq S}{Fibonacci(Sum(W))})\ modulo\ (10^9+7), where Sum(W) denotes the sum of all the elements in the sub-multiset W and Fibonacci(Z)=the Z-th Fibonacci number.

The function F applied over a subarray [L,R] of the array A is defined as F(S) where S is the multiset consisting of all the elements from the range [L,R] from the array A (i.e. S={A(L), A(L+1), …, A®}).


We can use the following property of Fibonacci numbers: Fibonacci(A+B)=Fibonacci(A)xFibonacci(B+1)+Fibonacci(A-1)xFibonacci(B).

With this property we can use a segment tree in order to efficiently process updates and queries. For each interval corresponding to a node of the segment tree we will store 3 values:

  • sfib = sum of the values Fibonacci(Sum(W)) (modulo 10^9+7), for all the sub-multisets W
  • sfibm1 = sum of the values Fibonacci(Sum(W) - 1) (modulo 10^9+7), for all the sub-multisets W
  • sfibp1 = sum of the values Fibonacci(Sum(W) + 1) (modulo 10^9+7), for all the sub-multisets W

For the leaves of the segment tree we will initialize these values directly. For a leaf node L corresponding to a position i of the array A we will simply set:

  • L.sfib = Fibonacci(A[i])
  • L.sfibm1 = Fibonacci(A[i] - 1)
  • L.sfibp1 = Fibonacci(A[i] + 1)

Since A[i] can be pretty large, we need an efficient method of computing Fibonacci(A[i]) (and, in general, for computing Fibonacci(Y) for Y as large as 10^9). There are multiple methods which can be used for computing these values in O(log(Y)) time. Below you can see one such function which uses memoization:

Fibonacci(y) {
    if (y <= 0) return 0;
    if (y <= 2) return 1;
    if (y in fibonacci_cache) {
        return fibonacci_cache[y];
    int f, b, a;
    b = y / 2; // integer division
    a = y - b;
    f = (Fibonacci(a) * Fibonacci(b + 1) + Fibonacci(a - 1) * Fibonacci(b)) % MOD;
    fibonacci_cache[y] = f;
    return f;

For a non-leaf node node we can use the following function for computing its information based on the information available in its left and right children:

CombineIntervalInfo(left, right, node) {
    node.sfib = (left.sfib + right.sfib + left.sfib * right.sfibp1 + left.sfibm1 * right.sfib) % MOD;
    node.sfibm1 = (left.sfibm1 + right.sfibm1 + left.sfib * right.sfib + left.sfibm1 * right.sfibm1) % MOD;
    node.sfibp1 = (left.sfibp1 + right.sfibp1 + left.sfibp1 * right.sfibp1 + left.sfib * right.sfib) % MOD;

Processing an update operation X Y can be done as follows in O(log(N)) time. For the leaf node L corresponding to the position X from the array we reinitialize its values:

  • L.sfib = Fibonacci(Y)
  • L.sfibm1 = Fibonacci(Y - 1)
  • L.sfibp1 = Fibonacci(Y + 1)

Then, for every ancestor of the node L, starting from its parent and going up to the root, we recompute its information using the CombineIntervalInfo function.

Handling a query L R can be done as follows, also in O(log(N)) time. We find the O(log(N)) nodes of the segment tree such that the disjoint union of their intervals is equal to the interval [L,R]. Let these nodes be node(1), …, node(K). If K=1 then the answer is node(K).sfib. Otherwise, we will we compute a tuple ANS containing the same fields sfib, sfibm1 and sfibp1 as the information stored for each segment tree node. We initialize ANS by combining the information from node(1) and node(2). Then, for every 3\leq i\leq K we update ANS by combining its information with that of node(i). At the end, the answer is ANS.sfib.


Author’s solution can be found here.
Tester’s solution can be found here.
Editorialist’s solution can be found here.


A very nice problem, also excited for other approaches too.

Alternate approach :

Build a segment tree where we store a 2x2 transition matrix in each node . ( Transition matrix is [1,1],[1,0] and fib(n) = T^n ) .
Let matrix for parent and its two child nodes be X,A,B respectively . We can easily see that X = A+B+A*B .
To get the final answer just multiply the matrix by the base case matrix ([0,1]) .


I used Matrix exponentiation in my Solution
I have added my explanation in it.


Can this be solved using the golden ratio?

Where is the specific property described? What is its proof?


Let f(x) be the xth fibonacci number. Let M = [ [1,1], [1,0]].

Consider a 1-element range. The answer to this query is f(x) where x is the element, which is given by the [1][0] element of M^x. Let us call this A for now.

Consider now a 2-element range (x,y). Here the result must be f(x) + f(y) + f(x+y). Which is the [1][0] element of A + M^y + A.M^y where A = M^x and we use M^(x+y) = M^x.M^y . Now let us call this matrix as A.

Similarly for the 3-element range (x,y,z), we get the result as A + M^z + A.M^z and we can clearly see the recurrence kind of relation.

You can get 20 points for running a loop from l to r. But for 100 points you need to build a segment tree with each node containing a Matrix (yes, the segtree will be a 3-D array, an array of 2-D Matrices).

Here is the code. Took much less time than the editorial code and much much less heap memory.


@ash_code can be solved using golden ratio … check this

check the last comments of [this][2].


I used the golden ratio and the polynomial multiplication approach


There is one more optimization which should work for computing fibonacci. For modulus 10^9+7, pisano number was 2*10^9+18. So if number greater than this, we first take modulus with 2 * 10^9 + 18 and then compute Fibonacci.

It would be great if you can explain me how the multisets are generated of each interval in the segment tree from Fibo(A+B)?


The problem could be made more challenging by introducing range update :slight_smile:

good explanation can u describe how to done it with segment trees @atulshanbhag

To partially answer your question: the interval multisets are not generated, it’s not necessary. CombineIntervalInfo will calculate the correct answer, but I don’t understand how. Specifically I don’t understand how “F(A+B) = F(A).F(B+1) + F(A-1).F(B)” can be applied to Sum(F(Sum(s))) for s in multisets.

Here is an example where I computed the combination of [3, 4] and [1,2] and then [3, 4, 1, 2] separately, just to confirm it works:

I also I don’t find the relationship between Fibonacci(n+m) and F(A U {x}) intuitive at all. Does anyone have a justification of this, other than ‘clearly’ or ‘notice’ or ‘easily see’?

Can anyone explain how they are finding the sum of individual subsets in a given range?

Please mention the topics or Algorithms along with the links that are required as a prerequisite for this problem…
Can anyone explain this editorial in an easy language…that is understandable to a newbiee…
Thanks in advance (y)


Here is a decent intro to segment trees:

Here is a reference I used for Fibonacci generation:

Hope that helps, though I think there is a little bit of mystery that noone has explained linked to regarding this particular function (not prereqs).

Can some one explain how CombineIntervalInfo() generates multiset as well as the summation.
Thanx in advance.

whole time i was figuring out how can we get all subset of the set… as to me that was the main problem… but after contest ended i came to know of -> (1+a)(1+b)(1+c)…(1+n)=1 + (all__subset__in__product__form)… eg. (1+a)(1+b)(1+c)=1+a+b+c+ab+bc+ca+abc…

as u can see now we just can now easily use segment tree…