题意:
一个书架有n本书,书本依次编号为1,2,。。。,n,初始有一个给定顺序从上到下摆放,有五种操作:
Top x:将编号为x的书置于最顶上
Bottom x:将编号为x的书置于最底下
Insert x y:将编号为x的书置于x的原位置+y的位置
Ask x:询问编号为x的书上面有多少本书
Query x:询问第x本书的编号
思路:
使用Splay伸展树维护,但是维护的操作都需要加一点细节进去,伸展树总体维护目标将第x本书维护在节点x上以书的标号为权值,但是每次操作都需要先删除再重新添加进伸展树,重新添加就会引进新标号,这个引进的信标号将会对应被抽出的那本书,然后重新赋予节点编号,再次维护进伸展树,也可以看作是删除原书,重新添加一本新书,只不过权值都是相同的而已
插入:因为要具体维护到某一位置,需要为插入多调个参数
插入函数如下:
void Insert( int now , int data , int k )
{
if ( k==1&&son[now][0]==0 )
Newnode( data , now ),son[now][0] = sizes;
else if ( k==2+Getsize(son[now][0])&&son[now][1]==0 )
Newnode( data , now ),son[now][1] = sizes;
else if ( k<=1+Getsize(son[now][0]) )
Insert( son[now][0] , data , k );
else
Insert( son[now][1] , data , k-Getsize(son[now][0])-1 );
Update( now );
}
查找某一节点位置:这样就可以知道节点x是第几本书
int Get( int x )
{
Splay( x , 0 );
return Getsize(son[x][0])+1;
}
查找指定位置数据:这样就可以知道第x本书的编号
int Find( int x )
{
int now = root;
while ( true )
{
if ( x==1+Getsize(son[now][0]) )
return now;
if ( x> 1+Getsize(son[now][0]) )
x -= 1+Getsize(son[now][0]),now = son[now][1];
else
now = son[now][0];
}
}
C++代码:
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<complex>
#include<iostream>
#include<algorithm>
using namespace std;
#define lson rt<<1
#define rson rt<<1|1
typedef double DB;
typedef long long LL;
typedef complex<double>CD;
const int maxn = 200010;
const int mod = 1000000;
const int inf = 0x3f3f3f3f;
const int INF = 0x7FFFFFFF;
void Mod( int &x ){ x%=mod; }
int Min( int a , int b ){ return a<b?a:b; }
int Max( int a , int b ){ return a>b?a:b; }
int Abs( int x ){ return x>=0?x:-x; }
int Random(){ static int seed = 703; return seed=(int)(seed*48271LL%2147483647); }
int fa[maxn],son[maxn][2],val[maxn],sz[maxn],a[maxn],pos[maxn],sizes,root;
void Update( int x )
{
sz[x] = 1;
if ( son[x][0]!=0 )
sz[x] += sz[son[x][0]];
if ( son[x][1]!=0 )
sz[x] += sz[son[x][1]];
}
void Newnode( int data , int father )
{
sizes++;
sz[sizes] = 1;
fa[sizes] = father;
son[sizes][0] = 0;
son[sizes][1] = 0;
val[sizes] = data;
}
int Getsize( int x )
{
if ( x==0 )
return 0;
else
return sz[x];
}
void Rotate( int x , int kind )
{
int y = fa[x];
int z = fa[y];
son[y][1-kind] = son[x][kind];
if ( son[x][kind]!=0 )
fa[son[x][kind]] = y;
fa[x] = z;
if ( z!=0 )
son[z][son[z][1]==y] = x;
fa[y] = x,son[x][kind] = y;
Update( y );
Update( x );
}
void Splay( int x , int ace )
{
while ( fa[x]!=ace )
{
int y = fa[x];
int z = fa[y];
if ( z==ace )
Rotate( x , son[y][1]!=x );
else
{
if ( son[y][1]==x&&son[z][1]==y )
Rotate( y , 0 ),Rotate( x , 0 );
else if ( son[y][1]==x&&son[z][0]==y )
Rotate( x , 0 ),Rotate( x , 1 );
else if ( son[y][0]==x&&son[z][0]==y )
Rotate( y , 1 ),Rotate( x , 1 );
else
Rotate( x , 1 ),Rotate( x , 0 );
}
}
if ( ace==0 )
root = x;
}
void Insert( int now , int data , int k )
{
if ( k==1&&son[now][0]==0 )
Newnode( data , now ),son[now][0] = sizes;
else if ( k==2+Getsize(son[now][0])&&son[now][1]==0 )
Newnode( data , now ),son[now][1] = sizes;
else if ( k<=1+Getsize(son[now][0]) )
Insert( son[now][0] , data , k );
else
Insert( son[now][1] , data , k-Getsize(son[now][0])-1 );
Update( now );
}
void Delete( int x )
{
Splay( x , 0 );
if ( son[x][0]==0&&son[x][1]==0 )
root = 0;
else if ( son[x][0]==0 )
root = son[x][1],fa[son[x][1]] = 0;
else if ( son[x][1]==0 )
root = son[x][0],fa[son[x][0]] = 0;
else
{
int y = son[x][0];
while ( son[y][1] )
y = son[y][1];
fa[son[x][0]] = 0;
Splay( y , 0 );
son[y][1] = son[x][1];
fa[son[x][1]] = y;
root = y;
Update( y );
}
}
int Find( int x )
{
int now = root;
while ( true )
{
if ( x==1+Getsize(son[now][0]) )
return now;
if ( x> 1+Getsize(son[now][0]) )
x -= 1+Getsize(son[now][0]),now = son[now][1];
else
now = son[now][0];
}
}
int Get( int x )
{
Splay( x , 0 );
return Getsize(son[x][0])+1;
}
int main()
{
for ( int n,m,t ; scanf ( "%d%d" , &n , &m )==2 ; /*printf ("End\n")*/ )
{
for ( int i=1 ; i<=n ; i++ )
scanf ( "%d" , &a[i] ),pos[a[i]] = i;
sizes = 0;
for ( int i=1 ; i<=n ; i++ )
{
if ( i==1 )
Newnode( a[i] , 0 ),root = sizes;
else
Insert( root , a[i] , i ),Splay( sizes , 0 );
}
for ( int i=1 ; i<=m ; i++ )
{
char s[15]; int x,y;
scanf ( "%s%d" , s , &x );
if ( s[0]=='I' )
scanf ( "%d" , &y );
if ( s[0]=='T' )
Delete( pos[x] ),Insert( root , x , 1 ),pos[x] = sizes,Splay( sizes , 0 );
else if ( s[0]=='B' )
Delete( pos[x] ),Insert( root , x , n ),pos[x] = sizes,Splay( sizes , 0 );
else if ( s[0]=='I' )
t = Get(pos[x]),t = t+y,Delete(pos[x]),Insert( root , x , t ),Splay( sizes , 0 ),pos[x] = sizes;
else if ( s[0]=='A' )
printf ( "%d\n" , Get(pos[x])-1 );
else
printf ( "%d\n" , val[Find(x)] );
}
}
return 0;
}