类似于1014,用splay维护这个序列,维护每个节点为根的子树的hash值,对于一个询问二分答案判断就行了。
反思:询问的时候因为是原序列的x,y,所以开始的时候直接splay(x-1)了,后来发现这是不对的,因为可能在x前插入一些东西,所以需要麻烦些,先splay(x),然后提出来右端点为size[son[rot][0]]+1+len,然后再splay(find(size[son[rot][0]]+1))。
/**************************************************************
Problem: 2258
User: BLADEVIL
Language: C++
Result: Accepted
Time:7904 ms
Memory:3640 kb
****************************************************************/
//By BLADEVIL
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 100010
using namespace std;
char s[maxn];
int fac[maxn],key[maxn],num,rot,son[maxn][],father[maxn],size[maxn],hash[maxn];
void update(int x) {
if (!x) return ;
hash[x]=hash[son[x][]]+(key[x]+hash[son[x][]]*)*fac[size[son[x][]]];
size[x]=size[son[x][]]+size[son[x][]]+;
}
int build(int l,int r) {
int mid=l+r>>,left=,right=;
if (mid<r) right=build(mid+,r);
if (mid>l) left=build(l,mid-);
father[left]=father[right]=mid;
son[mid][]=left; son[mid][]=right;
update(mid);
return mid;
}
void rotate(int x,int &rot) {
int y=father[x],z=father[y];
int p=(son[y][]==x),q=p^;
if (y==rot) rot=x; else if (son[z][]==y) son[z][]=x; else son[z][]=x;
father[x]=z; father[y]=x; father[son[x][q]]=y;
son[y][p]=son[x][q]; son[x][q]=y;
update(y);
}
void splay(int x,int &rot) {
while (x!=rot) {
int y=father[x],z=father[y];
if (y!=rot)
if ((son[y][]==x)^(son[z][]==y)) rotate(x,rot); else rotate(y,rot);
rotate(x,rot);
}
update(x);
}
int find(int x) {
int t=rot;
while () {
if (size[son[t][]]+==x) return t; else
if (size[son[t][]]+>x) t=son[t][]; else
if (size[son[t][]]+<x) x-=size[son[t][]]+,t=son[t][];
}
}
bool judge(int x,int y,int len) {
if (len==) return key[x+]==key[y+];
int p=x+; splay(p,rot);
int q=find(size[son[rot][]]++len);
splay(find(size[son[rot][]]),rot); splay(q,son[rot][]);
int a1=hash[son[q][]];
p=y+; splay(p,rot);
q=find(size[son[rot][]]++len);
splay(find(size[son[rot][]]),rot); splay(q,son[rot][]);
int a2=hash[son[q][]];
return a1==a2;
}
int main() {
scanf("%s",s); num=strlen(s);
fac[]=; for (int i=;i<maxn;i++) fac[i]=fac[i-]*;
for (int i=;i<=num+;i++) key[i]=s[i-]-'a'+; num+=;
rot=build(,num);
int task; scanf("%d",&task);
while (task--) {
int x,y;
scanf("%s",s);
if (s[]=='Q') {
scanf("%d%d",&x,&y);
if (x>y) swap(x,y);
splay(y+,rot);
int l=,r=size[son[rot][]],mid,ans=;
while (l<=r) {
mid=l+r>>;
if (judge(x,y,mid)) ans=mid, l=mid+; else r=mid-;
}
printf("%d\n",ans);
} else
if (s[]=='I') {
scanf("%s%d",s,&x);
x=(x>num-)?num:x;
key[++num]=s[]-'a'+;
int p=find(x); splay(p,rot);
int q=find(x+); splay(q,son[rot][]);
father[num]=q; son[q][]=num;
splay(num,rot);
}
}
return ;
}