很久以前xzz大佬就喊我做这题,结果现在才做qwq
因为要在序列中插入,所以直接用(Splay)维护这个串的哈希值,插入就直接把那个点插♂进去,修改就把点旋到根,然后修改和pushup,询问的话可以考虑二分,check就看两个对应区间哈希值是否相等
#include<bits/stdc++.h>
#define LL long long
#define il inline
#define re register
#define ull unsigned long long
using namespace std;
const int N=150000+10;
il int rd()
{
int x=0,w=1;char ch=0;
while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
return x*w;
}
int n,q;
char cc[N];
int rt,tt,fa[N],ch[N][2],sz[N];
ull a[N],ha[N],bs=377,bb[N];
il void psup(int x)
{
sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;
ha[x]=ha[ch[x][0]]*bb[sz[ch[x][1]]+1]+a[x]*bb[sz[ch[x][1]]]+ha[ch[x][1]];
}
il void rot(int x)
{
int y=fa[x],z=fa[y],yy=ch[y][1]==x,w=ch[x][!yy];
ch[z][ch[z][1]==y]=x;fa[x]=z;
ch[y][yy]=w;fa[w]=y;
ch[x][!yy]=y,fa[y]=x;
psup(y),psup(x);
}
il void spl(int x,int en)
{
if(!x) return;
while(fa[x]!=en)
{
int y=fa[x],z=fa[y];
if(z!=en) ((ch[y][1]==x)^(ch[z][1]==y))?rot(x):rot(y);
rot(x);
}
if(!en) rt=x;
}
il int gkth(int x)
{
if(x<=0||x>n+2) return 0;
int nw=rt;
while(233)
{
if(x<=sz[ch[nw][0]]) nw=ch[nw][0];
else if(x>sz[ch[nw][0]]+1) x-=sz[ch[nw][0]]+1,nw=ch[nw][1];
else return nw;
}
}
il int bui(int l,int r)
{
if(l>r) return 0;
int mid=(l+r)>>1,nw=++tt;
sz[nw]=1,a[nw]=(cc[mid-1]>='a')?(ull)(cc[mid-1]-'a'):0;
fa[ch[nw][0]=bui(l,mid-1)]=nw;
fa[ch[nw][1]=bui(mid+1,r)]=nw;
psup(nw);
return nw;
}
int main()
{
bb[0]=1;
for(int i=1;i<=150000;++i) bb[i]=bb[i-1]*bs;
scanf("%s",cc+1);
n=strlen(cc+1);
rt=bui(1,n+2);
q=rd();
char ss[2];
while(q--)
{
scanf("%s",ss);
if(ss[0]=='Q')
{
int x=rd(),y=rd();
if(x>y) swap(x,y);
int ll=gkth(x),rr=gkth(y),l=1,r=n-y+1,an=0;
while(l<=r)
{
int mid=(l+r)>>1;
ull h1,h2;
int zz=gkth(x+mid+1);//x+mid-1+2
spl(ll,0),spl(zz,ll),h1=ha[ch[zz][0]];
zz=gkth(y+mid+1);
spl(rr,0),spl(zz,rr),h2=ha[ch[zz][0]];
if(h1==h2) an=mid,l=mid+1;
else r=mid-1;
}
printf("%d
",an);
}
else if(ss[0]=='R')
{
int x=gkth(rd()+1);
scanf("%s",ss);
spl(x,0),a[x]=ss[0]-'a',psup(x);
}
else
{
int x=gkth(rd()+1),nw=++tt;
++n;
scanf("%s",ss);
spl(x,0);
a[nw]=ss[0]-'a',fa[nw]=x;
fa[ch[x][1]]=nw,ch[nw][1]=ch[x][1],ch[x][1]=nw;
psup(nw),psup(x);
}
}
return 0;
}