DOUBT IN APPROACH

In this ques What I can Think of is using a segment tree where each range will have f(x,k) sum for that range but problem that i am facing is how to store f(x,k) in node

1 approach is to have an array of size n for each node of segment tree where each index represent the count of time particular number in array occur in that range but this soln go upto O(n^2)

2 Is to calculate f(x,k) for each range but here problem is now how will i tackle update as for e.g. A[4] can occur in any range so update will also be a difficult job