Advertisement
zhukov000

Template for Treap

Dec 29th, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct Key
  5. {
  6.   int reliability, id;
  7.   Key(int _id, int _reliability = 0): id(_id), reliability(_reliability) {}
  8.   bool operator<(const Key & b) { return reliability > b.reliability || reliability == b.reliability && id < b.id; }
  9. };
  10.  
  11. struct SecurityNode
  12. {
  13.   string code;
  14.   Key key;
  15.   int y, left, right, cnt;
  16.   SecurityNode(): key(0) {}
  17. };
  18.  
  19. const int MAX_CNT = 100000;
  20. vector<bool> deleted(MAX_CNT + 1, false);
  21. SecurityNode nodes[MAX_CNT + 1];
  22. unordered_map<string, int> code_id;
  23.  
  24. int root_id = 0, last_used_id = 0;
  25.  
  26. int get_cnt(const int & node_id) // return cnt-field value or zero
  27. {
  28.   if(!node_id) return 0;
  29.   return nodes[node_id].cnt;
  30. }
  31.  
  32. void update_cnt(const int & node_id); // update cnt-field value for node
  33. pair<int, int> split(const int & node_id, Key & key); // return two treaps
  34. int & merge(int & noed1, int & node2); // merge two treaps and return id of result treap
  35.  
  36. void insert(Key key, const string & code); // insert into treap
  37. void erase(Key key); // erase node with key
  38.  
  39. // [0 ... n)
  40. int get_k(const int & node_id, const int & k) // return node_id for element in k position
  41. {
  42.   if (!node_id) return 0;
  43.   int left_cnt = get_cnt(nodes[node_id].left);
  44.   if (k < left_cnt) return get_k(nodes[node_id].left, k);
  45.   if (k == left_cnt || !nodes[node_id].right) return node_id;
  46.   return get_k(nodes[node_id].right, k - (left_cnt + 1));
  47. }
  48.  
  49. int main()
  50. {
  51.   // TODO
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement