splay瞎搞一下,正解是分块数组或分块链表,但是学不会啊!
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<string>
#include<set>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<cmath>
#include<cstring>
#include<map>
#include<stack>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 2005
#define ull unsigned long long
#define ll long long
#define hashmod 99999839
#define mod 9997
int son[maxn][],fa[maxn],sz[maxn],root,len,q;
char s[maxn],v[maxn],ch;
//kpmsrzxxzu
inline void update(int o){
sz[o] = ;
if(son[o][] != -) sz[o] += sz[son[o][]];
if(son[o][] != -) sz[o] += sz[son[o][]];
}
inline int isrson(int p,int f){
if(f == -) return ;
return son[f][] == p;
}
inline void changeson(int p,int f,int v){
if(f != -) son[f][v] = p;
if(p != -) fa[p] = f;
if(f == -) root = p;
}
int build(int l,int r){
if(l > r) return -;
int o = len;
len++;
if(l == r){son[o][] = son[o][] = -,sz[o] = ,v[o] = s[l];return o;}
int mid = (l + r) >> ,ls,rs;
v[o] = s[mid],ls = build(l,mid-),rs = build(mid+,r);
changeson(ls,o,),changeson(rs,o,),update(o);
return o;
}
void rotate(int p){//只会出现祖辈不存在的情况
int f = fa[p],g = fa[f];
int u = isrson(p,f),v = isrson(f,g);
changeson(son[p][u^],f,u),changeson(f,p,u^),changeson(p,g,v);
update(f);
}
void splay(int p,int tar){
if(fa[p] == tar) return;
while(fa[p] != tar && fa[fa[p]] != tar){
int f = fa[p],g = fa[f];//保证了父亲及其祖先存在
int u = isrson(p,f),v = isrson(f,g);
if(u ^ v) rotate(p),rotate(p);
else rotate(f),rotate(p);
}
if(fa[p] != tar) rotate(p);
update(p);
}
int findtr(){
int p = root,ls = son[p][],rs = son[p][];
if(q >= sz[p]) q = sz[p] - ;
while(){
if(ls == -){
if(q == ) break;
else p = rs,q--;
ls = son[p][],rs = son[p][];
continue;
}
if(sz[ls] >= q) p = ls;
else if(sz[ls] + == q) break;
else p = rs,q = q - sz[ls] - ;
ls = son[p][],rs = son[p][];
}
return p;
}
void insert(){
int p = findtr();
splay(p,-);
int o = len;
len++,v[o] = ch;
changeson(root,o,),changeson(son[root][],o,);
son[root][] = -,fa[o] = -;
update(root);
root = o;
update(root);
}
void query(){
int p = findtr();
printf("%c\n",v[p]);
splay(p,-);
}
signed main(){
// freopen("a.in","r",stdin);
// freopen("b.out","w",stdout);
// int k = 0;
while(~scanf("%s",s + )){
int t = strlen(s + );
len = ;
s[] = '\0',s[t + ] = '\0';
root = build(,t + );
fa[root] = -;
int n;
char op[];
scanf("%d",&n);
// printf("Case %d:\n",++k);
for(register int i = ;i <= n;++i){
scanf("%s",op);
if(op[] == 'Q') scanf("%d",&q),q++,query();
else scanf("%s%d",op,&q),ch = op[],insert();
}
}
return ;
}