Weird Range Query

4.1

10 votes
Math, , Prefix, Basic Math, Basic Programming, Dynamic Programming
Problem

Given an array, A, containing n integers, a1,a2,a3,...an, you have to answer q queries.

For each query you will be given a one-indexed range [l,r], lr, and your answer to each query should be: ri=l(ri+1).a[i]

For example, if A=[6,10,9,11,12], for l=2, and r=5:

(52+1)10+(53+1)9+(54+1)11+(55+1)12=

410+39+211+112=47

NOTE: The answer to a query may not fit a 32-bit integer type. 

Input format

  • The first line contains two integers n, and q - denoting the number of integers in A, and the number of queries respectively. 
  • The next line contains n integers, ai - denoting the values of the integers in A
  • The ith line among next q lines each contain two integers, li, and ri - denoting the indices to be queried for the ith query.

Output format
Your program should output q lines, and the ithof these lines should be the answer to the ith query. 

 Constraints

1n,q105107Ai1071lirin

 

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

A in the sample is [6,10,9,11,12], and there are 3 queries: 

The first query l=2 and r=5 is explained in the sample.

In the second query l=3 and r=4, it can be evaluated as (43+1)9+(44+1)11=29+111=7.

In the third query l=3 and r=3, it can be evalued as (33+1)9=19=9.

Editor Image

?