atlasworld's blog

By atlasworld, history, 5 years ago, In English

You , are given an array of n elements and q queries . Both n and q can range upto 10^5 , and 1<=a[i] <= 10^9.

how to find / count all the elements less than a given element in each query .

queries are in the form of l , r , x where x is number and l and r are starting and ending indices.

Example: index 1 based

a[] = {5 , 3 , 2 , 2 , 3 , 1 , 2 , 9 , 10 , 2 }

q1 = 5 9 5

ans = 3 { 3 , 1 , 2}

q2 = 1 4 2

ans = 2 {2 , 2 }

The problem gives a feel of segment tree , but how to solve it using ST ?

any idea.

  • Vote: I like it
  • -19
  • Vote: I do not like it

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

I don't know about segment tree, but it can be solved with SQRT decomposition and Mo's algorithm.

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

    mo's algorithm ? how . can u explain . despotovski01

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

      If you are not familiar with Mo's algorithm, there's a good tutorial on HackerEarth about it. Do coordinate compression on all possible values found in array or queries, so that the range of the values becomes [0, 2 * 105). Then, keep count array for the values and a decomposed version of the array, which has blocks. Those arrays will keep track of the count of values for your queries. To calculate the total value of items less than x, you'll need just steps, because there are at most blocks, and at most values to be included that are not in a block. Total complexity is

      Edit: now I noticed that you asked how to find those elements as well. Just modify the previous algorithm to store the elements in each block in some data structure that supports efficient add / remove operations. A linked list or a hash table would be a good choice, because linked lists support addition and deletion of elements to the head and tail in O(1).

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

You can solve each query in (logN)^2 using merge sort tree.

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

    what is merge sort tree , i dont know .

    • »
      »
      »
      5 years ago, # ^ |
      Rev. 3   Vote: I like it +1 Vote: I do not like it

      in segment tree you have to keep a vector as a node element and each node of segment tree will have the sorted range of left and right sub tree. then you will have to query in L to R for that when you reach a segment that completely lies in the range you have to do a binary search for number of element less than x in that segment and like this you have to add the answers coming from all segments

      you can have a look to this link : https://discuss.codechef.com/questions/94448/merge-sort-tree-tutorial

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

this can be done with a wavelet-tree

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

i got this blog . can anyone tell why we need to sort the array and querie. why sorting will not affect our answer

https://www.geeksforgeeks.org/number-elements-less-equal-given-number-given-subarray/

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

    When you sort the array you also maintain the indices of the values. The BIT at each index basically stores whether that element is "active" in the array (0 or 1). Then to process the next query l, r, x, you "activate" every unactivated i such that ai < x by performing a point increment at every such i. To do so you just iterate through the sorted array. The answer to the query is then simply the range sum on the BIT over [l, r].

    I think you're misunderstanding why the sorting is done. The sorting is necessary to perform the right updates for the next query. What makes you think it would affect the answer in any way?

    Also note that this solution is offline unlike some of the suggestions you got.

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

Mo's algorithm + Fenwick tree

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

There exist a version of this problem with update query you can find it in Vietnamese-SPOJ It can be done using sqrt-decomposition + 2d BIT. The idea is to divide the sequence in to sqrt(N) blocks, and you maintain the numbers of value which is greater than a specific value by a BIT. You can find my implementation for it here

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

I see that same question asked every week... You can always use google to find a solution for such standard problems.

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

I'm not sure if it's possible to solve this with a Segment Tree. I know for a fact that you can solve it by compressing the coordinates and using a Persistent Segment Tree, however, it seems like an overkill to me. I'd use a Wavelet Tree to solve this problem.

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

you can solve it using segment tree + coordinate compression.

first we will make coordinate compression because values of a[i] is large (10 ^ 9) , so we will have new idx for every number.

after that we will build the segment tree where tree[node] = tree[node * 2] + tree[node * 2 + 1]

after that we will make query as it's sum problem that will return number of elements <= new idx of current element