Advertisement
Emiliatan

pb_ds tree

Jul 2nd, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/tree_policy.hpp>
  3. #include <ext/pb_ds/assoc_container.hpp>
  4.  
  5. using namespace std;
  6. using namespace __gnu_pbds;
  7.  
  8. typedef __gnu_pbds::tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> rbt;
  9.  
  10.  
  11. int main()
  12. {
  13.     rbt T;
  14.  
  15.  
  16.     //insert()
  17.     T.insert(9);
  18.     T.insert(1);
  19.     T.insert(3);
  20.     T.insert(3);
  21.     T.insert(5);
  22.     T.insert(10);
  23.  
  24.     printf("T:\n");
  25.     for(auto x : T)
  26.         printf("%d ", x); // 1 3 5 9 10
  27.     puts("\n");
  28.  
  29.  
  30.     //erase()
  31.     T.erase(1);
  32.  
  33.     printf("T:\n");
  34.     for(auto x : T)
  35.         printf("%d ", x); // 3 5 9 10
  36.     puts("\n");
  37.  
  38.  
  39.     //find()
  40.     auto a = T.find(10);
  41.     auto b = T.find(11);
  42.  
  43.     puts((a == T.end() ? "Not Find 10" : "Find 10")); // Find 10
  44.     puts((b == T.end() ? "Not Find 11" : "Find 11")); // Not Find 11
  45.     puts("");
  46.  
  47.  
  48.     //lower_bound(), upper_bound()
  49.     a = T.lower_bound(5);
  50.     b = T.upper_bound(5);
  51.  
  52.     printf("a = %d\n", *a); // 5
  53.     printf("b = %d\n", *b); // 9
  54.     puts("");
  55.  
  56.  
  57.     //find_by_order()
  58.     auto x = T.find_by_order(0);
  59.     auto y = T.find_by_order(1);
  60.     auto z = T.find_by_order(5);
  61.  
  62.     if(x == T.end())            // 3
  63.         printf("not find\n");
  64.     else
  65.         printf("%d\n", *x);
  66.  
  67.     if(y == T.end())            // 5
  68.         printf("not find\n");
  69.     else
  70.         printf("%d\n", *y);
  71.  
  72.     if(z == T.end())            // not find
  73.         printf("not find\n");
  74.     else
  75.         printf("%d\n", *z);
  76.     puts("");
  77.  
  78.  
  79.     //order_of_key()
  80.     printf("%d\n", T.order_of_key(3));  // 0
  81.     printf("%d\n", T.order_of_key(5));  // 1
  82.     printf("%d\n", T.order_of_key(11)); // 4
  83.     puts("");
  84.  
  85.  
  86.     //traverse
  87.     for(auto i = T.begin(); i != T.end(); ++i) // 3 5 9 10
  88.         printf("%d ", *i);
  89.     puts("\n");
  90.  
  91.     //clear()
  92.     T.clear();
  93.  
  94.  
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement