Module: Root decomposition


Problem

1 /6


Sum on segment - 1

Theory Click to read/hide

We have a problem about how to quickly calculate the sums on the interval l...r in a static array a in less than O(n) asymptotics.
Let's divide the array a into k blocks of the same size and first calculate the sum of elements for each of them.
Now, when answering the request, we can go through the elements of the array a and add them to the result, also if one of the blocks lies inside the segment, then we can add its sum to the result and skip the elements of this block.
The maximum number of operations per query with this algorithm is n / k + k, so the optimal k is equal to the square root of n.
 

Problem

Given an array a of length n (\(1 <= n <= 2 \cdot 10^6\)< /span>, \(1 <= a_i <= 10^9\)). Also given m (\(1 <= m <= 500\)) queries like l, r (\(1 <= l <= r <= n\)).

For each query, print the sum of numbers between l and r inclusive. Elements are numbered starting with 1 to n.

 

Examples
# Input Output
1
4
1 2 3 4
2
1 4
1 1
10
1
2
5
5 5 5 5 5
1
5 5
5