简单的Splay操作,只要加一个书的编号跟在树中的位置映射就可以了
1. Top S——表示把编号为S的书房在最上面。
2. Bottom S——表示把编号为S的书房在最下面。
3. Insert S T——T∈{-1,0,1},若编号为S的书上面有X本书,则这条命令表示把这本书放回去后它的上面有X+T本书;
4. Ask S——询问编号为S的书的上面目前有多少本书。
5. Query S——询问从上面数起的第S本书的编号。
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
#define Key_value ch[ch[root][1]][0]
int N,M;
map<int,int> mp;
int a[maxn];
int ch[maxn][2],pre[maxn],size[maxn],key[maxn];
int root,tot1,tot2,s[maxn];
void NewNode(int &r,int f,int val)
{
if(tot2)r=s[tot2--];
else r=++tot1;
ch[r][0]=ch[r][1]=0;
pre[r]=f;
key[r]=val;
mp[val]=r;
}
void pushup(int r)
{
size[r]=size[ch[r][1]]+size[ch[r][0]]+1;
}
void build(int &x,int l,int r,int fa)
{
if(l>r)return;
int mid=(l+r)>>1;
NewNode(x,fa,a[mid]);
build(ch[x][0],l,mid-1,x);
build(ch[x][1],mid+1,r,x);
pushup(x);
}
void init()
{
root=tot1=tot2=0;
ch[root][0]=ch[root][1]=key[root]=size[root]=0;
NewNode(root,0,-1);
NewNode(ch[root][1],root,-1);
build(Key_value,1,N,ch[root][1]);
pushup(ch[root][1]);
pushup(root);
}
void Rotate(int x,int kind)
{
int y=pre[x];
ch[y][!kind]=ch[x][kind];
pre[ch[x][kind]]=y;
if(pre[y])ch[pre[y]][ch[pre[y]][1]==y]=x;
pre[x]=pre[y];
ch[x][kind]=y;
pre[y]=x;
pushup(y);
}
void Splay(int r,int goal)
{
while(pre[r]!=goal)
{
if(pre[pre[r]]==goal)
{
Rotate(r,ch[pre[r]][0]==r);
}
else
{
int y=pre[r];
int kind=(ch[pre[y]][0]==y);
if(ch[y][kind]==r)
{
Rotate(r,!kind);
Rotate(r,kind);
}
else
{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
pushup(r);
if(goal==0)root=r;
}
int get_max(int r)
{
while(ch[r][1])
{
r=ch[r][1];
}
return r;
}
void remove()
{
if(ch[root][0]==0)
{
root=ch[root][1];
pre[root]=0;
}
else
{
int m=get_max(ch[root][0]);
Splay(m,root);
ch[m][1]=ch[root][1];
pre[ch[root][1]]=m;
root=m;
pre[root]=0;
pushup(root);
}
}
int get_kth(int r,int k)
{
int t=size[ch[r][0]]+1;
if(t==k)return r;
if(t>k)return get_kth(ch[r][0],k);
return get_kth(ch[r][1],k-t);
}
void Top(int x)
{
Splay(mp[x],0);
int tmp=root;
remove();
Splay(get_kth(root,1),0);
Splay(get_kth(root,2),root);
pre[tmp]=ch[root][1];
Key_value=tmp;
ch[tmp][0]=ch[tmp][1]=0;
size[tmp]=1;
pushup(ch[root][1]);
pushup(root);
}
void Bottom(int x)
{
Splay(mp[x],0);
int tmp=root;
remove();
Splay(get_kth(root,N),0);
Splay(get_kth(root,N+1),root);
pre[tmp]=ch[root][1];
Key_value=tmp;
ch[tmp][0]=ch[tmp][1]=0;
size[tmp]=1;
pushup(ch[root][1]);
pushup(root);
}
void Insert(int S,int T)
{
Splay(mp[S],0);
int cnt=size[ch[root][0]];
int tmp=root;
remove();
Splay(get_kth(root,cnt+T),0);
Splay(get_kth(root,cnt+T+1),root);
pre[tmp]=ch[root][1];
Key_value=tmp;
ch[tmp][0]=ch[tmp][1]=0;
size[tmp]=1;
pushup(ch[root][1]);
pushup(root);
}
void Ask(int x)
{
Splay(mp[x],0);
printf("%d\n",size[ch[root][0]]-1);
}
void Query(int x)
{
Splay(get_kth(root,x+1),0);
printf("%d\n",key[root]);
}
int main()
{
while(scanf("%d%d",&N,&M)!=EOF)
{
for(int i=1;i<=N;i++)scanf("%d",&a[i]);
init();
char op[10];
int S,T;
while(M--)
{
scanf("%s%d",op,&S);
if(op[0]=='T') Top(S);
else if(op[0]=='B')Bottom(S);
else if(op[0]=='I')
{
scanf("%d",&T);
Insert(S,T);
}
else if(op[0]=='A')Ask(S);
else Query(S);
}
}
return 0;
}