PAIRSUM - Sum of Pairwise Products

no tags 

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


hide comments
agaurav77: 2014-06-02 16:50:39

AC, pay heed to Haidar Abboud's comment.

GURVINDER SINGH: 2014-05-30 12:22:27

Easy one AC in first attempt :)

P_Quantum: 2014-05-18 16:03:04

gud one!!

Shobhit Trehan: 2014-03-29 20:51:09

Fast I/O and no vectors :/

RAHUL RANJAN: 2014-03-27 23:42:08

Brains are Awessum

Last edit: 2014-03-28 13:34:11
zicowa: 2014-03-21 19:09:51

nice problem look at the comment of Haider Abboud you will get the solution soon i also get it from there

SanchitK: 2013-12-29 21:54:00

@Manukranth- i have used O(M*n) algo but still it says TLE. pls tell isnt O(M*N) enough?

BLANKRK: 2013-07-08 12:01:23

gud one.......

sumit agarwal: 2013-06-20 17:50:01

phew!!...data types costed me 4 TLE's...long long can handle the values...

Haidar Abboud: 2013-03-15 14:28:19

- 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).


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