Fast Queries over subarrays

382 Views Asked by At

Problem: Given an sorted array of integers a[N], I have to process queries of kind as follow

  • [L R] p : find sum of aiCp for all i=L...R

Constraints:
N< 105
1<=ai<=106

Suppose there are Q such queries , Then please suggest a better method to solve this problem. Some points to note are
All queries are given in advance , i.e. offline algorithm can work .
Also note that the array is sorted.
each element in the array is bounded by small number. There are no updates to the array.

Thanks

PS: brute force approach is processing each query element by element which would give complexity: O(Q * N * (cost of n choose r) ) .

0

There are 0 best solutions below