传送门:

这是一道treap的模板题,不要问我为什么一直在写模板题

依旧只放代码

Treap 版

 //SPOJ 3273
 //by Cydiater
 //2016.8.31
 #include <iostream>
 #include <cstring>
 #include <ctime>
 #include <cmath>
 #include <cstdlib>
 #include <string>
 #include <algorithm>
 #include <queue>
 #include <map>
 #include <iomanip>
 #include <cstdio>
 using namespace std;
 #define ll long long
 #define up(i,j,n)        for(int i=j;i<=n;i++)
 #define down(i,j,n)        for(int i=j;i>=n;i--)
 ;
 const int oo=0x3f3f3f3f;
 inline int read(){
     ,f=;
     ;ch=getchar();}
     +ch-';ch=getchar();}
     return x*f;
 }
 ,tol=;
 char op;
 map<int,int>m;
 struct node{
     int rnd,v,cnt,siz,leftt,rightt;
 }t[MAXN];
 namespace solution{
     void updata(int k){
         t[k].siz=t[t[k].leftt].siz+t[t[k].rightt].siz+t[k].cnt;
     }
     void lefturn(int &k){
         int tt=t[k].rightt;t[k].rightt=t[tt].leftt;t[tt].leftt=k;
         t[tt].siz=t[k].siz;updata(k);k=tt;
     }
     void righturn(int &k){
         int tt=t[k].leftt;t[k].leftt=t[tt].rightt;t[tt].rightt=k;
         t[tt].siz=t[k].siz;updata(k);k=tt;
     }
     void insert(int &k,int x){
         ){
             k=++tol;t[k].leftt=t[k].rightt=;
             t[k].cnt=;t[k].siz=;t[k].v=x;
             t[k].rnd=rand();
             return;
         }
         t[k].siz++;
         if(t[k].v==x)t[k].cnt++;
         else if(x>t[k].v){
             insert(t[k].rightt,x);
             if(t[t[k].rightt].rnd<t[k].rnd)lefturn(k);
         }else if(x<t[k].v){
             insert(t[k].leftt,x);
             if(t[t[k].leftt].rnd<t[k].rnd)righturn(k);
         }
     }
     void del(int &k,int x){
         )    return;
         if(x==t[k].v){
             ){t[k].siz--;t[k].cnt--;return;}
             )    k=t[k].leftt+t[k].rightt;
             else if(t[t[k].leftt].rnd<t[t[k].rightt].rnd){
                 righturn(k);
                 del(k,x);
             }
             else if(t[t[k].leftt].rnd>t[t[k].rightt].rnd){
                 lefturn(k);
                 del(k,x);
             }
         }else if(x>t[k].v){
             t[k].siz--;
             del(t[k].rightt,x);
         }else if(x<t[k].v){
             t[k].siz--;
             del(t[k].leftt,x);
         }
     }
     int query_num(int k,int x){
         )                                return oo;
         if(x<=t[t[k].leftt].siz)                return query_num(t[k].leftt,x);
         else if(x>t[t[k].leftt].siz+t[k].cnt)    return query_num(t[k].rightt,x-(t[t[k].leftt].siz+t[k].cnt));
         else                                    return t[k].v;
     }
     int query_siz(int k,int x){
         )                                ;
         if(x<=t[k].v)                            return query_siz(t[k].leftt,x);
         else if(x>t[k].v)                        return t[t[k].leftt].siz+t[k].cnt+query_siz(t[k].rightt,x);
     }
     void slove(){
         N=read();
         while(N--){
             scanf("%c",&op);num=read();
             ){insert(root,num);m[num]=;}
             ){del(root,num);m[num]=;}
             if(op=='K'){
                 if(num>t[root].siz)puts("invalid");
                 else{
                     int ans=query_num(root,num);
                     if(ans>=oo){
                         puts("invalid");
                         continue;
                     }
                     printf("%d\n",ans);
                 }
             }
             if(op=='C')printf("%d\n",query_siz(root,num));
         }
     }
 }
 int main(){
     //freopen("input.in","r",stdin);
     using namespace solution;
     slove();
     ;
 }

Splay 版:

