题意:给一个1到n的序列,然后有三种操作,每次的rank及query输出某个值
思路:题目需要将某个数放到最前面,用splay来完成,对于top,将这个节点旋转到根节点,删除后加到最前面,然后query和rank的都比较简单,代码参考kuangbin大神
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3fll;
const int maxn=400010;
int pre[maxn],ch[maxn][2],size[maxn],root,tot1;//父节点,左右孩子,子树规模,根节点,节点数量
int key[maxn],flag[maxn],rev[maxn],m[maxn];//该点的值,懒惰标记
int a[maxn],n,q,cnt;
int s[maxn],tot2,num[maxn],tmp[maxn],st[maxn],en[maxn];
void newnode(int &r,int fa,int k){
r=k;
pre[r]=fa;num[r]=size[r]=en[k]-st[k]+1;
ch[r][0]=ch[r][1]=0;
}
void pushup(int r){
size[r]=size[ch[r][0]]+size[ch[r][1]]+num[r];
}
void buildtree(int &x,int l,int r,int fa){
if(l>r) return ;
int mid=(l+r)>>1;
newnode(x,fa,mid);
buildtree(ch[x][0],l,mid-1,x);
buildtree(ch[x][1],mid+1,r,x);
pushup(x);
}
void init(){
root=0;
ch[root][0]=ch[root][1]=size[root]=0,num[root]=0;pre[root]=0;
buildtree(root,1,cnt,0);
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);
}
}
if(goal==0) root=r;
}
int get_min(int r){
while(ch[r][0]){
r=ch[r][0];
}
return r;
}
void Remove(){
if(ch[root][0]==0||ch[root][1]==0){
root=ch[root][0]+ch[root][1];
pre[root]=0;
}else{
int k=get_min(ch[root][1]);
splay(k,root);
ch[ch[root][1]][0]=ch[root][0];
root=ch[root][1];
pre[ch[root][0]]=root;
pre[root]=0;
pushup(root);
}
}
int slove(int x){
int le=1,ri=cnt;
while(ri>=le){
int mid=(le+ri)>>1;
if(st[mid]<=x&&x<=en[mid]) return mid;
if(x<st[mid]) ri=mid-1;
else le=mid+1;
}
}
void Top(int x){
int pos=slove(x);
splay(pos,0);Remove();
splay(get_min(root),0);
ch[pos][0]=0;ch[pos][1]=root;
pre[root]=pos;
root=pos;pre[root]=0;
pushup(root);
}
int Query(int x){
int pos=slove(x);
splay(pos,0);
return size[ch[root][0]]+1;
}
int Rank(int r,int k){
int t=size[ch[r][0]];
if(k<=t) return Rank(ch[r][0],k);
else if(k<=t+num[r]) return st[r]+k-t-1;
else return Rank(ch[r][1],k-(t+num[r]));
}
char str[100010][10];
int Q[100010];
int main(){
char op[10];
int x,y,z,T,cas=1;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&q);
int kkk=0;
for(int i=0;i<q;i++){
scanf("%s%d",str[i],&Q[i]);
if(str[i][0]!='R') tmp[kkk++]=Q[i];
}
tmp[kkk++]=1;tmp[kkk++]=n;
sort(tmp,tmp+kkk);
kkk=unique(tmp,tmp+kkk)-tmp;
cnt=1;
st[cnt]=tmp[0];en[cnt]=tmp[0];
for(int i=1;i<kkk;i++){
if(tmp[i]-tmp[i-1]>1){
cnt++;
st[cnt]=tmp[i-1]+1;
en[cnt]=tmp[i]-1;
}
cnt++;
st[cnt]=tmp[i];en[cnt]=tmp[i];
}
init();
printf("Case %d:\n",cas++);
for(int i=0;i<q;i++){
if(str[i][0]=='T') Top(Q[i]);
else if(str[i][0]=='Q') printf("%d\n",Query(Q[i]));
else printf("%d\n",Rank(root,Q[i]));
}
}
return 0;
}