Power of the Array
Time limit3sMemory limit128 MB
Given an array and t range queries, compute for each subarray the sum over values s of s times the square of s's frequency in the range.
- Level
Hard8 of 10
- Topics
- Array, Prefix sum, Sorting, Math
- Solved
- No attempts yet
Problem
You are given an array of natural numbers .
The subarray from to is .
Let be the number of times the natural number appears in the subarray.
The power of a subarray is the sum of over all natural numbers .
Given the array and the ranges of several subarrays, write a program that computes the power of each subarray.
Input
The first line contains the size of the array and the number of subarrays ().
The second line contains natural numbers separated by spaces ().
Each of the next lines contains two integers and that describe the range of a subarray ().
Output
For each subarray given in the input, print its power on its own line.