Advertisement
Guest User

Untitled

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