第一次(2017.12.24):
#include<cstdio>
#include<cctype>
#include<algorithm>
//#define gc() getchar()
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
const int N=1e5+5,MAXIN=2e6;
char IN[MAXIN],*SS=IN,*TT=IN;
int size,sz[N],son[N][2],val[N],fix[N];
int New_Node(int v)
{
sz[++size]=1, val[size]=v, fix[size]=rand();
return size;
}
inline void Update(int rt)
{
sz[rt]=sz[son[rt][0]]+sz[son[rt][1]]+1;
}
void Split(int rt,int v,int &x,int &y)
{
if(!rt) x=y=0;
else
{
if(val[rt]<=v) x=rt,Split(son[rt][1],v,son[rt][1],y);
else y=rt,Split(son[rt][0],v,x,son[rt][0]);
Update(rt);
}
}
int Merge(int x,int y)
{
if(!x||!y) return x+y;
if(fix[x]<fix[y])
{
son[x][1]=Merge(son[x][1],y);
Update(x);
return x;
}
else
{
son[y][0]=Merge(x,son[y][0]);
Update(y);
return y;
}
}
int Kth(int rt,int k)
{
while(1)
{
if(sz[son[rt][0]]+1==k) return rt;
if(sz[son[rt][0]]<k) k-=sz[son[rt][0]]+1,rt=son[rt][1];
else rt=son[rt][0];
}
}
int Find(int rt,int w)
{
while(son[rt][w]) rt=son[rt][w];
return val[rt];
}
inline int read()
{
int now=0,f=1;register char c=gc();
for(;!isdigit(c);c=gc()) if(c=='-') f=-1;
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now*f;
}
int main()
{
int q=read(),opt,v,x,y,z,root=0;
while(q--)
{
opt=read(),v=read();
switch(opt)
{
case 1: Split(root,v,x,y),root=Merge(Merge(x,New_Node(v)),y);//Insert
break;
case 2: Split(root,v,x,z),Split(x,v-1,x,y);//Delete
y=Merge(son[y][0],son[y][1]),root=Merge(Merge(x,y),z);
break;
case 3: Split(root,v-1,x,y),printf("%d
",sz[x]+1);//Get_Rank
root=Merge(x,y);
break;
case 4: printf("%d
",val[Kth(root,v)]);//Rank
break;
case 5: Split(root,v-1,x,y),printf("%d
",val[Kth(x,sz[x])]);//printf("%d
",Find(x,1));
root=Merge(x,y);//Precursor
break;
case 6: Split(root,v,x,y),printf("%d
",val[Kth(y,1)]);//printf("%d
",Find(y,0));
root=Merge(x,y);//Successor
break;
}
}
return 0;
}
第二次(2017.3.24):差不了太多。
//尴尬 rank没加1
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
const int N=1e5+5;
struct fhq_Treap
{
#define lson son[rt][0]
#define rson son[rt][1]
int tot,son[N][2],val[N],fix[N],sz[N];
inline int New_Node(int v){
sz[++tot]=1, fix[tot]=rand(), val[tot]=v;
return tot;
}
inline void Update(int rt){
sz[rt]=sz[lson]+sz[rson]+1;
}
int Merge(int x,int y)
{
if(!x||!y) return x^y;
if(fix[x]<fix[y])
{
son[x][1]=Merge(son[x][1],y);
Update(x);
return x;
}
else
{
son[y][0]=Merge(x,son[y][0]);
Update(y);
return y;
}
}
void Split(int rt,int v,int &x,int &y)
{
if(!rt) x=y=0;
else{
if(val[rt]<=v) x=rt,Split(rson,v,rson,y);
else y=rt,Split(lson,v,x,lson);
Update(rt);
}
}
int Kth(int k,int rt)
{
while(1)
{
if(sz[lson]<k && sz[lson]+1==k) return val[rt];
if(sz[lson]<k) k-=sz[lson]+1,rt=rson;
else rt=lson;
}
}
int Find(int rt,int w)
{
while(son[rt][w]) rt=son[rt][w];
return val[rt];
}
}t;
inline int read()
{
int now=0,f=1;register char c=gc();
for(;!isdigit(c);c=gc()) if(c=='-') f=-1;
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now*f;
}
int main()
{
int q=read(),opt,val,x,y,z,root=0;
while(q--)
{
switch(opt=read(),val=read(),opt)
{
case 1: t.Split(root,val,x,y),root=t.Merge(t.Merge(x,t.New_Node(val)),y);
break;
case 2: t.Split(root,val,x,z),t.Split(x,val-1,x,y),
y=t.Merge(t.son[y][0],t.son[y][1]),root=t.Merge(t.Merge(x,y),z);
break;
case 3: t.Split(root,val-1,x,y),printf("%d
",t.sz[x]+1),root=t.Merge(x,y);
break;
case 4: printf("%d
",t.Kth(val,root));
break;
case 5: t.Split(root,val-1,x,y),printf("%d
",t.Find(x,1)/*t.Kth(sz[x],x)*/),root=t.Merge(x,y);
break;
case 6: t.Split(root,val/*以val分..*/,x,y),printf("%d
",t.Find(y,0)),root=t.Merge(x,y);
break;
}
}
return 0;
}
第三次写(2018.4.4):
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
const int N=1e5+5;
inline int read()
{
int now=0,f=1;register char c=gc();
for(;!isdigit(c);c=gc()) if(c=='-') f=-1;
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now*f;
}
namespace fhq_Treap
{
#define lson son[rt][0]
#define rson son[rt][1]
int tot,fa[N],son[N][2],sz[N],val[N],fix[N];
inline int New_Node(int v){
fix[++tot]=rand(), val[tot]=v, sz[tot]=1;
return tot;
}
inline void Update(int rt){
sz[rt]=sz[lson]+sz[rson]+1;
}
void Split(int rt,int v,int &x,int &y)
{
if(!rt) x=y=0;
else{
if(val[rt]<=v) x=rt,Split(rson,v,rson,y);//val[rt] not val[x]..
else y=rt,Split(lson,v,x,lson);
Update(rt);
}
}
int Merge(int x,int y)
{
if(!x||!y) return x^y;
if(fix[x]<fix[y]){
son[x][1]=Merge(son[x][1],y), Update(x);
return x;
}
else{
son[y][0]=Merge(x,son[y][0]), Update(y);
return y;
}
}
int Rank(int k,int rt)
{
while(1)
{
if(sz[lson]+1==k) return rt;
if(sz[lson]<k) k-=sz[lson]+1,rt=rson;
else rt=lson;
}
}
int Find(int rt,int w)
{
while(son[rt][w]) rt=son[rt][w];
return rt;
}
}
using namespace fhq_Treap;
int main()
{
int n=read(),opt,v,x,y,z,root=0;
while(n--)
switch(opt=read(),v=read(),opt)
{
case 1: Split(root,v,x,y),x=Merge(x,New_Node(v)),root=Merge(x,y);
break;
case 2: Split(root,v,x,z),Split(x,v-1,x,y),y=Merge(son[y][0],son[y][1]),root=Merge(Merge(x,y),z);
break;
case 3: Split(root,v-1,x,y),printf("%d
",sz[x]+1),root=Merge(x,y);
break;
case 4: printf("%d
",val[Rank(v,root)]);
break;
case 5: Split(root,v-1,x,y),printf("%d
",val[Find(x,1)]),root=Merge(x,y);
break;
case 6: Split(root,v,x,y),printf("%d
",val[Find(y,0)]),root=Merge(x,y);
break;
}
return 0;
}