Under construction

Still being built. Things may change

Sums of Sums (Large)

Time limit5sMemory limit512 MB

Summary
Given a positive array, sort all its subarray sums and answer the sum of entries ranked L through R for each query.
Level

Medium7 of 10

Topics
Binary search, Two pointers, Prefix sum
Solved
No attempts yet

Problem

Alice gave her friend Bob an array of NN positive integers, indexed from 11 to NN. She asked him many queries of the form "what is the sum of the numbers between these two indexes?", and Bob answered them without any trouble.

So Alice took her array and found all N(N+1)/2N(N+1)/2 non-empty subarrays of it. She computed the sum of each subarray, then sorted those sums in nondecreasing order into a new array, indexed from 11 to N(N+1)/2N(N+1)/2. For the initial array [2,3,2][2, 3, 2] the subarrays are [2][2], [3][3], [2][2], [2,3][2, 3], [3,2][3, 2], [2,3,2][2, 3, 2]. Note that [2,2][2, 2] is not a subarray. Their sums are 22, 33, 22, 55, 55, 77, and sorting them gives the new array [2,2,3,5,5,7][2, 2, 3, 5, 5, 7].

Alice hands Bob the initial array together with QQ queries of the form "what is the sum of the numbers from index LiL_i to index RiR_i, inclusive, in the new array?" Now Bob is stuck. Help him out.

Input

The first line contains the number of test cases, TT. TT test cases follow.

Each test case begins with one line holding two space separated integers NN and QQ, the number of elements of the initial array and the number of queries. The next line holds NN space separated integers, the elements of the initial array. Then come QQ lines with two space separated integers each, LiL_i and RiR_i, the inclusive index bounds of the ii-th query.

Limits

  • 1≤T≤101 \le T \le 10
  • 1≤Q≤201 \le Q \le 20
  • every element of the initial array is between 11 and 100100
  • 1≤Li≤Ri≤N(N+1)/21 \le L_i \le R_i \le N(N+1)/2
  • 1≤N≤2000001 \le N \le 200000

Output

For each test case, print one line with Case #x:, where xx is the test case number starting from 11. Then print QQ lines, each with one integer, the answers to the queries in the order they were asked.

Note

For the array [5,4,3,2,1][5, 4, 3, 2, 1], the new array is [1,2,3,3,4,5,5,6,7,9,9,10,12,14,15][1, 2, 3, 3, 4, 5, 5, 6, 7, 9, 9, 10, 12, 14, 15].

Examples2

  1. Example 1

    Input
    1
    5 5
    5 4 3 2 1
    1 1
    1 10
    1 15
    3 8
    4 11
    
    Expected output
    Case #1:
    1
    45
    105
    26
    48
    
  2. Example 2

    Input
    1
    3 5
    2 3 2
    1 1
    1 6
    2 4
    6 6
    3 5
    
    Expected output
    Case #1:
    2
    24
    10
    7
    13