Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.79 KB | None | 0 0
  1. #ifndef skeleta
  2. #include "orderedset.h"
  3. #endif
  4.  
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. #define T long long
  10.  
  11. const int BUCKETS = 40;
  12. const long long B = 1000000000000000000/BUCKETS;
  13.  
  14.  
  15. struct node {
  16.   T key;
  17.   unsigned long long priority;
  18.   unsigned size;
  19.   node *left, *right;
  20. };
  21.  
  22. node node_buff[1000007];
  23. int buff_pos;
  24.  
  25. struct treap {
  26.   private:
  27.         struct xorshift {
  28.       unsigned long long x,y,z,w;
  29.       xorshift(): x(1234567891011121314), y(91734892562378), z(77777777777777), w(986732789462378) {}
  30.       unsigned long long next() {
  31.         unsigned long long t=x^(x<<11);
  32.         x=y;y=z;z=w;
  33.         return w=w^(w>>19)^t^(t>>8);
  34.       }
  35.     };
  36.     typedef node *node_ptr;
  37.     node_ptr root;
  38.     xorshift rng;
  39.    
  40.     unsigned long long next() {
  41.       return rng.next();
  42.     }
  43.     node_ptr new_node(T key) {
  44.       //node_ptr res=(node_ptr)malloc(sizeof(node));
  45.       node_ptr res=&node_buff[buff_pos++];
  46.       res->key=key;
  47.       res->priority=next();
  48.       res->size=1;
  49.       res->left=NULL;
  50.       res->right=NULL;
  51.       return res;
  52.     }
  53.     unsigned size(node_ptr &root) {
  54.       if(root==NULL) return 0;
  55.       else return root->size;
  56.     }
  57.     void update_size(node_ptr &root) {
  58.       if(root!=NULL) root->size=1+size(root->left)+size(root->right);
  59.     }
  60.     node_ptr right_rotation(node_ptr x) {
  61.       node_ptr y=x->left,t2=y->right;
  62.       x->left=t2;
  63.       y->right=x;
  64.       update_size(x);
  65.       update_size(y);
  66.       return y;
  67.     }
  68.     node_ptr left_rotation(node_ptr y) {
  69.       node_ptr x=y->right,t2=x->left;
  70.       y->right=t2;
  71.       x->left=y;
  72.       update_size(y);
  73.       update_size(x);
  74.       return x;
  75.     }
  76.     node_ptr insert(node_ptr root, T key) {
  77.       if(root==NULL) {
  78.         return new_node(key);
  79.       }
  80.       else if(key<root->key) {
  81.         root->left=insert(root->left,key);
  82.         if(root->left->priority>root->priority) root=right_rotation(root);
  83.       }
  84.       else {
  85.         root->right=insert(root->right,key);
  86.         if(root->right->priority>root->priority) root=left_rotation(root);
  87.       }
  88.       update_size(root);
  89.       return root;
  90.     }
  91.     node_ptr remove_it(node_ptr root) {
  92.       if(root->left==NULL && root->right==NULL) {
  93.         return NULL;
  94.       }
  95.       if(root->left==NULL) {
  96.         root=left_rotation(root);
  97.         root->left=remove_it(root->left);
  98.         update_size(root);
  99.         return root;
  100.       }
  101.       if(root->right==NULL) {
  102.         root=right_rotation(root);
  103.         root->right=remove_it(root->right);
  104.         update_size(root);
  105.         return root;
  106.       }
  107.       if(root->left->priority>root->right->priority) {
  108.         root=right_rotation(root);
  109.         root->right=remove_it(root->right);
  110.       }
  111.       else {
  112.         root=left_rotation(root);
  113.         root->left=remove_it(root->left);
  114.       }
  115.       update_size(root);
  116.       return root;
  117.     }
  118.     node_ptr erase(node_ptr root, T key) {
  119.       if(key<root->key) {
  120.         root->left=erase(root->left,key);
  121.       }
  122.       else if(key>root->key) {
  123.         root->right=erase(root->right,key);
  124.       }
  125.       else {
  126.         root=remove_it(root);
  127.       }
  128.       update_size(root);
  129.       return root;
  130.     }
  131.     unsigned count_less(node_ptr root, T key) {
  132.       if(root==NULL) return 0;
  133.       else if(key<root->key) return count_less(root->left,key);
  134.       else if(key==root->key) return size(root->left);
  135.       else return 1+size(root->left)+count_less(root->right,key);
  136.     }
  137.     T kth(node_ptr root, unsigned k) {
  138.       if(1+size(root->left)==k) return root->key;
  139.       else if(k<=size(root->left)) return kth(root->left,k);
  140.       else return kth(root->right,k-1-size(root->left));
  141.     }
  142.     bool find(node_ptr root, T key) {
  143.       if(root==NULL) return false;
  144.       else if(key<root->key) return find(root->left,key);
  145.       else if(key>root->key) return find(root->right,key);
  146.       else return true;
  147.     }
  148.   public:
  149.     treap(): root(NULL) {}
  150.     void clear() {
  151.       root=NULL;
  152.       rng=xorshift();
  153.     }
  154.     unsigned size() {
  155.       return size(root);
  156.     }
  157.     bool empty() {
  158.       return (size()==0);
  159.     }
  160.     bool find(T key) {
  161.       return find(root,key);
  162.     }
  163.     void insert(T key) {
  164.       if(find(key)==false) root=insert(root,key);
  165.     }
  166.     void erase(T key) {
  167.       if(find(key)==true) root=erase(root,key);
  168.     }
  169.     T kth(unsigned k) {
  170.       return kth(root,k);
  171.     }
  172.     unsigned count_less(T key) {
  173.       return count_less(root,key);
  174.     }  
  175. };
  176.  
  177. treap t[BUCKETS + 4];
  178.  
  179. void add_number(long long x) {
  180.   t[x/B].insert(x);
  181. }
  182.  
  183. void remove_number(long long x) {
  184.   t[x/B].erase(x);
  185. }
  186.  
  187. long long kth_number(int k) {
  188.   for(int i=0;i<BUCKETS;i++) {
  189.     if(t[i].size()>=k) {
  190.       return t[i].kth(k);
  191.     }
  192.     k-=t[i].size();
  193.   }
  194. }
  195.  
  196. #ifdef skeleta
  197. int main() {
  198.  
  199. }
  200. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement