This page is still under construction.

Parts of this page are still being built. What you see may change.

Power of the Array

Time limit3sMemory limit128 MB

Summary
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 nn natural numbers a1,a2,a3,…,ana_1, a_2, a_3, \dots, a_n.

The subarray from ll to rr is al,al+1,…,ara_l, a_{l+1}, \dots, a_r.

Let KsK_s be the number of times the natural number ss appears in the subarray.

The power of a subarray is the sum of Ks⋅Ks⋅sK_s \cdot K_s \cdot s over all natural numbers ss.

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 nn and the number of subarrays tt (1≤n,t≤1051 \le n, t \le 10^5).

The second line contains nn natural numbers aia_i separated by spaces (1≤ai≤1061 \le a_i \le 10^6).

Each of the next tt lines contains two integers lil_i and rir_i that describe the range of a subarray (1≤li≤ri≤n1 \le l_i \le r_i \le n).

Output

For each subarray given in the input, print its power on its own line.

Examples4

  1. Example 1

    Input
    8 3
    4 3 1 1 1 3 1 2
    2 7
    1 6
    3 8
    
    Expected output
    28
    25
    21
    
  2. Example 2

    Input
    1 1
    5
    1 1
    
    Expected output
    5
    
  3. Example 3

    Input
    5 5
    1 2 3 4 5
    1 1
    2 2
    3 3
    4 4
    5 5
    
    Expected output
    1
    2
    3
    4
    5
    
  4. Example 4

    Input
    5 2
    10 20 30 40 50
    1 5
    2 4
    
    Expected output
    150
    90