BZOJ 1014: [JSOI2008]火星人prefix Splay+二分

时间:2024-03-26 15:05:56

1014: [JSOI2008]火星人prefix

题目连接:

http://www.lydsy.com/JudgeOnline/problem.php?id=1014

Description

火星人最近研究了一种操作:求一个字串两个后缀的公共前缀。比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 8 9 10 11 字符 m a d a m i m a d a m 现在,火星人定义了一个函数LCQ(x, y),表示:该字符串中第x个字符开始的字串,与该字符串中第y个字符开始的字串,两个字串的公共前缀的长度。比方说,LCQ(1, 7) = 5, LCQ(2, 10) = 1, LCQ(4, 7) = 0 在研究LCQ函数的过程中,火星人发现了这样的一个关联:如果把该字符串的所有后缀排好序,就可以很快地求出LCQ函数的值;同样,如果求出了LCQ函数的值,也可以很快地将该字符串的后缀排好序。 尽管火星人聪明地找到了求取LCQ函数的快速算法,但不甘心认输的地球人又给火星人出了个难题:在求取LCQ函数的同时,还可以改变字符串本身。具体地说,可以更改字符串中某一个字符的值,也可以在字符串中的某一个位置插入一个字符。地球人想考验一下,在如此复杂的问题中,火星人是否还能够做到很快地求取LCQ函数的值。

Input

第一行给出初始的字符串。第二行是一个非负整数M,表示操作的个数。接下来的M行,每行描述一个操作。操作有3种,如下所示: 1、 询问。语法:Q x y,x, y均为正整数。功能:计算LCQ(x, y) 限制:1 <= x, y <= 当前字符串长度。 2、 修改。语法:R x d,x是正整数,d是字符。功能:将字符串中第x个数修改为字符d。限制:x不超过当前字符串长度。 3、 插入:语法:I x d,x是非负整数,d是字符。功能:在字符串第x个字符之后插入字符d,如果x = 0,则在字符串开头插入。限制:x不超过当前字符串长度。

Output

对于输入文件中每一个询问操作,你都应该输出对应的答案。一个答案一行。

Sample Input

madamimadam

7

Q 1 7

Q 4 8

Q 10 11

R 3 a

Q 1 7

I 10 a

Q 2 11

Sample Output

5

1

0

2

1

Hint

数据规模:

对于100%的数据,满足:

1、 所有字符串自始至终都只有小写字母构成。

2、 M <= 150,000

3、 字符串长度L自始至终都满足L <= 100,000

4、 询问操作的个数不超过10,000个。

对于第1,2个数据,字符串长度自始至终都不超过1,000

对于第3,4,5个数据,没有插入操作。

题意

题解:

用splay去维护就好了

询问就直接暴力二分判断就行了

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
const int mod = 1e9+7;
const int P = 27; struct Splay
{
int fa[maxn];
int ch[maxn][2];
int val[maxn];
int siz[maxn];
long long h[maxn];
long long p[maxn];
int root,tot,n;
char s[maxn];
void newnode(int &r,int father,int k)//建立新的节点
{
r=++tot;
fa[r]=father;
val[r]=k;
ch[r][0]=ch[r][1]=0;
siz[r]=1;
}
void push_up(int x)
{
int l=ch[x][0],r=ch[x][1];
siz[x]=siz[l]+siz[r]+1;
h[x]=h[l]*p[siz[r]+1]+val[x]*p[siz[r]]+h[r];
h[x]%=mod;
}
void zig(int x)
{
int y=fa[x];
ch[y][0]=ch[x][1];
fa[ch[x][1]]=y;
ch[x][1]=y;
fa[x]=fa[y];
if(y==ch[fa[y]][0])
ch[fa[y]][0]=x;
else if(y==ch[fa[y]][1])
ch[fa[y]][1]=x;
fa[y]=x;
push_up(y);
if(y==root)root=x;
}
void zag(int x)
{
int y=fa[x];
ch[y][1]=ch[x][0];
fa[ch[x][0]]=y;
ch[x][0]=y;
fa[x]=fa[y];
if(y==ch[fa[y]][0])
ch[fa[y]][0]=x;
else if(y==ch[fa[y]][1])
ch[fa[y]][1]=x; fa[y]=x;
push_up(y);
if(y==root)root=x;
}
void splay(int x,int goal)
{
while(fa[x]!=goal)
{
int y=fa[x],z=fa[y];
if(z==goal)
{
if(x==ch[y][0])
zig(x);
else
zag(x);
break;
}
if(x==ch[y][0])
{
if(y==ch[z][0])
zig(y);
zig(x);
}
else
{
if(y==ch[z][1])
zag(y);
zag(x);
}
}
push_up(x);
}
void fi(int x,int rk,int z)
{
while(1)
{
if(rk<=siz[ch[x][0]])
x=ch[x][0];
else
{
rk-=siz[ch[x][0]];
if(rk==1)break;
rk--;
x=ch[x][1];
}
}
splay(x,z);
}
void build(int &x,int l,int r,int father)
{
if(l>r)return;
int mid = (l+r)/2;
newnode(x,father,s[mid]-'a'+1);
if(l<mid)build(ch[x][0],l,mid-1,x);
if(r>mid)build(ch[x][1],mid+1,r,x);
push_up(x);
}
void init()
{
p[0]=1;
for(int i=1;i<maxn;i++)
p[i]=p[i-1]*P%mod;
scanf("%s",s);
n=strlen(s);
newnode(root,0,1);
newnode(ch[root][1],root,1);
siz[root]=2;
build(ch[ch[root][1]][0],0,n-1,ch[root][1]);
push_up(ch[root][1]);
push_up(root);
}
long long query(int x,int len)
{
fi(root,x,0);
fi(root,x+len+1,root);
//if(len==1)return val[ch[ch[root][1]][0]];
//printf("%c %c %lld\n",val[ch[ch[root][1]][0]]+'a'-1,val[ch[root][1]]+'a'-1,h[ch[ch[root][1]][0]]);
return h[ch[ch[root][1]][0]];
}
int solve(int x,int y)
{
int l=1,r=min(siz[root]-x,siz[root]-y)-1;
int ans=0;
while(l<=r)
{
int mid = (l+r)/2;
if(query(x,mid)==query(y,mid))l=mid+1,ans=mid;
else r=mid-1;
}
return ans;
}
void change(int x,int y)
{
fi(root,x+1,0);
val[root]=y;
push_up(root);
}
void in(int x,int y)
{
fi(root,x+1,0);
fi(root,x+2,root);
newnode(ch[ch[root][1]][0],ch[root][1],y);
push_up(ch[ch[root][1]][0]);
push_up(ch[root][1]);
push_up(root);
}
}T; int main()
{
T.init();
int q;scanf("%d",&q);
while(q--)
{
char c[10];
scanf("%s",c);
if(c[0]=='R')
{
int x;char k[10];
scanf("%d%s",&x,k);
T.change(x,k[0]-'a'+1);
}
else if(c[0]=='I')
{
int x;char k[10];
scanf("%d%s",&x,k);
T.in(x,k[0]-'a'+1);
}
else
{
int x,y;scanf("%d%d",&x,&y);
printf("%d\n",T.solve(x,y));
}
}
}