splay会被卡掉..0.3s貌似扛不住了,不过过了前几组,先粘上来吧

 //SPOJ 3273
 //by Cydiater
 //2016.9.4
 #include <iostream>
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
 #include <string>
 #include <iomanip>
 #include <ctime>
 #include <cmath>
 #include <iomanip>
 #include <queue>
 #include <map>
 using namespace std;
 #define ll long long
 #define up(i,j,n)        for(int i=j;i<=n;i++)
 #define down(i,j,n)        for(int i=j;i>=n;i--)
 ;
 const int oo=0x3f3f3f3f;
 ;
 ;
 inline int read(){
     ,f=;
     ;ch=getchar();}
     +ch-';ch=getchar();}
     return x*f;
 }
 char op;
 ,root=,num,N;
 struct SpalyTree{
     ],siz,cnt,v,fa;
 }t[MAXN];
 struct Hash{
     int Num[MAXN];
     Hash(){up(i,,MAXN-)Num[i]=-oo;}
     void insert(int x){
         int tmp=x%mod;
         ||Num[tmp]!=-oo){tmp+=step;tmp%=mod;}
         Num[tmp]=x;
     }
     bool Get(int x){
         ){hashvalue+=step;hashvalue%=mod;}
         while(Num[hashvalue]!=x&&Num[hashvalue]!=-oo){hashvalue+=step;hashvalue%=mod;}
         ;
         ;
     }
     void del(int x){
         int tmp=x%mod;
         ||Num[tmp]!=x){tmp+=step;tmp%=mod;}
         Num[tmp]=;
     }
 }lable;
 namespace solution{
     void updata(int x){
         if(x){
             t[x].siz=t[x].cnt;
             ])t[x].siz+=t[t[x].son[]].siz;
             ])t[x].siz+=t[t[x].son[]].siz;
         }
     }
     ]==x;}
     ]=t[x].son[]=t[x].fa=t[x].siz=t[x].cnt=t[x].v=;}
     void rotate(int x){
         int old=t[x].fa,oldf=t[old].fa,which=get(x);
         t[old].son[which]=t[x].son[which^];t[t[old].son[which]].fa=old;
         t[old].fa=x;t[x].son[which^]=old;
         t[x].fa=oldf;
         ]==old]=x;
         updata(old);updata(x);
     }
     void splay(int x){
         for(int fa;(fa=t[x].fa);rotate(x))if(t[fa].fa)
         rotate((get(x)==get(fa)?fa:x));root=x;
     }
     void insert(int x){
         ){
             root=++tol;
             t[root].son[]=t[root].son[]=t[root].fa=;
             t[root].v=x;t[root].cnt=t[root].siz=;
             return;
         }
         ;
         ){
             if(t[now].v==x){
                 t[now].cnt++;
                 updata(now);updata(fa);
                 splay(now);break;
             }
             fa=now;now=t[now].son[x>t[now].v];
             ){
                 now=++tol;
                 t[now].son[]=t[now].son[]=;t[now].v=x;
                 t[now].siz=t[now].cnt=;t[now].fa=fa;
                 t[fa].son[x>t[fa].v]=tol;
                 updata(fa);splay(now);break;
             }
         }
     }
     int find(int v){
         ,now=root;
         ){
             ];
             else{
                 ans+=(t[now].son[]?t[t[now].son[]].siz:);
                 if(v==t[now].v){
                     splay(now);
                     ;
                 }
                 ans+=t[now].cnt;
                 now=t[now].son[];
             }
         }
     }
     int pre(){
         ];
         ])now=t[now].son[];
         return now;
     }
     int match_rank(int x){
         int now=root;
         ){
             ]&&x<=t[t[now].son[]].siz)now=t[now].son[];
             else{
                 ]?t[t[now].son[]].siz:)+t[now].cnt;
                 if(x<=tmp)        return t[now].v;
                 x-=tmp;now=t[now].son[];
             }
         }
     }
     void del(int x){
         int whatever=find(x);
         ){
             t[root].cnt--;
             t[root].siz--;
             return;
         }
         ]+t[root].son[]==){
             clear(root);root=;
             return;
         }
         ]){
             ];t[root].fa=;
             clear(oldroot);return;
         }]){
             ];t[root].fa=;
             clear(oldroot);return;
         }
         int leftbig=pre(),oldroot=root;
         splay(leftbig);
         t[t[oldroot].son[]].fa=root;
         t[root].son[]=t[oldroot].son[];
         clear(oldroot);
         updata(root);
     }
     int get_siz(int num){
         int now=root;
         ){
             ]].siz+t[now].cnt;
             ];
         }
     }
 }
 int main(){
     //freopen("input.in","r",stdin);
     using namespace solution;
     N=read();
     while(N--){
         scanf("%c",&op);num=read();
         ){insert(num);lable.insert(num);}
         ){del(num);lable.del(num);}
         if(op=='K'){
             if(num>t[root].siz)puts("invalid");
             else printf("%d\n",match_rank(num));
         }
         if(op=='C')printf("%d\n",get_siz(num));
     }
     ;
 }

