PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000102. Truy vấn tổng tích lũy (Cumulative Sum Query)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Truy vấn tổng tích lũy (Cumulative Sum Query)

Cumulative Sum Query

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

William Macfarlane has an array of nn integers a0,…,an−1a_0,\ldots,a_{n-1} indexed from zero. For each pair of indices i,ji,j, find the sum of all elements between those positions, including both endpoints. The two endpoints may be given in either order.

Input

The first line contains integer nn.

The second line contains the nn array elements.

The third line contains integer qq, the number of queries.

Each of the next qq lines contains two indices i,ji,j.

Output

Print qq lines, one sum per query in input order.

Subtasks

  • Subtask 1 (20 points): 1≤n≤10001\le n\le 1000; 1≤q≤10001\le q\le 1000; −2147483648≤ai≤2147483647-2147483648\le a_i\le 2147483647 for all 0≤i<n0\le i<n; 0≤i,j<n0\le i,j<n.
  • Subtask 2 (30 points): 1≤n≤1000001\le n\le 100000; 1≤q≤100001\le q\le 10000; −2147483648≤ai≤2147483647-2147483648\le a_i\le 2147483647 for all 0≤i<n0\le i<n; 0≤i,j<n0\le i,j<n; ∣i−j∣+1≤10|i-j|+1\le 10.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤q≤100001\le q\le 10000; −2147483648≤ai≤2147483647-2147483648\le a_i\le 2147483647 for all 0≤i<n0\le i<n; 0≤i,j<n0\le i,j<n.

Examples

Input

3
1 4 1
3
1 1
1 2
0 2

Output

4
5
6

Explanation

The indices are 0,1,20,1,2. The interval [1,1][1,1] contains 4; [1,2][1,2] contains 4 and 1, totaling 5; [0,2][0,2] contains all three elements, totaling 1+4+1=61+4+1=6.