1

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

Marco A.
  • 43,032
  • 26
  • 132
  • 246
v78
  • 2,803
  • 21
  • 44
  • 2
    What is your question? It currently seems like you want us to do your work, which stackoverflow isn't for. – AliciaBytes Jun 06 '15 at 16:23
  • No, I just want to know a fast way to process such a large number of queries. I don't want any code , I just want ideas and links. – v78 Jun 07 '15 at 01:09
  • a_i C p is Choose(a_i,p)? – amit Jun 10 '15 at 07:02
  • Yes, thanks for clarifying – v78 Jun 10 '15 at 07:10
  • 1
    Try splitting the problem: compute _c_i = Choose(a_i, p)_; support queries about _sum c_L … c_R_. – greybeard Jun 10 '15 at 19:15
  • @greybeard but, p is not fixed for all the queries. – v78 Jun 11 '15 at 01:13
  • Any constraints for `p`? – Pham Trung Jun 11 '15 at 02:49
  • @PhamTrung, p is ultimately bounded by ai, which is less than 1 million. – v78 Jun 11 '15 at 04:17
  • @cc2, this is an obfuscated version of [Chef and String](http://www.codechef.com/JUNE15/problems/CHSTR), isn't it? – dened Jun 11 '15 at 20:03
  • If this is `Chef ans String`, the problem is much easier. – Pham Trung Jun 12 '15 at 02:30
  • 1
    @PhamTrung, may be, but we should not help cc2 to solve the problem before [the contest](http://www.codechef.com/JUNE15/) is finished (three days left). – dened Jun 12 '15 at 05:14
  • @dened, @PhamTrung: I don't see the problems to be closely related, thanks for keeping an eye out, anyway. Precomputing f_0 = 1, f_i = a_i!/a_i-1! seems an idea (even being sorted would help). Do the `p`s have to be no greater than a_L (, is the result well-defined otherwise)? – greybeard Jun 12 '15 at 05:33
  • @greybeard let's wait for OP come back and confirm, the point is for `C`, we need to divide each term by `(ai - p)!`, so pre-calculated table may not work in this case, and, this problem may not have a solution. However, if this is `Chef and String`, it is easier, I solved that problem yesterday. – Pham Trung Jun 12 '15 at 05:44
  • @greybeard, just notice that every _ai_ is the number of occurrences of the corresponding substring _si_ in _S_ (_S_ is from [Chef and String](http://www.codechef.com/JUNE15/problems/CHSTR)). And _p_ of _i_-th query is _Ki_ from Chef and String. – dened Jun 12 '15 at 06:24
  • @dened: so, how does Choose(a_i, p) relate to sum over Choose(a_i, p) for L <= i <= R? – greybeard Jun 12 '15 at 06:43
  • @greybeard , for a query `q`, so we need to return all ways to choose `q` from q, q + 1, ... n equivalent substrings, so that make sense. – Pham Trung Jun 12 '15 at 06:51
  • @greybeard, sum over Choose(ai, p) for 1 <= _i_ <= _N_ is the answer to Chef and String. – dened Jun 12 '15 at 06:52
  • @cc2 can you clarify this? is this problem `Chef and String`? can you post a link to the original problem if possible? – Pham Trung Jun 18 '15 at 07:26
  • link to Chef and String is posted above comments by @dened . – v78 Jun 18 '15 at 07:47

0 Answers0