What data structure to find number of elements in a given range in O(log n) time?

Anoop

I'm solving a problem and I realized I am need of a data structure with following properties but cant find one even after few hours of googling. I believe STL library is too rich to not have this one hence asking here.

  1. Insert any element(should be able to contain repetetive ones) in O(log(n)) time
  2. Remove an element in O(log(n)) time as well.
  3. If i want to query for the number of elemenes in range [a,b], I should get that count in O(log(n)) time..

If I were to write it from scratch, for part 1 and 2, I would use a set or multiset and I would modify their find() method(which runs in O(log(N)) time) to return indices instead of iterators so that I can do abs(find(a)-find(b)) so I get the count of elements in log(N) time. But unfortunately for me, find() returns and iterator.

I have looked into multiset() and I could not accomplish requirement 3 in O(log(n)) time. It takes O(n).

Any hints to get it done easily?

behzad.nouri

Though not part of STL, you may use Policy-Based Data Structures which are part of gcc extensions; In particular you may initialize an order statistics tree as below. The code compiles with gcc without any external libraries:

#include<iostream>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

int main()
{
    tree<int,         /* key                */
         null_type,   /* mapped             */
         less<int>,   /* compare function   */
         rb_tree_tag, /* red-black tree tag */
         tree_order_statistics_node_update> tr;

    for(int i = 0; i < 20; ++i)
        tr.insert(i);

    /* number of elements in the range [3, 10) */
    cout << tr.order_of_key(10) - tr.order_of_key(3);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to find a range of given number?

From Dev

Data structure to check if number of elements is equal in O(1) time?

From Dev

Find elements that sum to a given number

From Dev

time complexity of this given data structure

From Dev

time complexity of this given data structure

From Dev

XSLT - Find number range in elements

From Dev

Finding number of elements within a given range in an array

From Dev

Fastest way to find number of elements in a range

From Dev

Find number of elements in range from map object

From Dev

how to find number that's divisible by a number in a given range?

From Java

Data structure to check if a static array does not contain an element of a given range

From Dev

Find the largest positive number in my given data?

From Dev

Efficient algo to find number of integers in a sorted array that are within a certain range in O(log(N)) time?

From Dev

MySQL - Find number of days in date range which fall in a given month

From Dev

How to find number of integers in a given range has even set bits

From Dev

How to find missing time value(s) in a given range in MySQL database?

From Dev

Get the smallest difference between elements in a data structure in less than O(n) time

From Dev

Get the smallest difference between elements in a data structure in less than O(n) time

From Dev

Scale a number by given range

From Dev

find optimal sum of elements in list of numbers that are > a given number

From Dev

Find number of elements smaller than a given element in BST

From Dev

Best Data structure to support range Sum and consecutive elements swap operation?

From Dev

Best Data structure to support range Sum and consecutive elements swap operation?

From Dev

SQL: find records having data for each month in a given date range

From Dev

SQL: find records having data for each month in a given date range

From Dev

What data structure can achieve random pop and push in better than O(n) time?

From Dev

Find the combinations, given n number of box with x number of balls

From Dev

Wrong number of elements in structure

From Dev

Given n-1*n array, find missing number

Related Related

  1. 1

    How to find a range of given number?

  2. 2

    Data structure to check if number of elements is equal in O(1) time?

  3. 3

    Find elements that sum to a given number

  4. 4

    time complexity of this given data structure

  5. 5

    time complexity of this given data structure

  6. 6

    XSLT - Find number range in elements

  7. 7

    Finding number of elements within a given range in an array

  8. 8

    Fastest way to find number of elements in a range

  9. 9

    Find number of elements in range from map object

  10. 10

    how to find number that's divisible by a number in a given range?

  11. 11

    Data structure to check if a static array does not contain an element of a given range

  12. 12

    Find the largest positive number in my given data?

  13. 13

    Efficient algo to find number of integers in a sorted array that are within a certain range in O(log(N)) time?

  14. 14

    MySQL - Find number of days in date range which fall in a given month

  15. 15

    How to find number of integers in a given range has even set bits

  16. 16

    How to find missing time value(s) in a given range in MySQL database?

  17. 17

    Get the smallest difference between elements in a data structure in less than O(n) time

  18. 18

    Get the smallest difference between elements in a data structure in less than O(n) time

  19. 19

    Scale a number by given range

  20. 20

    find optimal sum of elements in list of numbers that are > a given number

  21. 21

    Find number of elements smaller than a given element in BST

  22. 22

    Best Data structure to support range Sum and consecutive elements swap operation?

  23. 23

    Best Data structure to support range Sum and consecutive elements swap operation?

  24. 24

    SQL: find records having data for each month in a given date range

  25. 25

    SQL: find records having data for each month in a given date range

  26. 26

    What data structure can achieve random pop and push in better than O(n) time?

  27. 27

    Find the combinations, given n number of box with x number of balls

  28. 28

    Wrong number of elements in structure

  29. 29

    Given n-1*n array, find missing number

HotTag

Archive