PAIRSUM - Sum of Pairwise Products

Given N non negative numbers, the task is to answer M queries.

Each query is as follows: 

Given u,v you need to find the pairwise product sum (u and v are zero indexed)

auau + au+1au+1 + au+1au + au+2au+2 + au+2au+1 + au+2au + ... + avav + avav-1 + ... + avau

Input

N
a0 a1 ... aN-1
M
u1 v1
u2 v2
...
uM vM

Output

Print the answer for each query in a separate line.

Example

Input:
5
2 0 1 3 3
3
0 2
1 2
3 4

Output:
7
1
27

Constraints

0 <= u <= v < N
N <= 100000
M <= 100000
0 <= ai <= 1000000


Added by:Manukranth
Date:2011-09-20
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64

hide comments
2014-06-02 16:50:39 agaurav77
AC, pay heed to Haidar Abboud's comment.
2014-05-30 12:22:27 GURVINDER SINGH
Easy one AC in first attempt :)
2014-05-18 16:03:04 P_Quantum
gud one!!
2014-03-29 20:51:09 Shobhit Trehan
Fast I/O and no vectors :/
2014-03-27 23:42:08 RAHUL RANJAN
Brains are Awessum

Last edit: 2014-03-28 13:34:11
2014-03-21 19:09:51 zicowa
nice problem look at the comment of Haider Abboud you will get the solution soon i also get it from there
2013-12-29 21:54:00 SanchitK
@Manukranth- i have used O(M*n) algo but still it says TLE. pls tell isnt O(M*N) enough?
2013-07-08 12:01:23 BLANKRK
gud one.......
2013-06-20 17:50:01 sumit agarwal
phew!!...data types costed me 4 TLE's...long long can handle the values...
2013-03-15 14:28:19 Haidar Abboud
- MAKE SURE you understand what you are asked to compute.
- Each query should be answered in O(1) , resulting in a total time complexity of O(N + Q).
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.