鏈接
題解
這題就是在廣義後綴自動機的樹上跑倍增的裸題
建樹的時候要注意處理重邊
然後真沒啥好說的了,因爲實在是太裸了
代碼
#include <bits/stdc++.h>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 600010
#define maxe 600010
#define maxk 20
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
char s[maxn];
ll tb[maxn];
struct Trie
{
int tot, ch[maxn][26], end[maxn];
void init(){while(tot--)cl(ch[tot+1]),end[tot+1]=0;tot=1;}
int* operator[](int index){return ch[index];}
int append(int pos, int c)
{
int &t=ch[pos][c];
t = t?t:t=++tot;
end[t]++;
return t;
}
}trie;
struct EXSAM
{
int tot, ch[maxn][26], fa[maxn], len[maxn], pref[maxn], tb[maxn];
int* operator[](int u){return ch[u];}
void init()
{
int i;
rep(i,1,tot)cl(ch[i]),fa[i]=len[i]=pref[i]=0;
tot=1;
}
int append(int las, int c, int cnt)
{
int p(las);
len[las=++tot]=len[p]+1;
pref[las]=cnt;
for(;p and !ch[p][c];p=fa[p])ch[p][c]=las;
if(!p)fa[las]=1;
else
{
int q=ch[p][c];
if(len[q]==len[p]+1)fa[las]=q;
else
{
int qq=++tot;
memcpy(ch[qq],ch[q],sizeof(ch[q]));
fa[qq]=fa[q];
len[qq]=len[p]+1;
fa[q]=fa[las]=qq;
for(;ch[p][c]==q;p=fa[p])ch[p][c]=qq;
}
}
return las;
}
void bfs_build(Trie& trie)
{
queue<pii> q;
q.em(pii(1,1));
while(!q.empty())
{
auto pr=q.front(); q.pop();
int pos=pr.first, u=pr.second;
tb[pos]=u;
for(int i=0;i<26;i++)
if(trie[pos][i])
{
int to=trie[pos][i], v=ch[u][i]?ch[u][i]:append(u,i,trie.end[to]);
q.em(pii(to,v));
}
}
}
int mov(int p, int c){return max(1,ch[p][c]);}
}exsam;
struct Graph
{
int etot, head[maxn], to[maxe], next[maxe], w[maxe];
void clear(int N)
{
for(int i=1;i<=N;i++)head[i]=0;
etot=0;
}
void adde(int a, int b, int c=0){to[++etot]=b;w[etot]=c;next[etot]=head[a];head[a]=etot;}
#define forp(_,__) for(auto p=__.head[_];p;p=__.next[p])
}G, G0;
struct Doubling_LCA
{
int f[maxn][maxk+1], depth[maxn];
void clear(int n){for(int i=1;i<=n;i++)depth[i]=0, cl(f[i]);}
void dfs(Graph &G, int pos, int pre)
{
for(auto k=1;(1<<k)<=depth[pos];k++)f[pos][k]=f[f[pos][k-1]][k-1];
for(auto p(G.head[pos]);p;p=G.next[p])
if(G.to[p]!=pre)
{
f[G.to[p]][0]=pos;
depth[G.to[p]]=depth[pos]+1;
dfs(G,G.to[p],pos);
}
}
void run(Graph &G, int root)
{
depth[root]=1;
dfs(G,root,0);
}
int jp(int x, int b)
{
for(auto k=0;k<=maxk;k++)
if(b&(1<<k))x=f[x][k];
return x;
}
}db;
void DP(Graph& G, vector<ll>& dp, ll u)
{
dp[u] = exsam.pref[u];
forp(u,G)
{
ll v = G.to[p];
DP(G,dp,v);
dp[u] += dp[v];
}
}
void dfs(Graph& G, Trie& T, ll ug, ll ut)
{
tb[ug] = ut;
forp(ug,G)
{
ll vg = G.to[p], alpha = s[vg] - 'A';
ll vt = T.append(ut,alpha);
dfs(G,T,vg,vt);
}
}
int main()
{
ll N=read(), Q=read(), i;
vector<ll> fa(N+5), dp(2*N+5);
trie.init();
scanf("%s",s+2);
fa[2]=1;
rep(i,3,N+1)fa[i] = read()+1;
rep(i,2,N+1)G0.adde(fa[i],i);
dfs(G0,trie,1,1);
exsam.init();
exsam.bfs_build(trie);
rep(i,2,exsam.tot)G.adde(exsam.fa[i],i);
db.run(G,1);
DP(G,dp,1);
rep(i,1,Q)
{
ll X=tb[read()+1], L=read(), pos = exsam.tb[X], k;
drep(k,maxk,0)
{
if(exsam.len[db.f[pos][k]]>=L)pos = db.f[pos][k];
}
printf("%lld\n",dp[pos]);
}
return 0;
}