Advertisement
Guest User

Untitled

a guest
May 20th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <math.h>
  4. #include <stack>
  5. #include <vector>
  6. #include <memory>
  7.  
  8. using namespace std;
  9. template<typename t>
  10. class Treap {
  11. private:
  12. struct Node{
  13. int cnt;
  14. int prior;
  15. t value;
  16.  
  17. shared_ptr<Node> left;
  18. shared_ptr<Node> right;
  19.  
  20. Node (): cnt(-1), prior(-1), left(nullptr), right(nullptr) { }
  21. Node ( int cnt, int prior ) : cnt(cnt), prior(prior), left(nullptr), right(nullptr) { }
  22. };
  23. shared_ptr<Node> root;
  24. shared_ptr<Node> new_node(t val){
  25. shared_ptr<Node> res(new Node);
  26. res->prior = rand();
  27. res->cnt = 1;
  28. res->value = val;
  29. res->left = res->right = nullptr;
  30. return res;
  31. }
  32.  
  33. void merge (Node* &node, Node* l, Node* r);
  34. void split ( Node* node, int cnt, Node* & left, Node* & right );
  35. void delete_tree ( Node* node );
  36.  
  37. int cnt (Node* node) {
  38. return node ? node->cnt : 0;
  39. }
  40. void update_cnt (Node* node) {
  41. if (node)
  42. node->cnt = cnt(node->left) + cnt(node->right) + 1;
  43. }
  44.  
  45. public:
  46. Treap ( );
  47. ~Treap ( );
  48. void add ( int pos, t val );
  49. void remove ( int pos );
  50. t get_value (Node* node, int pos );
  51.  
  52. };
  53.  
  54. template<typename t>
  55. Treap<t> :: Treap ( ) {
  56. root = nullptr;
  57. }
  58.  
  59. template<typename t>
  60. void Treap<t> :: merge (Node* &node, Node* t1, Node* t2) {
  61. if (t1 == nullptr)
  62. node = t2;
  63. if (t2 == nullptr)
  64. node = t1;
  65. if (t1->prior > t2->prior) {
  66. merge(t1->right, t1->right, t2);
  67. update_cnt(t1);
  68. node = t1;
  69. }
  70. else {
  71. merge(t2->left, t1, t2->left);
  72. update_cnt(t2);
  73. node = t2;
  74. }
  75. }
  76.  
  77. template<typename t>
  78. void Treap<t> :: split ( Node* node, int key, Node *&t1, Node *&t2 ) {
  79. if (node == nullptr) {
  80. t1 = t2 = nullptr;
  81. return;
  82. }
  83.  
  84. if (cnt(node->left) < key){
  85. split(node->right, key - cnt(node->left) - 1, node->right, t2);
  86. t1 = node;
  87. }
  88. else {
  89. split(node->left, key, t1, node->left);
  90. t2 = node;
  91. }
  92. update_cnt(node);
  93. }
  94.  
  95. template<typename t>
  96. void Treap<t> :: add ( int pos, t val ) {
  97. Node *t1, *t2, *t3;
  98. split(root, pos, t1, t2);
  99. Node* new_tree = new_node(val);
  100. merge(t3, t1, new_tree);
  101. merge(root, t3, t2);
  102. }
  103.  
  104. template<typename t>
  105. void Treap<t> ::remove ( int pos ){
  106. Node *t1, *t2, *t3, *t4;
  107. split(root, pos, t1, t2);
  108. split(t2, pos + 1, t3, t4);
  109. merge(root, t1, t4);
  110. delete t3;
  111. }
  112.  
  113. template<typename t>
  114. t Treap<t> :: get_value (Node* node, int pos ){
  115. if ( !node )
  116. node = root;
  117.  
  118. int my_idx = cnt ( node->left );
  119. if ( pos < my_idx )
  120. return get_value ( node->left, pos );
  121. else if ( pos == my_idx )
  122. return node->value;
  123. else
  124. return get_value(node->right, pos - my_idx - 1);
  125. }
  126. class ArrayString {
  127. public:
  128. ArrayString () { }
  129. void InsertAt( int position, const string& value );
  130. void DeleteAt( int position );
  131. string GetAt( int position );
  132. private:
  133. Treap<string> tree;
  134. };
  135.  
  136. void ArrayString :: InsertAt( int position, const string& value ){
  137. tree.add(position, value);
  138. }
  139. void ArrayString:: DeleteAt( int position ){
  140. tree.remove(position);
  141. }
  142. string ArrayString:: GetAt( int position ){
  143. return tree.get_value(nullptr, position);
  144. }
  145. int main(){
  146. ArrayString ars;
  147. int k = 0;
  148. cin >> k;
  149. for(int i = 0; i < k; ++i){
  150. string s;
  151. cin >> s;
  152. if(s[0] == '+'){
  153.  
  154. int n = 0;
  155. int j = 2;
  156. string val;
  157.  
  158. while(s[j] != ' '){
  159. n += int(s[j]) * pow(10, j - 2);
  160. ++j;
  161. }
  162. for(; j < s.size(); ++j)
  163. val.push_back(s[j]);
  164.  
  165. ars.InsertAt(n, val);
  166. }
  167. else{
  168. int n = 0, j = 2;
  169. while ( j < s.size() ){
  170. n += (s[j] - 64) * pow(10, j);
  171. ++j;
  172. }
  173. if(s[0] == '-')
  174. ars.DeleteAt(n);
  175. else
  176. ars.GetAt(n);
  177. }
  178. }
  179. return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement