fork download
  1. /// Implicit Treap - muoii
  2.  
  3. class trarr{
  4. public:
  5.  
  6. typedef struct item{
  7. int val,prior,cnt;
  8.  
  9. item *l,*r;
  10.  
  11. item(const int &v): val(v), l(nullptr), r(nullptr), cnt(1) { prior = (rand() << 16) | rand(); }
  12.  
  13. void push() {
  14. }
  15.  
  16. void recalc() {
  17. cnt = 1 + count(l) + count(r);
  18. }
  19.  
  20. static int count(item* ptr) {
  21. return ptr?ptr->cnt:0;
  22. }
  23. } *pitem;
  24.  
  25. pitem merge(pitem l, pitem r)
  26. {
  27. if(!l || !r) return l?l:r;
  28.  
  29. if (l->prior < r->prior)
  30. {
  31. l->push();
  32. l->r = merge(l->r, r);
  33. l->recalc();
  34. return l;
  35. }
  36. else
  37. {
  38. r->push();
  39. r->l = merge(l, r->l);
  40. r->recalc();
  41. return r;
  42. }
  43. }
  44.  
  45. void split(pitem node, int idx, pitem& l, pitem& r)
  46. {
  47. l = r = nullptr;
  48. if (!node) return;
  49.  
  50. node->push();
  51.  
  52. if (count(node->l) < idx)
  53. {
  54. l = node;
  55. split(l->r, idx - count(node->l) - 1, l->r, r);
  56. l->recalc();
  57. }
  58. else
  59. {
  60. r = node;
  61. split(r->l, idx, l, r->l);
  62. r->recalc();
  63. }
  64. }
  65.  
  66. public:
  67.  
  68. static int count(pitem ptr) { return ptr?ptr->cnt:0;}
  69. pitem root=0;
  70. size_t siz=0;
  71.  
  72. size_t size() const{
  73. return siz;
  74. }
  75.  
  76. void insert(const int &idx,const int &val)
  77. {
  78. pitem l=0,r=0;
  79.  
  80. split(root, idx, l, r);
  81.  
  82. pitem cur = new item(val);
  83.  
  84. root = merge(merge(l,cur),r);
  85.  
  86. ++siz;
  87. }
  88.  
  89. void erase(const int &idx)
  90. {
  91. pitem l=0,m=0,r=0;
  92.  
  93. split(root, idx, l, r);
  94. split(r, 1, m, r);
  95.  
  96. root = merge(l, r);
  97.  
  98. --siz;
  99. }
  100.  
  101. void replace(const int &idx,const int &val)
  102. {
  103. erase(idx);
  104. insert(idx,val);
  105. }
  106.  
  107. void push_front(const int &val) {
  108. return insert(0,val);
  109. }
  110. void push_back(const int &val) {
  111. return insert(siz,val);
  112. }
  113. void pop_front()
  114. {
  115. erase(0);
  116. --siz;
  117. }
  118. void pop_back()
  119. {
  120. erase(--siz);
  121. }
  122.  
  123. pitem operator [](const int &idx)
  124. {
  125. pitem l=0,m=0,r=0;
  126.  
  127. split(root,idx,l,r);
  128. split(r,1,m,r);
  129.  
  130. if (!m) throw runtime_error("IndexitemOutOfBound");
  131.  
  132. root = merge(merge(l, m), r);
  133. return m;
  134. }
  135.  
  136. /// query:
  137. /// sum,max/min
  138. };
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:70:5: error: ‘size_t’ does not name a type
     size_t siz=0;
     ^~~~~~
prog.cpp:72:2: error: ‘size_t’ does not name a type
  size_t size() const{
  ^~~~~~
prog.cpp: In constructor ‘trarr::item::item(const int&)’:
prog.cpp:11:84: error: ‘rand’ was not declared in this scope
         item(const int &v): val(v), l(nullptr), r(nullptr), cnt(1) { prior = (rand() << 16) | rand(); }
                                                                                    ^
prog.cpp: In member function ‘void trarr::insert(const int&, const int&)’:
prog.cpp:86:5: error: ‘siz’ was not declared in this scope
   ++siz;
     ^~~
prog.cpp: In member function ‘void trarr::erase(const int&)’:
prog.cpp:98:5: error: ‘siz’ was not declared in this scope
   --siz;
     ^~~
prog.cpp: In member function ‘void trarr::push_back(const int&)’:
prog.cpp:111:17: error: ‘siz’ was not declared in this scope
   return insert(siz,val);
                 ^~~
prog.cpp:111:24: error: return-statement with a value, in function returning 'void' [-fpermissive]
   return insert(siz,val);
                        ^
prog.cpp: In member function ‘void trarr::pop_front()’:
prog.cpp:116:8: error: ‘siz’ was not declared in this scope
      --siz;
        ^~~
prog.cpp: In member function ‘void trarr::pop_back()’:
prog.cpp:120:14: error: ‘siz’ was not declared in this scope
      erase(--siz);
              ^~~
prog.cpp: In member function ‘trarr::item* trarr::operator[](const int&)’:
prog.cpp:130:52: error: ‘runtime_error’ was not declared in this scope
   if (!m) throw runtime_error("IndexitemOutOfBound");
                                                    ^
stdout
Standard output is empty