题解:CF1801G A task for substrings

· · 题解

前言

复建来了。

思路

首先我们可以有一个粗略的思路就是用前缀 1\sim r 的答案减去 1\sim l-1 的答案然后得到一个大于等于正确答案的东西,然后我们考虑一下哪些答案会被多算我们发现如果有一个串的 l 在 1\sim l-1 中然后 r 在 l\sim r 中的话那么这个串我们无法减去所以我们考虑对于每一组询问算这样的数量然后剪掉即可。

细节讲一下这两个步骤如何实现。

实在不会的话只需要把充要条件写出来就行了假设 1\to p 的点为 a,p+1\to len_i 的点为 b,1\to l-1 的点为 c,l\sim n 的点为 d,那么只需要满足。

dfn_a\leq dfn_c\leq dfn_a+siz_a-1\\ dfn1_b\leq dfn1_d\leq dfn1_b+siz1_b-1

只需要注意是在两棵树上的 dfs 序就行了。

代码

写得有点史。

#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define rep(i,x,y) for(register int i=x;i<=y;i++)
#define rep1(i,x,y) for(register int i=x;i>=y;--i)
#define fire signed
#define ll long long
#define il inline
template<class T> il void print(T x) {
    if(x<0) printf("-"),x=-x;
    if (x > 9) print(x / 10);
    putchar(x % 10 + '0');
}
int T=1;
const int N=1e6+10,M=5e5+10;
struct nn{
    struct node{
        int s[30];
        int lst;
    }tr[N];
    int idx;
    int len[N];
    void modify(string s) {
        int p=0;
        for(auto to:s) {
            if(!tr[p].s[to-'a']) {
                tr[p].s[to-'a']=++idx;
                len[idx]=len[p]+1;
            }
            p=tr[p].s[to-'a'];
        }
    }
    void build() {
        queue<int>q;
        rep(i,0,25) if(tr[0].s[i]) q.push(tr[0].s[i]);
        while(q.size()) {
            int x=q.front();
            q.pop();
            rep(i,0,25) {
                int &to=tr[x].s[i];
                if(to) {
                    tr[to].lst=tr[tr[x].lst].s[i];
                    q.push(to);
                }else {
                    tr[x].s[i]=tr[tr[x].lst].s[i];
                }
            }
        }
    }
    vector<int>v[N];
    int dfn[N],tot,siz[N];
    int f[N][20];
    void dfs(int x,int fa) {
        dfn[x]=++tot;
        siz[x]=1;
        f[x][0]=fa;
        for(auto to:v[x]) {
            if(to==fa) continue;
            dfs(to,x);
            siz[x]+=siz[to];
        }
    }
    void ii() {
        rep(j,1,19) {
            rep(i,1,tot) f[i][j]=f[f[i][j-1]][j-1];
        }
    }
    void init() {
        rep(i,0,idx) v[tr[i].lst].pb(i);
    }
}t1,t2;
struct nnn{
    int a,b,c;
};
vector<nnn>pl[N];
ll tr[N*5];
int lowbit(int x) {
    return x&-x;
}
void add(int x,int k,int mx) {
    for(;x<=mx;x+=lowbit(x)) {
        tr[x]+=k;
    }
}
ll Ans(int x) {
    ll res=0;
    for(;x;x-=lowbit(x)) res+=tr[x];
    return res;
}
ll ans[M];
int n,m;
vector<int>qy[N];
string t,s[M];
ll sum[N*5];
int ll1[M],rr[M];
vector<int>idd[N*5];
int ip[M];
vector<int>id1[N*5];
void solve() {
    cin>>n>>m;
    cin>>t;
    rep(i,1,n) cin>>s[i],t1.modify(s[i]);
    rep(i,1,n) {
        reverse(s[i].begin(),s[i].end());
        t2.modify(s[i]);
        reverse(s[i].begin(),s[i].end());
    }
    t1.build();
    t2.build();
    t1.init();
    t2.init();
    t1.dfs(0,0);
    t2.dfs(0,0);
    t2.ii();
    int lt=t.size()-1;
    int now=0;
    rep(i,1,n) {
        int p=0;
        for(auto to:s[i]) {
            p=t1.tr[p].s[to-'a'];
        }
        add(t1.dfn[p],1,t1.tot);
        add(t1.dfn[p]+t1.siz[p],-1,t1.tot);
    }
    rep(i,0,lt) {
        now=t1.tr[now].s[t[i]-'a'];
        sum[i+1]=Ans(t1.dfn[now]);
    }
    rep(i,1,lt+1) sum[i]+=sum[i-1];
    rep(i,1,m) {
        int l,r;
        cin>>l>>r;
        ll1[i]=l,rr[i]=r;
        idd[l-1].pb(i);
        id1[l].pb(i);
        ans[i]=sum[r]-sum[l-1];
    }
    now=0;
    rep(i,1,lt+1) {
        now=t1.tr[now].s[t[i-1]-'a'];
        for(auto to:idd[i]) {
            qy[t1.dfn[now]].pb(to);
        }
    }
    rep(i,1,n) {
        vector<int>v,v1;
        v.clear(),v1.clear();
        int p=0;
        for(auto to:s[i]) {
            p=t1.tr[p].s[to-'a'];
            v.pb(p);
        }
        p=0;
        reverse(s[i].begin(),s[i].end());
        for(auto to:s[i]) {
            p=t2.tr[p].s[to-'a'];
            v1.pb(p);
        }
        int ll11=v.size()-1;
        rep(j,0,ll11-1) {
            int a=t1.dfn[v[j]],b=t1.dfn[v[j]]+t1.siz[v[j]]-1;
            int c=t2.dfn[v1[ll11-1-j]],d=t2.dfn[v1[ll11-1-j]]+t2.siz[v1[ll11-1-j]]-1;
            pl[a].pb({c,d,1});
            pl[b+1].pb({c,d,-1});
        }
    }
    now=0;
    rep1(i,lt+1,1) {
        now=t2.tr[now].s[t[i-1]-'a'];
        for(auto to:id1[i]) {
            ip[to]=now;
        }
    }
    memset(tr,0,sizeof tr);
    rep(i,1,t1.tot) {
        for(auto to:pl[i]) {
            add(to.a,to.c,t2.tot);
            add(to.b+1,-to.c,t2.tot);
        }
        for(auto id:qy[i]) {
            int now=ip[id];
            int len=rr[id]-ll1[id]+1;
            rep1(j,19,0) {
                if(t2.f[now][j]&&t2.len[t2.f[now][j]]>len) {
                    now=t2.f[now][j];
                }
            }
            if(len<t2.len[now]) now=t2.f[now][0];
            ans[id]-=Ans(t2.dfn[now]);
        }
    }
    rep(i,1,m) printf("%lld ",ans[i]);
}
fire main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0); 
    while(T--) {
        solve();
    }
    return false;
}