Sums of Sums (Large)
Time limit5sMemory limit512 MB
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 positive integers, indexed from to . 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 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 to . For the initial array the subarrays are , , , , , . Note that is not a subarray. Their sums are , , , , , , and sorting them gives the new array .
Alice hands Bob the initial array together with queries of the form "what is the sum of the numbers from index to index , inclusive, in the new array?" Now Bob is stuck. Help him out.
Input
The first line contains the number of test cases, . test cases follow.
Each test case begins with one line holding two space separated integers and , the number of elements of the initial array and the number of queries. The next line holds space separated integers, the elements of the initial array. Then come lines with two space separated integers each, and , the inclusive index bounds of the -th query.
Limits
- every element of the initial array is between and
Output
For each test case, print one line with Case #x:, where is the test case number starting from . Then print lines, each with one integer, the answers to the queries in the order they were asked.
Note
For the array , the new array is .