Sum as per frequency

2.6

44 votes
Problem

There are N numbers A1,A2,...,An, and you are given Q queries. In each query, you are given two integers l and r.

You are required to print the sum of all the numbers whose frequency of occurrence is between l and r (including l and r). Print a single integer for each query in a new line.

Input format

  • The first line contains N denoting the size of the array.
  • The second line contains N integers denoting the elements of the array.
  • The third line contains Q denoting the number of queries.
  • Next Q lines contain l and r

Output format

For each query, print the sum of all elements of the array whose frequency of occurrence is between l and r (inclusive) in a new line.

Constraints

1N1000000

1Ai1000000

1Q1000000

1lrN

Sample Input
8
4 4 6 5 3 3 3 9
4
1 4
2 7
3 7
5 6
Sample Output
37
17
9
0
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In the first query we need to output the sum of all the numbers whose frequency of occurrecne is between 1 and 4 (inclusive). Here all the given numbers have their frequecny between 1 and 4. 3 occurs 3 times, 4 occurs 2 times, 5, 6, 9 occurs 1 time. So total sum would be 3+3+3+4+4+5+6+9 = 37.

Editor Image

?