__Handle__'s blog

By __Handle__, history, 5 years ago, translation, In English

Given array A and Q queries. Each query is l, r, x for every query you need to calc number of elements that less than x.

  • Vote: I like it
  • +6
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

I think you can create for example fenwick tree or segment tree of order statistic trees and then sum up orders of x in all order statistic trees in [l, r].

EDIT: Or simply a merge sort tree.

»
5 years ago, # |
  Vote: I like it +3 Vote: I do not like it

You can solve offline the queries. 1) sort the queries in nondecreasing order regarding x; 2) build an array V with 2 parameters: the value of A[i] and i, the position, then sort V regarding first value; 3) after these operations, keep 2 pointers: you will start i from the first value from V and start with j from the first element in Q (sorted); update the values in the binary indexed tree unless V[i].first_parameter will be >= the x from actual j query, then calculate with BIT the answer like this: sum of first r values in BIT — sum of first l-1 values in BIT, then print this value.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

For offline:Sort the queries by x,then for each query,add the elements(weren't added before)less than x into the i-th position of a BIT.Then check the sum in interval l and r.Complexity is .

For online:Discrete the array first.Then build a persistent segment tree.The i-th version contains the first i elements in the array.The difference between i and i-1 is the a[i]-th position is increased by 1.For a query,the answer is:the sum in [l,r] in the r-th tree minus the sum in [l,r] in the l-1-th tree.The Complexity is also .

  • »
    »
    5 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Some mistakes here...

    For a query,the answer is:the sum in [l,r] in the r-th tree minus the sum in [l,r] in the l-1-th tree

    it should be:

    For a query,the answer is:the sum in [1,y] in the r-th tree minus the sum in [1,y] in the l-1-th tree(y is the number of elements strictly less than x in the whole array)