SPOJ 3273 - Order statistic set Treap模板题

          题意:

                    有个集合..起初为空...有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 200005
using 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值