fork(2) download
  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. namespace __gnu_pbds{
  7. typedef tree<int,
  8. null_type,
  9. less_equal<int>,
  10. rb_tree_tag,
  11. tree_order_statistics_node_update> ordered_set;
  12. }
  13. using namespace __gnu_pbds;
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20. void Insert(ordered_set &s,int x){ //this function inserts one more occurrence of (x) into the set.
  21.  
  22. s.insert(x);
  23.  
  24. }
  25.  
  26.  
  27. bool Exist(ordered_set &s,int x){ //this function checks weather the value (x) exists in the set or not.
  28.  
  29. if((s.upper_bound(x))==s.end()){
  30. return 0;
  31. }
  32. return ((*s.upper_bound(x))==x);
  33.  
  34. }
  35.  
  36.  
  37. void Erase(ordered_set &s,int x){ //this function erases one occurrence of the value (x).
  38.  
  39. if(Exist(s,x)){
  40. s.erase(s.upper_bound(x));
  41. }
  42.  
  43. }
  44.  
  45.  
  46. int FirstIdx(ordered_set &s,int x){ //this function returns the first index of the value (x)..(0 indexing).
  47.  
  48. if(!Exist(s,x)){
  49. return -1;
  50. }
  51. return (s.order_of_key(x));
  52.  
  53. }
  54.  
  55.  
  56. int Value(ordered_set &s,int idx){ //this function returns the value at the index (idx)..(0 indexing).
  57.  
  58. return (*s.find_by_order(idx));
  59.  
  60. }
  61.  
  62.  
  63. int LastIdx(ordered_set &s,int x){ //this function returns the last index of the value (x)..(0 indexing).
  64.  
  65. if(!Exist(s,x)){
  66. return -1;
  67. }
  68. if(Value(s,(int)s.size()-1)==x){
  69. return (int)(s.size())-1;
  70. }
  71. return FirstIdx(s,*s.lower_bound(x))-1;
  72.  
  73. }
  74.  
  75.  
  76. int Count(ordered_set &s,int x){ //this function returns the number of occurrences of the value (x).
  77.  
  78. if(!Exist(s,x)){
  79. return 0;
  80. }
  81. return LastIdx(s,x)-FirstIdx(s,x)+1;
  82.  
  83. }
  84.  
  85.  
  86. void Clear(ordered_set &s){ //this function clears all the elements from the set.
  87.  
  88. s.clear();
  89.  
  90. }
  91.  
  92.  
  93. int Size(ordered_set &s){ //this function returns the size of the set.
  94.  
  95. return (int)(s.size());
  96.  
  97. }
  98.  
  99.  
  100.  
  101. int main(){
  102.  
  103.  
  104. /*
  105.  
  106.   *Please read the above functions and their descriptions.
  107.  
  108.   *If you want to change your multi ordered set data type,you should change almost everything in the above functions.
  109.  
  110.   */
  111.  
  112.  
  113.  
  114. return 0;
  115.  
  116. }
  117.  
Success #stdin #stdout 0s 4888KB
stdin
Standard input is empty
stdout
Standard output is empty