Flickyyy

Елки палки

Dec 12th, 2023
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #ifdef LOCAL_FLICKY
  3. #define DEBUG(val) std::cout << #val << " = " << val << '\n'
  4. #else
  5. #define DEBUG(val) {}
  6. #endif
  7. #define int ll
  8. using namespace std;
  9. using ll = long long;
  10. using pii = std::pair<int, int>;
  11.  
  12. void fast() {
  13. #ifdef LOCAL_FLICKY
  14. if (!freopen("in.txt", "r", stdin)) exit(1000 - 7);
  15. if (!freopen("out.txt", "w", stdout)) exit(1000 - 7);
  16. #endif
  17. std::ios_base::sync_with_stdio(false);
  18. std::cin.tie(nullptr);
  19. }
  20.  
  21. #define ALL(cont) cont.begin(), cont.end()
  22. using namespace std;
  23.  
  24. //#define MULTI_TESTS
  25. void solve();
  26. vector<int> trace;
  27.  
  28. signed main() {
  29. fast();
  30. #ifdef MULTI_TESTS
  31. int t; cin >> t;
  32. while (t--) {
  33. #endif
  34. solve();
  35. #ifdef MULTI_TESTS
  36. }
  37. #endif
  38. }
  39.  
  40.  
  41. template<typename Ty>
  42. struct Node {
  43. explicit Node(Ty val) : left(nullptr), right(nullptr), val_(val), size(1) {}
  44. Node (Ty val, Node* l, Node* r) : val_(val), left(l), right(r), size(1 + l->size + r->size) {}
  45. Node *left, *right;
  46. Ty val_;
  47. int size = 0;
  48. };
  49.  
  50. template <typename Ty>
  51. class BST {
  52. public:
  53.  
  54. Node<Ty>* Next(const Ty& val) {
  55. Node<Ty>* current = head_, *successor = nullptr;
  56. while (current != nullptr) {
  57. if (current->val_ > val) {
  58. successor = current;
  59. current = current->left;
  60. }
  61. else {
  62. current = current->right;
  63. }
  64. }
  65. return successor;
  66. }
  67.  
  68. Node<Ty>* Prev(const Ty& val) {
  69. Node<Ty>* current = head_, *successor = nullptr;
  70. while (current != nullptr) {
  71. if (current->val_ >= val) {
  72. current = current->left;
  73. }
  74. else {
  75. successor = current;
  76. current = current->right;
  77. }
  78. }
  79. return successor;
  80. }
  81.  
  82. Node<Ty>* Minimum(Node<Ty>* node) {
  83. if (node->left == nullptr) {
  84. return node;
  85. }
  86. return Minimum(node->left);
  87. }
  88.  
  89. Node<Ty>* Maximum(Node<Ty>* node) {
  90. if (node->right == nullptr) {
  91. return node;
  92. }
  93. return Maximum(node->right);
  94. }
  95.  
  96. Node<Ty>* GetHead() {
  97. return head_;
  98. }
  99.  
  100. bool Exist(const Ty& val) {
  101. return Find(head_, val) != nullptr;
  102. }
  103.  
  104. void Delete(const Ty& val) {
  105. head_ = Delete(head_, val);
  106. }
  107.  
  108. void Insert(const Ty& val) {
  109. head_ = Insert(head_, val);
  110. }
  111.  
  112. void PrintPreorder(Node<Ty>* node, vector<string> pref = {}) {
  113. if (node != nullptr) {
  114. #ifdef LOCAL_FLICKY
  115. cout << "val: " << node->val_ << " size: " << node->size << " : ";
  116. for (auto e : pref) {
  117. cout << e;
  118. }
  119. cout << '\n';
  120. #endif
  121. if (trace.size() < pref.size() + 1) {
  122. trace.push_back(node->val_);
  123. } else {
  124. trace[pref.size()] = node->val_;
  125. }
  126. pref.push_back("->left ");
  127. PrintPreorder(node->left, pref);
  128. pref.pop_back();
  129. pref.push_back("->right ");
  130. PrintPreorder(node->right, pref);
  131. pref.pop_back();
  132. }
  133. }
  134.  
  135. Ty KthElement(int k) {
  136. k = head_->size - k + 1;
  137. Node<Ty>* node = head_;
  138. while (true) {
  139. int size = 1;
  140. if (node->left != nullptr) {
  141. size = node->left->size + 1;
  142. }
  143. if (size == k) {
  144. return node->val_;
  145. } else if (node->left == nullptr || size < k) {
  146. k -= size;
  147. node = node->right;
  148. } else {
  149. node = node->left;
  150. }
  151. }
  152. exit(1);
  153. }
  154.  
  155. private:
  156. Node<Ty>* Insert(Node<Ty>* x, const Ty& z) {
  157. if (x == nullptr) {
  158. return new Node(z);
  159. }
  160. else {
  161. if (z < x->val_)
  162. x->left = Insert(x->left, z);
  163. else if (z > x->val_)
  164. x->right = Insert(x->right, z);
  165.  
  166. x->size = 1;
  167. if (x->left) x->size += x->left->size;
  168. if (x->right) x->size += x->right->size;
  169. }
  170.  
  171. return x;
  172. }
  173.  
  174. Node<Ty>* Delete(Node<Ty>* root, const Ty& val) {
  175. if (!Exist(val)) {
  176. return root;
  177. }
  178. if (root == nullptr) {
  179. return root;
  180. }
  181. root->size -= 1;
  182. if (val < root->val_) {
  183. root->left = Delete(root->left, val);
  184. }
  185. else if (val > root->val_) {
  186. root->right = Delete(root->right, val);
  187. }
  188. else if (root->left != nullptr and root->right != nullptr) {
  189. root->val_ = Minimum(root->right)->val_;
  190. root->right = Delete(root->right, root->val_);
  191. }
  192. else {
  193. if (root->left != nullptr) {
  194. root = root->left;
  195. }
  196. else if (root->right != nullptr) {
  197. root = root->right;
  198. }
  199. else {
  200. root = nullptr;
  201. }
  202. }
  203. return root;
  204. }
  205.  
  206. Node<Ty>* Find(Node<Ty>* x, const Ty& k) {
  207. if (x == nullptr or k == x->val_)
  208. return x;
  209. if (k < x->val_)
  210. return Find(x->left, k);
  211. else
  212. return Find(x->right, k);
  213. }
  214.  
  215. Node<Ty>* head_ = nullptr;
  216. };
  217.  
  218. const int inf = 1e9 + 7;
  219.  
  220. template<typename Ty>
  221. void travel(Node<Ty>* node, vector<int>& trace, int depth = 0) {
  222. if (node == nullptr) {
  223. return;
  224. }
  225. travel(node->right, trace, depth + 1);
  226. travel(node->left, trace, depth + 1);
  227. }
  228.  
  229. void solve() {
  230. int n; cin >> n;
  231. BST<int> tree;
  232. while (n-- > 0) {
  233. int v; cin >> v;
  234. tree.Insert(v);
  235. #ifdef LOCAL_FLICKY
  236. cout << "TREE:\n";
  237. tree.PrintPreorder(tree.GetHead());
  238. cout << "--------\n";
  239. cout.flush();
  240. #endif
  241. }
  242. tree.PrintPreorder(tree.GetHead());
  243. for (auto e : trace) cout << e << ' ';
  244. }
  245.  
Advertisement
Add Comment
Please, Sign In to add comment