hurmawe

Декартовое дерево по неявному ключу 1

Mar 6th, 2021 (edited)
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. struct node
  2. {
  3.     int value_node = NULL;
  4.     int size       = NULL;
  5.     node* left = nullptr;
  6.     node* right = nullptr;
  7.  
  8.     node(int new_value)
  9.     {
  10.         value_node = new_value;
  11.         size = 1;
  12.     }
  13. };
  14.  
  15. typedef pair<node*, node*> Pair;
  16.  
  17. void  update(node* A)
  18. {
  19.     A->size =1 + A->left->size + A->right->size;
  20. }
  21.  
  22. Pair split(node* A,const int& x)
  23. {
  24.     if (!A) return { NULL ,NULL };
  25.     int l = A->left->size;
  26.     if (l >= x)
  27.     {
  28.         Pair q = split(A->left, x);
  29.         A->left = q.second;
  30.         update(A);
  31.         return { q.first, A };
  32.     }
  33.     else
  34.     {
  35.         Pair q = split(A->right, x-l-1);
  36.         A->right = q.first;
  37.         update(A);
  38.         return {A, q.second};
  39.     }
  40. }
  41. node* merge(node* A,node* B)
  42. {
  43.     if (!A) return B;
  44.     if (!B) return A;
  45.  
  46.     if (A->size > B->size)
  47.     {
  48.         A->right = merge(A->right, B);
  49.         return A;
  50.     }
  51.     else
  52.     {
  53.         B->left = merge(A, B->left);
  54.         return B;
  55.     }
  56. }
Add Comment
Please, Sign In to add comment