SPOJ 3273 - Order statistic set Treap模板题

来源:互联网 发布:淘宝网小米4s充电线 编辑:程序博客网 时间:2024/06/03 14:26

          题意:

                    有个集合..起初为空...有Q(1<=Q<=200000)个操作或者询问

                            操作insert:    I  x: 若集合中没有x...把x放入

                            操作delete:   D x:若集合中有x..删去x

                            询问K-th:       K  x:输出集合中第x大的数是什么...若总个数没有k个输出invalid

                            询问Count:   C  x:输出集合中有多少个数比x小... 

          题解:

                    裸treap了....操作还挺全的...为了找到第k大的数以及小于某个数的个数...每个点上加一个变量.用于存储以其为根的子树有多少个节点...insert和delete参考了nocow中CmYkRgB123的代码...两个询问时原创的~不用递归相当和谐..


Program:

#include<iostream>#include<stdio.h>#include<string.h>#include<cmath>#include<queue>#include<stack>#include<set>#include<time.h>#include<map>#include<algorithm>#define ll long long#define eps 1e-5#define oo 1000000007#define pi acos(-1.0)#define MAXN 200005using namespace std; struct node{       int l,r,key,fix,size;};struct treap{       node h[MAXN];       int root,num;       void initial() { srand((int)time(0)),num=root=0; }       void rot_l(int &x)       {               int R=h[x].r,L=h[x].l;                h[x].size=h[x].size-h[R].size+h[h[R].l].size;               h[R].size+=h[L].size+1;               h[x].r=h[R].l,h[R].l=x;               x=R;              }       void rot_r(int &x)       {               int L=h[x].l,R=h[x].r;               h[x].size=h[x].size-h[L].size+h[h[L].r].size;               h[L].size+=h[R].size+1;               h[x].l=h[L].r,h[L].r=x;               x=L;        }       bool insert(int &k,int key)       {               if (!k)               {                      k=++num;                      h[k].l=h[k].r=0,h[k].size=1;                      h[k].key=key,h[k].fix=rand();                      return true;               }               if (h[k].key==key) return false;               if (h[k].key>key)               {                      if (!insert(h[k].l,key)) return false;                      h[k].size++;                      if (h[h[k].l].fix>h[k].fix) rot_r(k);                       return true;               }else               {                      if (!insert(h[k].r,key)) return false;                      h[k].size++;                      if (h[h[k].r].fix>h[k].fix) rot_l(k);                       return true;               }       }       int count(int key)       {                int g=0,k=root;               while (k)               {                      if (h[k].key>key) k=h[k].l;                         else g+=h[h[k].l].size+1,k=h[k].r;               }               return g;       }       int k_th(int kth)       {                int g=0,k=root;               if (h[root].size<kth) return -1;                while (h[h[k].l].size+g+1!=kth)               {                       if (h[h[k].l].size+g+1>=kth) k=h[k].l;                         else g+=h[h[k].l].size+1,k=h[k].r;               }                    return h[k].key;                 }       bool del(int &k,int key)       {                if (!k) return false;               if (h[k].key>key)                {                       if (!del(h[k].l,key)) return false;                       h[k].size--;                }               else                if (h[k].key<key)                {                       if (!del(h[k].r,key)) return false;                       h[k].size--;                 }               else               {                       if (!h[k].l && !h[k].r) k=0;                          else                       if (!h[k].l) k=h[k].r;                           else                       if (!h[k].r) k=h[k].l;                       else                       {                                if (h[h[k].l].fix<h[h[k].r].fix)                                {                                        rot_l(k);                                        if (!del(h[k].l,key)) return false;                                        h[k].size--;                                }                      else                        {                            rot_r(k);                                        if (!del(h[k].r,key)) return false;                                        h[k].size--;                        }                       }                }               return true;       }       }mytreap;int main(){                     int m,x;       char c;           mytreap.initial();       scanf("%d",&m);       while (m--)       {              do { c=getchar(); } while (c<'A' || c>'Z');              scanf("%d",&x);              if (c=='I') mytreap.insert(mytreap.root,x); else              if (c=='C') printf("%d\n",mytreap.count(x-1)); else              if (c=='K')               {                     x=mytreap.k_th(x);                     if (x==-1) printf("invalid\n");                        else printf("%d\n",x);              }else              if (c=='D') mytreap.del(mytreap.root,x);       }       return 0;}


原创粉丝点击