Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. #include <iomanip>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5.  
  6. class ImplicitTreap {
  7. public:
  8. ImplicitTreap();
  9. ~ImplicitTreap();
  10. void insert(size_t position, const int& value);
  11. void remove(size_t position);
  12. void clear();
  13. int get(size_t position) const;
  14.  
  15. private:
  16. struct Node {
  17. Node(const int& value);
  18.  
  19. // Fixes treeSize
  20. void fix();
  21.  
  22. // Stores size of subtree
  23. size_t treeSize;
  24. // Priority of current node
  25. int priority;
  26. // Value to store
  27. int value;
  28.  
  29. // Pointers tp left and right children
  30. Node* left;
  31. Node* right;
  32. };
  33.  
  34. private:
  35. std::pair<Node*, Node*> split(Node* node, size_t position);
  36. Node* merge(Node* node1, Node* node2);
  37. void clear(Node* node);
  38.  
  39. private:
  40. Node* _root;
  41. };
  42.  
  43.  
  44.  
  45. ImplicitTreap::ImplicitTreap() : _root(nullptr) {}
  46.  
  47. ImplicitTreap::~ImplicitTreap() { clear(); }
  48.  
  49. void ImplicitTreap::clear() {
  50. clear(_root);
  51. _root = nullptr;
  52. }
  53.  
  54. void ImplicitTreap::clear(Node* node) {
  55. if (node == nullptr) return;
  56. clear(node->left);
  57. clear(node->right);
  58. delete node;
  59. }
  60.  
  61. void ImplicitTreap::insert(size_t position, const int& value) {
  62. Node* current = new Node(value);
  63. std::pair<Node*, Node*> ans = split(_root, position);
  64. Node* right = merge(current, ans.second);
  65. _root = merge(ans.first, right);
  66. }
  67.  
  68. void ImplicitTreap::remove(size_t position) {
  69. std::pair<Node*, Node*> ans1 = split(_root, position);
  70. std::pair<Node*, Node*> ans2 = split(ans1.second, 1);
  71. delete ans2.first;
  72. _root = merge(ans1.first, ans2.second);
  73. }
  74.  
  75. int ImplicitTreap::get(size_t position) const {
  76. size_t l;
  77. Node* node = _root;
  78. do {
  79. l = (node->left != nullptr) ? (node->left->treeSize) : 0;
  80. if (l == position) break;
  81. if (position < l) {
  82. node = node->left;
  83. } else {
  84. position -= l + 1;
  85. node = node->right;
  86. }
  87. } while (true);
  88. return node->value;
  89. }
  90.  
  91.  
  92. std::pair<ImplicitTreap::Node*, ImplicitTreap::Node*> ImplicitTreap::split(
  93. Node* node, size_t position) {
  94. if (node == nullptr) return std::pair<Node*, Node*>(nullptr, nullptr);
  95. size_t l = (node->left != nullptr) ? (node->left->treeSize) : 0;
  96. if (l >= position) {
  97. std::pair<Node*, Node*> ans = split(node->left, position);
  98. node->left = ans.second;
  99. node->fix();
  100. return std::make_pair(ans.first, node);
  101. } else {
  102. std::pair<Node*, Node*> ans = split(node->right, position - l - 1);
  103. node->right = ans.first;
  104. node->fix();
  105. return std::make_pair(node, ans.second);
  106. }
  107. }
  108.  
  109. ImplicitTreap::Node* ImplicitTreap::merge(Node* node1, Node* node2) {
  110. if (node2 == nullptr) return node1;
  111. if (node1 == nullptr) return node2;
  112. if (node1->priority > node2->priority) {
  113. node1->right = merge(node1->right, node2);
  114. node1->fix();
  115. return node1;
  116. } else {
  117. node2->left = merge(node1, node2->left);
  118. node2->fix();
  119. return node2;
  120. }
  121. }
  122.  
  123.  
  124. ImplicitTreap::Node::Node(const int& value)
  125. : value(value),
  126. priority(rand()),
  127. left(nullptr),
  128. right(nullptr),
  129. treeSize(1) {}
  130.  
  131. void ImplicitTreap::Node::fix() {
  132. treeSize = 1;
  133. if (left != nullptr) treeSize += left->treeSize;
  134. if (right != nullptr) treeSize += right->treeSize;
  135. }
  136.  
  137. int alpha = 0;
  138.  
  139. std::vector<int> Code(const std::vector<int>& v) {
  140. ImplicitTreap permutation;
  141. for (int i = 0; i < alpha; ++i) {
  142. permutation.insert(i, i + 1);
  143. }
  144. std::vector<int> res;
  145. int in = 0;
  146. int out = 0;
  147. for (int i = 0; i < v.size(); ++i) {
  148. in = v[i];
  149. out = permutation.get(in - 1);
  150. res.push_back(out);
  151. permutation.remove(out - 1);
  152. permutation.insert(0, in);
  153. }
  154. return res;
  155. }
  156.  
  157. std::vector<int> Decode(const std::vector<int>& v) {
  158. ImplicitTreap permutation;
  159. for (int i = 0; i < alpha; ++i) {
  160. permutation.insert(i, i + 1);
  161. }
  162. std::vector<int> res;
  163. int in = 0;
  164. int out = 0;
  165. for (int i = 0; i < v.size(); ++i) {
  166. in = v[i];
  167. int mid = in;
  168. bool flag = false;
  169. while ((mid = permutation.get(mid - 1)) != in) {
  170. out = mid;
  171. flag = true;
  172. } if (!flag) {
  173. out = permutation.get(in - 1);
  174. }
  175. res.push_back(out);
  176. permutation.remove(in - 1);
  177. permutation.insert(0, out);
  178. }
  179. return res;
  180. }
  181.  
  182. /*
  183. int main() {
  184. //std::srand(std::time(nullptr));
  185. std::ios_base::sync_with_stdio(false);
  186. std::cin.tie(NULL);
  187. int len = 0;
  188. std::cin >> len >> alpha;
  189. std::vector<int> start;
  190. for (int i = 0; i < len; ++i) {
  191. start.push_back(1 + std::rand() % alpha);
  192. }
  193. auto code = Code(start);
  194. auto res = Decode(code);
  195. if (res != start) {
  196. std::cout << "START: ";
  197. for (int i = 0; i < start.size(); ++i) {
  198. std::cout << start[i] << ' ';
  199. }
  200. std::cout << '\n';
  201. std::cout << "CODER: ";
  202. for (int i = 0; i < code.size(); ++i) {
  203. std::cout << code[i] << ' ';
  204. }
  205. std::cout << '\n';
  206. std::cout << "RESUL: ";
  207. for (int i = 0; i < res.size(); ++i) {
  208. std::cout << res[i] << ' ';
  209. }
  210. std::cout << '\n';
  211. }
  212. }
  213. */
  214.  
  215.  
  216. int main() {
  217. std::ios_base::sync_with_stdio(false);
  218. std::cin.tie(NULL);
  219. int len, alpha, type = 0;
  220. std::cin >> len >> alpha >> type;
  221. ImplicitTreap permutation;
  222. for (int i = 0; i < alpha; ++i) {
  223. permutation.insert(i, i + 1);
  224. }
  225. if (type == 2) {
  226. int in = 0;
  227. int out = 0;
  228. for (int i = 0; i < len; ++i) {
  229. std::cin >> in;
  230. out = permutation.get(in - 1);
  231. std::cout << out << ' ';
  232. permutation.remove(in - 1);
  233. permutation.insert(0, out);
  234. }
  235. } else {
  236. int in = 0;
  237. int out = 0;
  238. for (int i = 0; i < len; ++i) {
  239. std::cin >> in;
  240. out = permutation.get(in - 1);
  241. std::cout << out << ' ';
  242. permutation.remove(out - 1);
  243. permutation.insert(0, in);
  244. }
  245. }
  246. return 0;
  247. }
  248.  
  249.  
  250. /*
  251. * START: 2 2 3 3 2 3 1
  252. * CODER: 2 1 3 1 2 1 3
  253. * RESUL: 2 2 3 3 2 2 1
  254. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement