14753 - Stair Sum   

Description

You are given a integer sequence (a1, a2, ..., an) of length n.

In this problem, you need to answer q quiries, each query is of the form:

  • l r

Meaning you need to answer the sum of al * 1 + al+1 * 2 + al+2 * 3 + ... + ar-1 * (r - l) + ar * (r - l + 1).

 

Contraints

  • 1 <= n <= 105
  • 0 <= ai < 232, i = 1, 2, ..., n
  • All ai s are integer
  • For each query, 1 <= l <= r <= n

Subtask

  • Testcase 1~3: n <= 1000
  • Testcase 4~6: No additional constraints

Input

The first line contains an integer n: the length of the sequence.

The second line contains n integers a1, a2, ..., an, each integer is seperated by a blank.

The third line contains an integer q: the number of queries you need to answer.

For the next q line, each line contains two integer l r, seperated by a blank.

Output

For each query, print the answer in one line.

 

Hint

For the first query in the sample input, the answer should be a1 * 1 + a2 * 2 + a3 * 3 = 1 * 1 + 1 * 2 + 4 * 3 = 15. For the second query, the answer should be a5 * 1 + a6 * 2 = 1 * 1 + 2 * 4 = 9.

Sample Input  Download

Sample Output  Download

Tags




Discuss