【题目】
原题地址
给定一个
01
01
01串
s
s
s,定义
p
i
=
s
[
1
…
i
]
p_i=s[1\dots i]
pi=s[1…i]。
多组询问
l
,
r
l,r
l,r表示询问
l
≤
i
<
j
≤
r
l\leq i<j\leq r
l≤i<j≤r,
l
c
s
(
p
i
,
p
j
)
lcs(p_i,p_j)
lcs(pi,pj)的最大值。
【解题思路】
在
SAM
\text{SAM}
SAM上,两个节点的
LCA
\text{LCA}
LCA就是它们的
l
c
s
lcs
lcs,
m
x
mx
mx值就是
l
c
s
lcs
lcs的长度。
我们建出原串的 SAM \text{SAM} SAM,那么问题就是求区间前缀两两 LCA \text{LCA} LCA中的 m x mx mx最大值。
考虑从小到大枚举右端点,我们每次加入一个字母,将它在 p a r e n t parent parent树上到根节点的路径打上时间标记,对于一个点,一定是标记越大越优秀。那么实际上这就是一个 a c c e s s access access过程。
具体来说,我们建出序列线段树,每当经过一个节点,若它上面有标记 x x x,就将线段树的第 x x x个位置答案更新成当前标记,然后将这个节点标记更新。查询时我们只需要查询线段树中对应区间的最大值即可。
复杂度 O ( n log 2 n ) O(n\log^2 n) O(nlog2n),代码很好写。
【参考代码】
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10,M=N<<2;
int n;
namespace IO
{
int read()
{
int ret=0;char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) ret=ret*10+(c^48),c=getchar();
return ret;
}
void write(int x){if(x>9)write(x/10);putchar(x%10^48);}
void writeln(int x){write(x);putchar('\n');}
}
namespace Data_Structure
{
struct SAM
{
int sz,las,fa[N],mx[N],pos[N],ch[N][2];
void init(){sz=las=1;}
void extend(int x,int id)
{
int p,q,np,nq;
p=las;las=np=++sz;mx[np]=mx[p]+1;
for(;p && !ch[p][x];p=fa[p]) ch[p][x]=np;
if(!p) fa[np]=1;
else
{
q=ch[p][x];
if(mx[q]==mx[p]+1) fa[np]=q;
else
{
nq=++sz;mx[nq]=mx[p]+1;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
fa[nq]=fa[q];fa[q]=fa[np]=nq;
for(;ch[p][x]==q;p=fa[p]) ch[p][x]=nq;
}
}
pos[id]=las;
}
}S;
struct Segment
{
#define ls (x<<1)
#define rs (x<<1|1)
int mx[M];
void pushup(int x){mx[x]=max(mx[ls],mx[rs]);}
void update(int x,int l,int r,int p,int v)
{
if(l==r) {mx[x]=max(mx[x],v);return;}
int mid=(l+r)>>1;
if(p<=mid) update(ls,l,mid,p,v);
else update(rs,mid+1,r,p,v);
pushup(x);
}
int query(int x,int l,int r,int L,int R)
{
if(L<=l && r<=R) return mx[x];
int mid=(l+r)>>1,res=0;
if(L<=mid) res=query(ls,l,mid,L,R);
if(R>mid) res=max(res,query(rs,mid+1,r,L,R));
return res;
}
#undef ls
#undef rs
}tr;
struct LCT
{
int top,fa[N],col[N],st[N],ch[N][2];
bool isroot(int x){return ch[fa[x]][0]!=x && ch[fa[x]][1]!=x;}
bool get(int x){return ch[fa[x]][1]==x;}
void pushdown(int x){col[ch[x][0]]=col[ch[x][1]]=col[x];}
void rotate(int x)
{
int y=fa[x],z=fa[y],k=get(x);
if(!isroot(y)) ch[z][get(y)]=x;
fa[ch[x][!k]]=y;fa[y]=x;fa[x]=z;
ch[y][k]=ch[x][!k];ch[x][!k]=y;
}
void splay(int x)
{
top=0;st[++top]=x;for(int t=x;!isroot(t);t=fa[t])st[++top]=fa[t];
while(top)pushdown(st[top--]);
while(!isroot(x))
{
int y=fa[x];
if(!isroot(y)) {if(get(y)^get(x)) rotate(x); else rotate(y);}
rotate(x);
}
}
void access(int x,int co)
{
for(int t=0;x;t=x,x=fa[x])
{
splay(x);
if(col[x]) tr.update(1,1,n,col[x],S.mx[x]);
col[x]=co;ch[x][1]=t;
}
}
}T;
}
using namespace Data_Structure;
namespace DreamLolita
{
int Q,ans[N];
char s[N];
struct data
{
int l,r,id;
bool operator <(const data&a)const{return r<a.r;}
}q[N];
void solve()
{
n=IO::read();Q=IO::read();scanf("%s",s+1);
for(int i=1;i<=Q;++i) q[i].l=IO::read(),q[i].r=IO::read(),q[i].id=i;
sort(q+1,q+Q+1); S.init();
for(int i=1;i<=n;++i) S.extend(s[i]^48,i);
for(int i=1;i<=S.sz;++i) T.fa[i]=S.fa[i];
for(int i=1,j=1;i<=n;++i)
{
T.access(S.pos[i],i);
while(j<=Q && q[j].r==i) ans[q[j].id]=tr.query(1,1,n,q[j].l,i),++j;
}
for(int i=1;i<=Q;++i) IO::writeln(ans[i]);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("LOJ6041.in","r",stdin);
freopen("LOJ6041.out","w",stdout);
#endif
DreamLolita::solve();
return 0;
}