Блог пользователя atlasworld

Автор atlasworld, история, 6 лет назад, По-английски

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.

  • Проголосовать: нравится
  • -19
  • Проголосовать: не нравится

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

  • »
    »
    6 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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

    • »
      »
      »
      6 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится -9 Проголосовать: не нравится

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

»
6 лет назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

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

»
6 лет назад, # |
  Проголосовать: нравится +13 Проголосовать: не нравится

this can be done with a wavelet-tree

»
6 лет назад, # |
Rev. 3   Проголосовать: нравится +3 Проголосовать: не нравится

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/

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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.

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Mo's algorithm + Fenwick tree

»
6 лет назад, # |
Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

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

»
6 лет назад, # |
  Проголосовать: нравится +20 Проголосовать: не нравится

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

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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.

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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