Advertisement
anoosykh95

Untitled

Jul 22nd, 2016
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct node;
  4. typedef node* pnode;
  5. struct node{
  6.     int key , prior , freq , cnt;
  7.     pnode l , r;
  8.     static pnode empt;
  9.     node(){
  10.         key=prior=freq=cnt=0;
  11.         l=r=this;
  12.     }
  13.     node(int V){
  14.         key = V; prior = rand(); freq=cnt=1;
  15.         l=r=empt;
  16.     }
  17. };
  18. pnode node::empt = new node();
  19. pnode dummy = node::empt;
  20. class Treap{
  21.     pnode root;
  22.     private:
  23.     void update(pnode &it){
  24.         it->cnt = it->l->cnt + it->r->cnt + it->freq;
  25.     }
  26.     void split(pnode it , int K , pnode&l , pnode&r){
  27.         if(it == dummy)
  28.             l = r = dummy;
  29.         else if(it->key > K)
  30.             split(it->l , K , l , it->l) , r = it; // pay attention
  31.         else
  32.             split(it->r , K , it->r , r) , l = it; // these are , not ;
  33.         update(it);
  34.     }
  35.     void merge_(pnode &it , pnode l , pnode r){
  36.         if(l==dummy || r==dummy)
  37.             it = (l==dummy)? r : l;
  38.         else if(l->prior > r->prior) merge_(l->r , l->r , r) , it=l;
  39.         else merge_(r->l , l , r->l) , it=r;
  40.         update(it);
  41.     }
  42.     void insert_(pnode &it, pnode item) {
  43.         if (it == dummy)
  44.             it = item;
  45.         else if(item -> prior > it -> prior)
  46.             split(it , item->key , item->l , item->r) , it = item;
  47.         else insert_(it->key > item->key ? it->l : it->r , item);
  48.         update(it);
  49.     }
  50.     void erase_(pnode &it , int K){
  51. //        cout<<it->key<<endl;
  52.         if(it->key == K) merge_(it , it->l , it->r);
  53.         else erase_(it->key > K ? it->l : it->r , K);
  54.         update(it);
  55.     }
  56.     void inc(pnode it , int K , int V){
  57.         if(it->key == K) it->freq += V;
  58.         else inc(it->key > K ? it->l : it->r , K , V);
  59.         update(it);
  60.     }
  61.     int find_(pnode it , int K){
  62.         if(it == dummy) return 0;
  63.         if(it->key == K) return it->freq;
  64.         return find_(it->key > K ? it->l : it->r , K);
  65.     }
  66.     int Kth(pnode it , int K){
  67.         if(it->l->cnt >= K) return Kth(it->l , K);
  68.         if(it->l->cnt + it->freq < K) return Kth(it->r , K-it->l->cnt-it->freq);
  69.         return it->key;
  70.     }
  71.     int countless(pnode it , int K){
  72.         if(it == dummy) return 0;
  73.         if(it->key > K) return countless(it->l , K);
  74.         return it->l->cnt + it->freq + countless(it->r , K);
  75.     }
  76.     void Clear( pnode &it ){
  77.         if(!it || it == dummy) return;
  78.         Clear(it->l); Clear(it->r);
  79.         delete(it);
  80.     }
  81.     public:
  82.     //////////////////////////////////////////////////////
  83.     // these are the functions you call
  84.  
  85.     void init(){
  86.         Clear(root);
  87.         root = dummy;
  88.     }
  89.  
  90.     void Insert(int K){
  91.         if(find_(root , K)) inc(root , K , 1);
  92.         else insert_(root , new node(K));
  93.     }
  94.     void Delete(int K){
  95.         int tt = find_(root , K);
  96.         if(tt > 1) inc(root , K , -1);
  97.         else if(tt==1) erase_(root , K);
  98.     }
  99.     int Count(int K){ // you can use count as a find :D
  100.         return find_(root , K);
  101.     }
  102.     int Get_Kth(int K){
  103.         return Kth(root , K);
  104.     }
  105.     int Rank(int K){ // this function returns how many elements less than or equal
  106.         return countless(root , K);
  107.     }
  108.     int Size(){
  109.         return root->cnt;
  110.     }
  111. } ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement