用Splay维护整个字符串,然而并不会求LCQ……后来想了半天,发现可以把字符串哈希起来,然后每次二分答案判断一下即可。就是询问复杂度多了个log,不过看询问操作的个数不超过10,000个,应该还是不虚的。不过,为什么我写的Splay常数都这么大啊……压着时间过去的……
1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<algorithm>
5 #include<cmath>
6 #define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
7 #define maxn 100010
8 #define sed 31
9
10 using namespace std;
11 typedef unsigned long long llg;
12
13 int n,m,val[maxn],s[maxn][2],fa[maxn],siz[maxn],rt,tt;
14 llg ha[maxn],mi[maxn]; char ss[maxn];
15
16 int getint(){
17 int w=0;bool q=0;
18 char c=getchar();
19 while((c>‘9‘||c<‘0‘)&&c!=‘-‘) c=getchar();
20 if(c==‘-‘) c=getchar(),q=1;
21 while(c>=‘0‘&&c<=‘9‘) w=w*10+c-‘0‘,c=getchar();
22 return q?-w:w;
23 }
24
25 void update(int u){
26 int l=s[u][0],r=s[u][1];
27 ha[u]=ha[l]*sed+val[u];
28 ha[u]=ha[u]*mi[siz[r]]+ha[r];
29 siz[u]=siz[l]+siz[r]+1;
30 }
31
32 int build(int l,int r){
33 int mid=l+r>>1,now=++tt; val[now]=ss[mid]-‘a‘;
34 if(l<mid) s[now][0]=build(l,mid-1),fa[s[now][0]]=now;
35 if(mid<r) s[now][1]=build(mid+1,r),fa[s[now][1]]=now;
36 update(now); return now;
37 }
38
39 void R(int u,int &k){
40 int p=fa[u],g=fa[p];
41 bool l= u==s[p][1],r=!l;
42 if(p==k) k=u;
43 else s[g][p==s[g][1]]=u;
44 fa[u]=g; if(s[u][r]) fa[s[u][r]]=p;
45 s[p][l]=s[u][r]; fa[p]=u; s[u][r]=p;
46 update(p); update(u);
47 }
48
49 void splay(int u,int &k){
50 while(u!=k){
51 int p=fa[u],g=fa[p];
52 if(p!=k){
53 if((u==s[p][1])^(p==s[g][1])) R(u,k);
54 else R(p,k);
55 }
56 R(u,k);
57 }
58 }
59
60 int fin(int x){
61 int u=rt;
62 while(x!=siz[s[u][0]]+1){
63 if(x<=siz[s[u][0]]) u=s[u][0];
64 else x-=siz[s[u][0]]+1,u=s[u][1];
65 }
66 return u;
67 }
68
69 llg split(int l,int r){
70 splay(fin(l+1),rt);
71 splay(fin(r+3),s[rt][1]);
72 return ha[s[s[rt][1]][0]];
73 }
74
75 int work(){
76 int x=getint(),y=getint(),l,r,mid;
77 l=0,r=n-max(x,y)+1;
78 while(l!=r){
79 mid=l+r+1>>1;
80 if(split(x,x+mid-1)==split(y,y+mid-1)) l=mid;
81 else r=mid-1;
82 }
83 return l;
84 }
85
86 void solve(int x){
87 char c=getchar();int u;
88 while(c<‘a‘ || c>‘z‘) c=getchar();
89 val[u=fin(x+2)]=c-‘a‘;
90 while(u) update(u),u=fa[u];
91 }
92
93 void insert(int x){
94 char c=getchar();int u,v;
95 while(c<‘a‘ || c>‘z‘) c=getchar();
96 n++; split(x,x);
97 u=s[s[rt][1]][0]; val[v=++tt]=c-‘a‘;
98 fa[v]=u; s[u][1]=v;
99 while(v) update(v),v=fa[v];
100 }
101
102 int main(){
103 File("a");
104 scanf("%s",ss+2); n=strlen(ss+2);
105 ss[0]=ss[1]=ss[n+2]=‘a‘; mi[0]=1;
106 for(int i=1;i<maxn;i++) mi[i]=mi[i-1]*sed;
107 m=getint(); rt=build(0,n+2);
108 while(m--){
109 char c=getchar();
110 while(c!=‘Q‘ && c!=‘R‘ && c!=‘I‘) c=getchar();
111 if(c==‘Q‘) printf("%d\n",work());
112 else if(c==‘R‘) solve(getint());
113 else insert(getint());
114 }
115 }