Advertisement
zhukov000

Cartesian

Dec 27th, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int MAX_CNT = 1000000;
  5.  
  6. struct Node
  7. {
  8.   int key, y;
  9.   int left, right;
  10.   int cnt;
  11.  
  12.   Node(int x = 0): key(x), y(0), left(0), right(0), cnt(1) {}
  13.   void gen_y()
  14.   {
  15.     y = (rand() << 15) | rand();
  16.   }
  17. };
  18.  
  19. Node nodes[MAX_CNT + 1];
  20. queue<int> free_nodes;
  21.  
  22. void init()
  23. {
  24.   for(int i=1; i<=MAX_CNT; ++i) free_nodes.push(i);
  25. }
  26.  
  27. int alloc_node()
  28. {
  29.   if (free_nodes.size() == 0) assert(false);
  30.   int i = free_nodes.front();
  31.   free_nodes.pop();
  32.   return i;
  33. }
  34.  
  35. void free_node(int i)
  36. {
  37.   free_nodes.push(i);
  38. }
  39.  
  40. int root_id = 0; // id of root node
  41.  
  42. // get count of nodes in tree
  43. int get_cnt(const int & root)
  44. {
  45.   if (root == 0) return 0;
  46.   return nodes[root].cnt;
  47. }
  48.  
  49. // change info about count
  50. void update_cnt(const int & root)
  51. {
  52.   if (root == 0) return;
  53.   int left_cnt = get_cnt(nodes[root].left);
  54.   int right_cnt = get_cnt(nodes[root].right);
  55.   nodes[root].cnt = left_cnt + right_cnt + 1;
  56. }
  57.  
  58. // return true if node with key in tree root
  59. bool exists(const int & root, int key)
  60. {
  61.   if ( root == 0 ) return false;
  62.   if ( nodes[root].key == key ) return true;
  63.   if ( key < nodes[root].key )
  64.     return exists( nodes[root].left, key );
  65.   return exists( nodes[root].right, key );
  66. }
  67.  
  68. // split tree by key and return two roots
  69. // [ ... k] | (k ... ]
  70. pair<int, int> split(const int & root, int key)
  71. {
  72.   if ( root == 0 ) return {0, 0};
  73.   if ( key <= nodes[root].key )
  74.   {
  75.     auto res = split( nodes[root].left, key ); // res.first => L, res.second => R, nodes[node] => X
  76.     nodes[root].left = res.second;
  77.     update_cnt(root);
  78.     return {res.first, root};
  79.   }
  80.   else
  81.   {
  82.     auto res = split( nodes[root].right, key );
  83.     nodes[root].right = res.first;
  84.     update_cnt(root);
  85.     return {root, res.second};
  86.   }
  87. }
  88.  
  89. // merge two trees and return root
  90. int merge(const int & root1, const int & root2)
  91. {
  92.   if ( root1 == 0 ) return root2;
  93.   if ( root2 == 0 ) return root1;
  94.   if ( nodes[root1].y <= nodes[root2].y )
  95.   {
  96.      nodes[root1].right = merge( nodes[root1].right, root2 );
  97.      update_cnt(root1);
  98.      return root1;
  99.   }
  100.   else
  101.   {
  102.     nodes[root2].left = merge( root1, nodes[root2].left );
  103.     update_cnt(root2);
  104.     return root2;
  105.   }
  106. }
  107.  
  108. int insert( int & root, const int & key )
  109. {
  110.   if ( exists(root, key) ) return root;
  111.   auto res = split(root, key);
  112.   int new_node = alloc_node();
  113.   nodes[new_node].key = key;
  114.   nodes[new_node].gen_y();
  115.   root = merge( merge(res.first, new_node), res.second );
  116.   return root;
  117. }
  118.  
  119. int erase(int & root, const int & key)
  120. {
  121.   if( !exists(root, key) ) return root;
  122.   auto res1 = split( root, key );
  123.   auto res2 = split( res1.first, key-1 );
  124.   free_node( res2.second );
  125.   root = merge(res2.first, res1.second);
  126.   return root;
  127. }
  128.  
  129. int depth(const int & root)
  130. {
  131.   if (root == 0) return 0;
  132.   int left_depth = depth(nodes[root].left);
  133.   int right_depth = depth(nodes[root].right);
  134.   return max(left_depth, right_depth) + 1;
  135. }
  136.  
  137. int main()
  138. {
  139.   ios_base::sync_with_stdio(0);
  140.   cin.tie(0);
  141.   cout.tie(0);
  142.   // freopen("INPUT.TXT", "r", stdin);
  143.   init();
  144.   int n = MAX_CNT;
  145.   srand(time(0));
  146.   for(int i=0; i<n; ++i)
  147.   {
  148.     int x = (rand() << 15) | rand();
  149.     root_id = insert(root_id, x);
  150.   }
  151.  
  152.   cout << depth(root_id) << " " << nodes[root_id].cnt;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement