Advertisement
tumaryui

Untitled

Mar 23rd, 2020
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. using namespace std;
  4.  
  5. struct node {
  6. int key, priority;
  7. node * left, * right;
  8. node (int nkey, int npriority){ key = nkey, priority = npriority , left = nullptr , right = nullptr;}
  9. };
  10.  
  11. void split (node *& t, int key, node *& l, node *& r) {
  12. if (!t)
  13. l = r = nullptr;
  14. else if (key < t->key)
  15. split (t->left, key, l, t->left), r = t;
  16. else
  17. split (t->right, key, t->right, r), l = t;
  18. }
  19. void insert (node *&t, node *add) {
  20. if (t == nullptr)
  21. t = add;
  22. else if (add->priority > t->priority)
  23. split (t, add->key, add->left, add->right), t = add;
  24. else
  25. insert (add->key < t->key ? t->left : t->right, add);
  26. }
  27.  
  28. void print_tree(node *& tree) {
  29. if(tree == nullptr) {
  30. cout << "null" << endl;
  31. return;
  32. }
  33. cout << tree -> key << " " << tree -> priority << endl;
  34. print_tree(tree -> left);
  35. print_tree(tree -> right);
  36. }
  37. main() {
  38. int n;
  39. cin >> n;
  40. int x[n], y[n];
  41. for(int i = 0; i < n; i++) {
  42. cin >> x[i] >> y[i];
  43. }
  44. node *tree = nullptr;
  45. for(int i = 0; i < n; i++) {
  46. node *tmp = new node(x[i], y[i]);
  47. insert(tree, tmp);
  48. }
  49. print_tree(tree);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement