题目大意:给你一个串,支持修改,插入,询问两点的lcp
用一个splay搞搞,每个点上维护hash值,询问就先二分,在比较hash值,可以过的
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long int64;
using namespace std;
const int maxn=100011,Index=131;
char s[maxn]; int n;
int64 power[maxn];
void init(){
scanf("%s",s+1); n=strlen(s+1); s[0]=s[n+1]=0;
}
struct Tspy{
struct node{
node *c[2],*f; int sz,n,v; int64 h;
void clear(){c[0]=0; c[1]=0; f=0; h=-1; sz=1; n=2;}
void update(){
if (c[0]) h=c[0]->h; else h=0; h=h*Index+v;
if (c[1]) h=h*power[c[1]->sz]+c[1]->h;
sz=1; if (c[0]) sz+=c[0]->sz; if (c[1]) sz+=c[1]->sz;
}
}*root,e[maxn];
int tot;
int getv(char ch){return ch-'a'+7;}
void clear(){tot=0; root=build(s,0,n+1); root->f=0;}
node* newnode(){e[tot].clear(); return e+(tot++);}
node* build(char *s,int l,int r){
int mid=(l+r)>>1; node *x=newnode();
if (mid-1>=l) x->c[0]=build(s,l,mid-1),x->c[0]->f=x;
if (mid+1<=r) x->c[1]=build(s,mid+1,r),x->c[1]->f=x;
if (x->c[0]) x->c[0]->n=0; if (x->c[1]) x->c[1]->n=1;
x->v=getv(s[mid]); x->update(); return x;
}
void rotate(node *x){
node *y=x->f,*z=y->f; int nx=x->n,ny=y->n;
x->n=ny; x->f=z; if (ny!=2) z->c[ny]=x;
z=x->c[1-nx];
y->f=x; y->n=1-nx; x->c[1-nx]=y;
if (z) z->f=y,z->n=nx; y->c[nx]=z; y->update();
}
void splay(node *x){
while (x->n!=2){
if (x->n==x->f->n) rotate(x->f); else rotate(x);
if (x->n!=2) rotate(x);
}
x->update(); root=x;
}
void modify(node *x,int v){
splay(x); x->v=v; x->update();
}
node* find(node *x,int rank){
for (;;){
int sz=0; if (x->c[0]) sz=x->c[0]->sz;
if (sz>=rank) {x=x->c[0]; continue;}
if (sz==rank-1) return x;
x=x->c[1]; rank-=sz+1;
}
}
void modify(int x,char ch){modify(find(root,x),getv(ch));}
void ins(int x,char ch){
node *a=find(root,x); ++n;
node *b=find(root,x+1);
splay(a); a->c[1]->n=2; a->c[1]->f=0; a->c[1]=0;
splay(b); node *c=newnode(); c->v=getv(ch);
b->f=c; b->n=1; c->c[1]=b; c->update();
c->f=a; c->n=1; a->c[1]=c; a->update();
root=a;
}
int64 geth(node *&x,node *&y){
splay(x); x->c[1]->f=0; x->c[1]->n=2; x->c[1]=0;
splay(y); int64 res=y->c[0]->h;
x->c[1]=y; y->f=x; y->n=1; x->update(); root=x;
return res;
}
bool check(node *&x,node *&y,node *&x1,node *&y1){
return geth(x,x1)==geth(y,y1);
}
int binary(int a,int b){
if (a>b) swap(a,b);
node *x=find(root,a-1),*y=find(root,b-1);
int t=1,w=n-b+2;
while (t<=w){
int mid=(t+w)>>1;
node *x1=find(root,a+mid),*y1=find(root,b+mid);
if (check(x,y,x1,y1)) t=mid+1; else w=mid-1;
}
return t-1;
}
void watch(node *x){
printf("Id=%d f=%d n=%d lc=%d rc=%d v=%d sz=%d\n",
x-e,x->f-e,x->n,x->c[0]-e,x->c[1]-e,x->v,x->sz);
if (x->c[0]) watch(x->c[0]); if (x->c[1]) watch(x->c[1]);
}
void watch(){watch(root);}
}t;
void prepare(){
power[0]=1; for (int i=1;i<maxn;++i) power[i]=power[i-1]*Index;
}
void work(){
prepare(); t.clear();
int m; scanf("%d",&m); char op[5];
for (int i=1;i<=m;++i){
scanf("%s",op);
if (op[0]=='Q'){
int a,b;
scanf("%d%d",&a,&b); ++a,++b;
printf("%d\n",t.binary(a,b));
}
if (op[0]=='I'){
int x; char ch[3];
scanf("%d%s",&x,ch); ++x; t.ins(x,ch[0]);
}
if (op[0]=='R'){
int x; char ch[3];
scanf("%d%s",&x,ch); ++x; t.modify(x,ch[0]);
}
}
}
int main(){
init();
work();
return 0;
}