SPOJ 3273的更多相关文章

  1. SPOJ 3273 - Order statistic set , Treap

    点击打开链接 题意: 集合S支持一下四种操作:   INSERT(S,x) :   假设S中没有x,则插入x DELETE(S,x):  假设S中有x,则删除x K-TH(S):           ...

  2. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  3. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

  4. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  5. 【填坑向】spoj COT/bzoj2588 Count on a tree

    这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...

  6. SPOJ bsubstr

    题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...

  7. 【SPOJ 7258】Lexicographical Substring Search

    http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...

  8. 【SPOJ 1812】Longest Common Substring II

    http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...

  9. 【SPOJ 8222】Substrings

    http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...

随机推荐

  1. 仿各种APP将文章DOM转JSON并在APP中以列表显示(android、ios、php已开源)

    背景 一直以来都想实现类似新闻客户端.鲜城等文章型app的正文显示,即在web editor下编辑后存为json,在app中解析json并显示正文. 网上搜过,没找到轮子.都是给的思路,然后告知是公司 ...

  2. 外网不能访问部署在虚机的NodeJs网站(80端口)

    外网能访问部署在虚机的NodeJs网站需注意如下: 在管理门户上配置端点(Http 80->80) 在虚机中的防火墙入站规则中增加应用程序Node.exe的允许规则 启动NodeJs的侦听进程时 ...

  3. svn1.8 server client eclipse 插件 配置 完全教程

    svn毋庸置疑,广受欢迎的版本管理软件,我们这里以1.8.10版本为例 本文分三部分 第一部分,服务器端svn安装与配置 第二部分,eclipse下svn插件安装与配置 第三部分,客户端svn简单介绍 ...

  4. redis入门配置

    简介: Redis是Nosql中比较出名的,分布式数据库缓存,提升相应的速度,降低对数据库的访问! Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,(永久 ...

  5. linux基础-第十四单元 Linux网络原理及基础设置

    第十四单元 Linux网络原理及基础设置 三种网卡模式图 使用ifconfig命令来维护网络 ifconfig命令的功能 ifconfig命令的用法举例 使用ifup和ifdown命令启动和停止网卡 ...

  6. canvas三角函数应用

    这个是圆圈旋转的简单案例 var canvas=document.getElementById("canvas"); var cxt=canvas.getContext(" ...

  7. 2016年GitHub 排名前 100 的安卓、iOS项目简介(收藏)

    排名完全是根据 GitHub 搜索 Java 语言选择 (Best Match) 得到的结果, 然后过滤了跟 Android 不相关的项目, 所以排名并不具备任何官方效力, 仅供参考学习, 方便初学者 ...

  8. 求二叉树的宽度C语言版

    /*层次遍历二叉树,每一层遍历完成以后都重新插入特定的指针 (比如本例使用的特殊指针是数据元素为#,左右儿子为空的指针), 这样在每次访问到所指向数据为#的队列中的结点指针是就知道该指针是这层的末尾, ...

  9. JAVA程序员一定知道的优秀第三方库(2016版)

    几乎每个程序员都知道要“避免重复发明轮子”的道理——尽可能使用那些优秀的第三方框架或库,但当真正进入开发时,我却经常发现他们有时并不知道那些轮子在哪里.最近,我在业余时间带几个年轻的程序员一起做了一个 ...

  10. Java反编译插件JadClipse

    Java反编译是很容易的,现在就介绍一个反编译插件,以后我们通过Ctrl+鼠标左键查看源码就容易得多了,不用再担心源码找不到了,配置过程很简单的. 准备: 1.下载JadClipse(jar文件,ec ...