Open In App

Ordered Set and GNU C++ PBDS

Prerequisite :Basic knowledge of STL and Sets Data structure.

About ordered set



Ordered set is a policy based data structure in g++ that keeps the unique elements in sorted order. It performs all the operations as performed by the set data structure in STL in log(n) complexity and performs two additional operations also in log(n) complexity .

Required header files to implement ordered set and their description



For implementing ordered_set and GNU C++ library contains other Policy based data structures we need to include :

The first one is used to include the associative containers or group of templates such as set, multimap, map etc.The tree-based data structures which we will be using below is present in this header file.
The second header file is used to include the tree_order_statistics_node update which is explained below:

using namespace __gnu_pbds;

It is a namespace necessary for the GNU based Policy based data structures.

The tree based container has a concrete structure but the necessary structure required for the ordered set implementation is :

tree < int ,  null_type ,  less ,  rb_tree_tag ,  tree_order_statistics_node_update >
  1. int : It is the type of the data that we want to insert (KEY).It can be integer, float or pair of int etc.
  2. null_type : It is the mapped policy. It is null here to use it as a set.If we want to get map but not the set, as the second argument type must be used mapped type.
  3. less : It is the basis for comparison of two functions.
  4. rb_tree_tag : type of tree used. It is generally Red black trees because it takes log(n) time for insertion and deletion while other take linear time such as splay_tree.
  5. tree_order_statistics_node__update : It is included in tree_policy.hpp and contains various operations for updating the node variants of a tree-based container, so we can keep track of metadata like the number of nodes in a subtree

Additional functions in the ordered set other than the set

Along with the previous operations of the set, it supports two main important operations

Difference between set and ordered set
There is not so much difference between the set and ordered set although ordered set can be assumed as an extended version of set capable of performing some more advanced functions(stated above) that are extensively used in competitive programming.


NOTE : ordered_set is used here as a macro given to tree<int, null_type, less, rb_tree_tag, tree_order_statistics_node_update>. Therefore it can be given any name as macro other than ordered_set but generally in the world of competitive programming it is commonly referred as ordered set as it is a set with additional operations.

Practical applications:
Suppose we have a situation where the elements are inserted one by one in an array and after each insertion, we are given a range [l, r] and we have to determine the number of elements in the array greater than equal to l and less than equal to r. Initially, the array is empty.
Examples:

Input :    5
           1 2
           1
           2 5
           2
           1 5

Output :   0
           1
           3

Explanation:

Input :     1
            1 2
            2
            3 5
            5
            1 4
Output :    1
            0
            2
2
2
2
5
1

Thus we can now solve the above problem easily i.e. count of elements between l and r can be found by:
o_set.order_of_key(r+1) – o_set.order_of_key(l)

NOTE : As the set contains only the UNIQUE elements, so to perform the operations on an array having repeated elements we can take the KEY as a pair of elements instead of integer in which the first element is our required element of the array and only the second element of the pair must be unique so that the whole pair is unique.

For more details refer to :
https://gcc.gnu.org/onlinedocs/libstdc++/manual/policy_data_structures.html


Article Tags :