Let us take an example, A = [a,\ b,\ c]
The subsequences of A with their PooPoo sum are:
[a] : (a)^2 = a^2
[b] : (b)^2 = b^2
[c] : (c)^2 = c^2
[a, b] : (a - b)^2 = a^2 + b^2 - 2ab
[a, c] : (a - c)^2 = a^2 + c^2 - 2ac
[b, c] : (b - c)^2 = b^2 + c^2 - 2bc
[a, b, c] : (a - b + c)^2 = a^2 + b^2 + c^2 + 2(-ab - bc + ac)
If we add them, we get :- 4[(a^2 + b^2 + c^2)\ -\ (ab + bc)]
For 4 elements, we get :- 8[(a^2 + b^2 + c^2 + d^2)\ -\ (ab + bc + cd)]
As you would have guessed from the pattern, the answer for array A of n elements is:
2^{n-1}\ \Big(\ \sum_{i\ =\ 1}^{n}\ A_i^2\ - \sum_{i\ =\ 1}^{n-1}\ A_i * A_{i+1}\ \Big)
Now the question reduces to the following:
For a given range [L,\ R], find the sum of squares of the elements and the sum of the product of adjacent elements, along with point updates.
The above operations deal with range sums and point updates and hence can be solved by using Segment Trees or Binary Indexed Trees (BIT) / Fenwick Tree.
Here are my codes:
Using Segment Tree: https://www.codechef.com/viewsolution/15108856
Using Fenwick Tree: https://www.codechef.com/viewsolution/15112212
To keep things separate, I’ve used one tree to store the sum of squares of elements and the other to store the sum of the product of adjacent elements.