Given an array A of integers , I am trying to find out at a given position j , how many times A[j] occurs from every i=0 to i=j in A. I have devised a solution like below
map<int,int> CF[400005];
for(int i=0;i<n;++i)
{
cin>>A[i];
if(i!=0)
CF[i-1] = CF[i];
++CF[i][A[i]];
}
than I can answer each query in logn time. But this procedure uses too much memory. Is there any way of doing it using less memory?
for more clarification , you can see the problem I am trying to solve http://codeforces.com/contest/190/problem/D
Create an array
B
of the same size asA
and a mapC
. For eachj
inB[j]
keep track of the number of occurrences ofA[j]
beforej
. InC
keep track of the last occurrence of given element. Then you answer queries in constant time and it requires just O(n) memory.Sorry for my pseudo-C++.