Advertisement
Tbl_Mne_Ne_Dryg

Untitled

Jan 5th, 2023
892
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define int long long
  3. using namespace std;
  4.  
  5. mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());
  6.  
  7. struct node {
  8.   int x = 0, y = gen();
  9.   node *l = nullptr;
  10.   node *r = nullptr;
  11.   node() = default;
  12.   node(int& x) {
  13.     this->x = x;
  14.   }
  15. };
  16.  
  17. node* merge(node* a, node* b) {
  18.   if (a == nullptr)
  19.     return b;
  20.   if (b == nullptr)
  21.     return a;
  22.   if (a->y > b->y) {
  23.     a->r = merge(a->r, b);
  24.     return a;
  25.   } else {
  26.     b->l = merge(a, b->l);
  27.     return b;
  28.   }
  29. }
  30.  
  31. pair<node*, node*> split(node* a, int x) {
  32.   if (a == nullptr)
  33.     return {nullptr, nullptr};
  34.   if (a->x < x) {
  35.     auto spl_R = split(a->r, x);
  36.     a->r = spl_R.first;
  37.     return {a, spl_R.second};
  38.   } else {
  39.     auto spl_L = split(a->l, x);
  40.     a->l = spl_L.second;
  41.     return {spl_L.first, a};
  42.   }
  43. }
  44.  
  45. node* find(node* a, int x) {
  46.   if (a == nullptr)
  47.     return nullptr;
  48.   if (a->x == x)
  49.     return a;
  50.   if (a->x > x)
  51.     return find(a->l, x);
  52.   if (a->x < x)
  53.     return find(a->r, x);
  54.   return nullptr;
  55. }
  56.  
  57. node* insert(node* a, int x) {
  58.   if (find(a, x) != nullptr)
  59.     return a;
  60.   node* A = new node(x);
  61.   auto spl = split(a, x);
  62.   A = merge(spl.first, A);
  63.   A = merge(A, spl.second);
  64.   return A;
  65. }
  66.  
  67. node* erase(node* a, int x) {
  68.   auto spl = split(a, x);
  69.   auto spl_L = split(spl.second, x + 1);
  70.   a = merge(spl.first, spl_L.second);
  71.   return a;
  72. }
  73.  
  74. node* getRight(node* a) {
  75.   if (a == nullptr)
  76.     return nullptr;
  77.   return (a->r == nullptr ? a : getRight(a->r));
  78. }
  79.  
  80. node* getLeft(node* a) {
  81.   if (a == nullptr)
  82.     return nullptr;
  83.   return (a->l == nullptr ? a : getLeft(a->l));
  84. }
  85.  
  86. node* next(node* a, int x) {
  87.   auto spl = split(a, x + 1);
  88.   auto ans = getLeft(spl.second);
  89.   a = merge(spl.first, spl.second);
  90.   return ans;
  91. }
  92.  
  93. node* prev(node* a, int x) {
  94.   auto spl = split(a, x);
  95.   auto ans = getRight(spl.first);
  96.   a = merge(spl.first, spl.second);
  97.   return ans;
  98. }
  99.  
  100. void display(node* a) {
  101.   if (a == nullptr)
  102.     return;
  103.   display(a->l);
  104.   cout << a->x << ' ';
  105.   display(a->r);
  106. }
  107.  
  108. signed main() {
  109.   ios::sync_with_stdio(false);
  110.   cin.tie(nullptr);
  111.   // freopen("input.txt", "r", stdin);
  112.   string s;
  113.   node* dd = nullptr;
  114.   while (cin >> s) {
  115.     if (s == "insert") {
  116.       int x;
  117.       cin >> x;
  118.       dd = insert(dd, x);
  119.     }
  120.     if (s == "delete") {
  121.       int x;
  122.       cin >> x;
  123.       dd = erase(dd, x);
  124.     }
  125.     if (s == "exists") {
  126.       int x;
  127.       cin >> x;
  128.       if (find(dd, x) == nullptr)
  129.         cout << "false\n";
  130.       else
  131.         cout << "true\n";
  132.     }
  133.     if (s == "next") {
  134.       int x;
  135.       cin >> x;
  136.       auto ans = next(dd, x);
  137.       cout << (ans == nullptr ? "none" : to_string(ans->x)) << '\n';
  138.     }
  139.     if (s == "prev") {
  140.       int x;
  141.       cin >> x;
  142.       auto ans = prev(dd, x);
  143.       cout << (ans == nullptr ? "none" : to_string(ans->x)) << '\n';
  144.     }
  145.   }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement