题解:P8947 Angels & Demons
基本思路不难,但是代码量巨大。
由于
本题需要知道的就是一个串在其 link 树上的子树内的所有 endpos 集合内出现。
对于
一个
第一个贡献形式是子树数点,第二种贡献形式就是对于每个点开一个动态开点线段树维护其每种长度被贡献次数。但是注意一个
时间复杂度单
#include<bits/stdc++.h>
#define pb emplace_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=1e6+10;
const int maxlog=20;
void cmax(int &x,int y){ x=x>y?x:y; }
void cmin(int &x,int y){ x=x<y?x:y; }
char s[maxn]; int rt[maxn],le[maxn],n,m,w0;
int dfn[maxn],sz[maxn],vis[maxn],tim;
vector<int> G[maxn],to[maxn],node;
vector<pii> vec[maxn],h;
inline int read(){
int x=0,f=1; char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*f;
}
struct BIT{
int c[maxn],up;
int lowbit(int x){ return x&-x; }
void modify(int x,int v){ for(;x<=up;x+=lowbit(x)) c[x]+=v; }
int query(int x){ int res=0; for(;x;x-=lowbit(x)) res+=c[x]; return res; }
int sum(int l,int r){ return query(r)-query(l-1); }
}t;
struct SegmentTree{
#define mid ((l+r)>>1)
int val[maxn<<5],ls[maxn<<5],rs[maxn<<5],tot;
void pushup(int p){ val[p]=val[ls[p]]+val[rs[p]]; }
void modify(int &p,int l,int r,int x,int v){
if(!p) p=++tot;
if(l==r){ val[p]+=v; return ; }
if(x<=mid) modify(ls[p],l,mid,x,v);
else modify(rs[p],mid+1,r,x,v);
pushup(p);
}
int query(int p,int l,int r,int ql,int qr){
if(!p) return 0;
if(ql<=l&&r<=qr) return val[p];
if(qr<=mid) return query(ls[p],l,mid,ql,qr);
if(ql>mid) return query(rs[p],mid+1,r,ql,qr);
else return query(ls[p],l,mid,ql,qr)+query(rs[p],mid+1,r,ql,qr);
}
}seg;
struct Trie{
int ch[maxn][27],c[maxn],fa[maxn],tot=1;
void ins(char s[],int id){
int u=1; int n=strlen(s+1);
for(int i=1;i<=n;i++){
int x=s[i]-'a';
if(!ch[u][x]){ ch[u][x]=++tot; fa[tot]=u; c[tot]=x; }
u=ch[u][x]; vec[u].pb(id,i);
}
}
}trie;
struct SAM{
int ch[maxn][27],dep[maxn],len[maxn],fa[maxn],mat[maxn];
int pos[maxn],pa[maxlog][maxn],PA[maxlog][maxn],lg[maxn],D,tot;
int add(int c,int last,vector<pii> &ID){
int p=last; int np=++tot; len[np]=len[p]+1;
for(auto z:ID) to[z.fi][z.se]=tot;
for(;p&&!ch[p][c];p=fa[p]) ch[p][c]=np;
if(!p) fa[np]=1;
else{
int q=ch[p][c];
if(len[q]==len[p]+1) fa[np]=q;
else{
int nq=++tot; memcpy(ch[nq],ch[q],sizeof(ch[q]));
fa[nq]=fa[q]; len[nq]=len[p]+1; fa[q]=fa[np]=nq;
for(;p&&ch[p][c]==q;p=fa[p]) ch[p][c]=nq;
}
}
return np;
}
void dfs(int u,int father){
PA[0][dfn[u]=++tim]=father; sz[u]=1;
for(auto v:G[u]){
dep[v]=dep[u]+1;
dfs(v,u); sz[u]+=sz[v];
}
}
int cmp(int x,int y){ return dfn[x]<dfn[y]?x:y; }
int lca(int u,int v){
if(u==v) return u;
u=dfn[u]; v=dfn[v];
if(u>v) swap(u,v);
u++; int k=lg[v-u+1];
return cmp(PA[k][u],PA[k][v-(1<<k)+1]);
}
void init(){
dfs(1,0);
for(int k=1;(1<<k)<=tot;k++)
for(int i=1;i+(1<<k)-1<=tot;i++)
PA[k][i]=cmp(PA[k-1][i],PA[k-1][i+(1<<k-1)]);
}
void walk(char t[]){
int n=strlen(t+1),u=1,L=0; h.clear();
for(int i=1;i<=n;i++){
int c=t[i]-'a';
if(ch[u][c]) u=ch[u][c],++L;
else{
while(u&&!ch[u][c]) u=fa[u];
if(u) L=len[u]+1,u=ch[u][c];
else L=0,u=1;
}
if(u!=1) h.pb(u,L);
}
}
void build(){
queue<int> q; tot=1; pos[1]=1;
for(int i=0;i<26;i++)
if(trie.ch[1][i]) q.push(trie.ch[1][i]);
while(!q.empty()){
int x=q.front(); q.pop();
pos[x]=add(trie.c[x],pos[trie.fa[x]],vec[x]);
for(int i=0;i<26;i++)
if(trie.ch[x][i]) q.push(trie.ch[x][i]);
}
for(int i=2;i<=tot;i++) G[fa[i]].pb(i);
dep[1]=1; init(); lg[1]=0; t.up=tot;
for(int i=2;i<=tot;i++) lg[i]=lg[i>>1]+1;
for(int i=2;i<=tot;i++) pa[0][i]=fa[i];
for(int k=1;(1<<k)<=tot;k++)
for(int i=1;i<=tot;i++)
pa[k][i]=pa[k-1][pa[k-1][i]];
}
int solve(int id,int l,int r){
int L=r-l+1,u=to[id][r];
if(u==1) return 0;
for(int k=lg[dep[u]];k>=0;k--){
int v=pa[k][u];
if(v<=1) continue;
if(L<=len[v]) u=v;
}
return seg.query(rt[u],1,len[u],L,len[u])+t.sum(dfn[u],dfn[u]+sz[u]-1);
}
}sam; int A,B,C,lst,q;
bool cmp(pii x,pii y){ return dfn[x.fi]>dfn[y.fi]||(x.fi==y.fi&&x.se>y.se); }
int main(){
cin>>n>>q>>w0; if(w0) cin>>A>>B>>C;
for(int i=1;i<=n;i++){
cin>>(s+1); trie.ins(s,i);
le[i]=strlen(s+1);
to[i].resize(le[i]+1);
}
sam.build();
while (q--){
int op; op=read();
if(op==1){
scanf("%s",s+1);
if(w0){
int x((1ll * A * lst + B) % C), l(strlen(s + 1));
for (int i(1); i <= l; ++i) {
swap(s[i], s[x % l + 1]);
x = (1ll * A * x + B) % C;
}
}
sam.walk(s); int lst=sam.tot+1;
sort(h.begin(),h.end(),cmp); node.clear();
for(auto z:h){
int u=z.fi;
if(lst<=dfn[u]+sz[u]-1||vis[u]) continue;
seg.modify(rt[u],1,sam.len[u],z.se,1);
lst=dfn[u]; vis[u]=1; node.pb(u);
} lst=0;
for(auto u:node){
if(lst) t.modify(dfn[sam.lca(lst,sam.fa[u])],-1);
t.modify(dfn[sam.fa[u]],1); lst=u;
}
for(auto z:h) vis[z.fi]=0;
}else{
int p,l,r; p=read(); l=read(); r=read();
if(w0){
int x((1ll * A * lst + B) % C);
p = (p + x) % n + 1;
x = (1ll * A * x + B) % C;
l = (l + x) % le[p] + 1;
x = (1ll * A * x + B) % C;
r = (r + x) % le[p] + 1;
if (l > r) swap(l, r);
}
lst=sam.solve(p,l,r);
printf("%d\n",lst);
}
}
return 0;
}