#include<algorithm>
#include<cstring>
#include<cctype>
#include<cstdio>
#define rep(i,x,y) for(int i=x; i<=y; ++i)
#define repd(i,x,y) for(int i=x; i>=y; --i)
#define mid (l+r>>1)
using namespace std;
const int N=600005;
typedef long long LL;
int n,m,q,seg_tot,T[N*50],ls[N*50],rs[N*50],cnt,h[N<<1];
char s[N],t[N];
int dfn[N<<1],pos[N<<1],siz[N<<1],ql,qr,root[N<<1],mx[N];
LL ans;
struct edge{int v,n;} e[N<<1];
void ins(int l,int r,int lst,int &rt,int x)
{
if(!rt) rt=++seg_tot;
if(l==r)
{
T[rt]=T[lst]+1;
return;
}
if(x<=mid) ins(l,mid,ls[lst],ls[rt],x),rs[rt]=rs[lst];
else ins(mid+1,r,rs[lst],rs[rt],x),ls[rt]=ls[lst];
T[rt]=T[ls[rt]]+T[rs[rt]];
}
int query(int l,int r,int lst,int rt)
{
if(!(T[rt]-T[lst])) return 0;
if(l==r) return l;
if(qr<=mid) return query(l,mid,ls[lst],ls[rt]);
if(ql>mid) return query(mid+1,r,rs[lst],rs[rt]);
int ret=query(mid+1,r,rs[lst],rs[rt]);
if(!ret) return query(l,mid,ls[lst],ls[rt]);
return ret;
}
void addedge(int u,int v)
{
e[cnt]=(edge){v,h[u]},h[u]=cnt++;
}
struct Sam
{
int fa[N<<1],len[N<<1],rk[N<<1],g[N<<1],bin[N];
int lst,tot,trf[N<<1][30];
void init()
{
rep(i,1,tot)
{
fa[i]=len[i]=rk[i]=g[i]=0;
rep(j,0,25) trf[i][j]=0;
}
lst=tot=1;
}
void ins(int c)
{
int u=lst,v,x=++tot;
lst=tot,len[x]=len[u]+1;
for(; u && !trf[u][c]; trf[u][c]=x,u=fa[u]);
if(!u) fa[x]=1;
else if(len[u]+1==len[v=trf[u][c]]) fa[x]=v;
else
{
int w=++tot;
len[w]=len[u]+1,fa[w]=fa[v];
memcpy(trf[w],trf[v],sizeof(trf[v]));
for(fa[x]=fa[v]=w; u && trf[u][c]==v; trf[u][c]=w,u=fa[u]);
}
}
void dfs(int x)
{
dfn[x]=++*dfn,siz[x]=1;
for(int i=h[x]; i!=-1; i=e[i].n)
dfs(e[i].v),siz[x]+=siz[e[i].v];
}
void build()
{
memset(h,-1,sizeof(h));
rep(i,2,tot) addedge(fa[i],i);
dfs(1);
int p=1;
rep(i,1,n) pos[dfn[p=trf[p][s[i]-'a']]]=i;
rep(i,1,tot)
if(pos[i]) ::ins(1,n,root[i-1],root[i],pos[i]);
else root[i]=root[i-1];
}
bool check(int l,int x)
{
if(!x) return 1;
int p=::query(1,n,root[dfn[x]-1],root[dfn[x]+siz[x]-1]);
return !p || p-l+1<ql;
}
void get_mx()
{
int p=1,nw=0;
rep(i,1,m)
{
while(p && check(nw+1,trf[p][t[i]-'a'])) p=fa[p],nw=len[p];
if(!p) p=1,nw=0;
else p=trf[p][t[i]-'a'],++nw;
mx[i]=nw;
}
}
void query()
{
int p=1;
ans=0;
rep(i,1,m) p=trf[p][t[i]-'a'],g[p]=mx[i];
rep(i,0,m) bin[i]=0;
rep(i,1,tot) ++bin[len[i]];
rep(i,1,m) bin[i]+=bin[i-1];
rep(i,1,tot) rk[bin[len[i]]--]=i;
repd(i,tot,1)
{
int x=rk[i];
g[fa[x]]=max(g[fa[x]],g[x]);
if(len[x]>g[x]) ans+=min(len[x]-g[x],len[x]-len[fa[x]]);
}
printf("%lld\n",ans);
}
} t1,t2;
int getint()
{
char ch;
while(!isdigit(ch=getchar()));
int x=ch-48;
while(isdigit(ch=getchar())) x=x*10+ch-48;
return x;
}
int main()
{
scanf("%s",s+1),n=strlen(s+1);
t1.init();
rep(i,1,n) t1.ins(s[i]-'a');
t1.build();
q=getint();
while(q--)
{
scanf("%s",t+1),ql=getint(),qr=getint();
m=strlen(t+1);
t2.init();
rep(i,1,m) t2.ins(t[i]-'a');
t1.get_mx();
t2.query();
}
return 0;
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 4.935 ms | 4 MB + 804 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #2 | 7.302 ms | 5 MB + 72 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #3 | 7.602 ms | 5 MB + 76 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #4 | 89.881 ms | 5 MB + 596 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #5 | 86.967 ms | 5 MB + 580 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #6 | 889.003 ms | 340 MB + 656 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #7 | 888.054 ms | 340 MB + 580 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #8 | 124.68 ms | 51 MB + 580 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #9 | 259.064 ms | 47 MB + 132 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #10 | 298.422 ms | 97 MB + 396 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #11 | 626.21 ms | 91 MB + 500 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #12 | 513.525 ms | 143 MB + 796 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #13 | 1.034 s | 137 MB + 612 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #14 | 729.016 ms | 191 MB + 788 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #15 | 1.468 s | 184 MB + 1012 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #16 | 951.631 ms | 239 MB + 812 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #17 | 1.912 s | 232 MB + 648 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #18 | 748.998 ms | 107 MB + 276 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #19 | 885.384 ms | 150 MB + 536 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #20 | 1.007 s | 194 MB + 868 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #21 | 1.083 s | 239 MB + 944 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #22 | 1.124 s | 239 MB + 756 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #23 | 1.11 s | 239 MB + 696 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #24 | 1.121 s | 239 MB + 908 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #25 | 1.106 s | 239 MB + 960 KB | Accepted | Score: 4 | 显示更多 |