I am a beginner in competitive programming.
I've read a problem where I have to output the number of frequency of an element into a sub-array range.
I've implemented that using normal for loop and counting the frequency. But I got TLE. I need an efficient function for it. Can you please write the C++ implementation for me? Its only for my learning purpose.
Here the the problem sample:
Input an array of N;
Then, enter number of queries q,
After that, in each line there will be given a sub array range i,j and an element e;
You have to find the frequency of element e into the sub-array.
suppose that you store or every number in the array the indexes where it appears
By instance, for "4 1 6 4 7 9 1 3 2 6" you'll have index[1] = {1, 6}
with this you can easily compute the accumulate frequency over the array, and to find it in the range is as simple as accumulate[b] - accumulate[a]