背景:
整整一天。
题目传送门:
https://www.luogu.org/problemnew/show/P2596
题意:
n
n
n本书,
5
5
5中操作。
1.
1.
1.把某本书放在最上面;
2.
2.
2.把某本书放在最下面;
3.
3.
3.把这本书放在原位置的附近(
±
1
\pm1
±1内);
4.
4.
4.询问某本书上面有多少本书;
5.
5.
5.询问自上而下某本书的编号。
思路:
鉴于这里有两个值(编号和高度)要维护,不妨考虑使 s p l a y splay splay中第 i i i个点维护编号为 i i i的书的高度。
考虑操作 1 , 2 1,2 1,2,由于这道题并不需要知道确切的高度(只需要知道相对高度的排名),因此有简单的维护方法。最上面和最下面对应的就是高度最小和最大,每一次将这本书删除,再在同一编号的节点塞入最小或最大值(高度)即可。
考虑操作 3 3 3,其实就是交换前驱或后继的高度,但是由于 s p l a y splay splay并不支持自动维护成平衡树,因此要先删除后加入(注意要先删完,在加入,不然就会有两个高度相同的点在 s p l a y splay splay中)。
操作
4
,
5
4,5
4,5其实就是模板,询问某个点的排名和排名为某个值的编号。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node1{int d,fa,c,son[2];} tr[200010];
struct node2{int x,id;} a[200010];
int n,m,root=0,len=0,mi=1,ma;
bool cmp(node2 x,node2 y)
{
return x.x<y.x;
}
void ups(int x)
{
tr[x].c=tr[tr[x].son[0]].c+tr[tr[x].son[1]].c+1;
}
void add(int d,int fa)
{
tr[++len]=(node1){d,fa,1,{0,0}};
}
void rot(int x,int w)//左旋:0,右旋:1
{
int y=tr[x].fa,yy=tr[y].fa;
tr[y].son[1^w]=tr[x].son[w];
if(tr[x].son[w]) tr[tr[x].son[w]].fa=y;
tr[yy].son[tr[yy].son[0]==y?0:1]=x;
tr[x].fa=yy,tr[x].son[w]=y,tr[y].fa=x;
ups(y),ups(x);
}
void splay(int x,int rt)
{
while(tr[x].fa!=rt)
{
int y=tr[x].fa,yy=tr[y].fa;
if(yy==rt)
{
rot(x,tr[y].son[0]==x?1:0);
continue;
}
if(tr[yy].son[0]==y)
{
if(tr[y].son[0]==x) rot(y,1),rot(x,1); else rot(x,0),rot(x,1);
continue;
}
if(tr[y].son[1]==x) rot(y,0),rot(x,0); else rot(x,1),rot(x,0);
}
if(!rt) root=x;
}
int findip(int x,int d)
{
if(d==tr[x].d) return x;
if(d<tr[x].d)
return tr[x].son[0]?findip(tr[x].son[0],d):x;
else
return tr[x].son[1]?findip(tr[x].son[1],d):x;
}
void ins(int d)
{
if(!root)
{
add(d,0);
root=1;
return;
}
int u=findip(root,d);
add(d,u);
tr[u].son[d<tr[u].d?0:1]=len;
ups(u);
splay(len,0);
}
void ins2(int x,int d)
{
int u=findip(root,d);
tr[x]=(node1){d,u,1,{0,0}};
tr[u].son[d<tr[u].d?0:1]=x;
ups(u);
splay(x,0);
}
void del(int u)
{
splay(u,0);
if(!tr[u].son[0]&&tr[u].son[1])
{
root=tr[u].son[1];
tr[root].fa=0;
tr[u]=(node1){0,0,0,{0,0}};
return;
}
if(tr[u].son[0]&&!tr[u].son[1])
{
root=tr[u].son[0];
tr[root].fa=0;
tr[u]=(node1){0,0,0,{0,0}};
return;
}
int o=tr[u].son[0];
while(tr[o].son[1]) o=tr[o].son[1];
splay(o,u);
tr[o].son[1]=tr[u].son[1];
tr[tr[o].son[1]].fa=o;
root=o;
tr[o].fa=0;
ups(o);
tr[u]=(node1){0,0,0,{0,0}};
}
int findnum(int x,int d)
{
int lc=tr[x].son[0],rc=tr[x].son[1];
if(d<=tr[lc].c) return findnum(lc,d);
else if(d>tr[lc].c+1) return findnum(rc,d-tr[lc].c-1);
else return x;
}
int findrank(int x)
{
splay(x,0);
return tr[tr[x].son[0]].c;
}
void work_T(int x)
{
del(x);
ins2(x,--mi);
}
void work_B(int x)
{
del(x);
ins2(x,++ma);
}
int findqianqu(int u)
{
splay(u,0);
u=tr[u].son[0];
while(tr[u].son[1]) u=tr[u].son[1];
return u;
}
int findhouji(int u)
{
splay(u,0);
u=tr[u].son[1];
while(tr[u].son[0]) u=tr[u].son[0];
return u;
}
void work(int x,int y)
{
if(!y) return;
int u;
if(y==-1) u=findqianqu(x); else u=findhouji(x);
int d1=tr[u].d,d2=tr[x].d;
del(x),del(u);
ins2(x,d1),ins2(u,d2);
}
int main()
{
int x,y;
char s[10];
scanf("%d %d",&n,&m);
ma=n;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i].x);
a[i].id=i;
}
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++)
ins(a[i].id);
for(int i=1;i<=m;i++)
{
scanf("%s",s+1);
if(s[1]=='T')
{
scanf("%d",&x);
work_T(x);
}
else if(s[1]=='B')
{
scanf("%d",&x);
work_B(x);
}
else if(s[1]=='I')
{
scanf("%d %d",&x,&y);
work(x,y);
}
else if(s[1]=='A')
{
scanf("%d",&x);
printf("%d\n",findrank(x));
}
else if(s[1]=='Q')
{
scanf("%d",&x);
printf("%d\n",findnum(root,x));
}
}
}