P6292 区间本质不同子串个数(SAM+LCT+线段树)

5 篇文章 0 订阅
5 篇文章 0 订阅

P6292 区间本质不同子串个数

题目描述

给定一个长度为 n n n 的字符串 S S S m m m 次询问由 S S S 的第 L L L 到第 R R R 个字符组成的字符串包含多少个本质不同的子串。

定义两个字符串 a , b a,b a,b 相同当且仅当 ∣ a ∣ = ∣ b ∣ |a|=|b| a=b 并且对于 i ∈ [ 1 , ∣ a ∣ ] i\in[1,|a|] i[1,a] 都有 a i = b i a_i=b_i ai=bi

输入格式

第一行一个长度为 n n n 的字符串 S S S

第二行一个整数 m m m,表示询问次数。

接下来 m m m 行,第 i i i 行包含两个整数 l i , r i l_i,r_i li,ri,描述第 i i i 次询问。

输出格式

m m m 行,每行一个整数,表示第 i i i 次询问的答案。

样例 #1

样例输入 #1

aababc
3
1 2
2 4
3 6

样例输出 #1

2
5
9

提示

样例 1 解释
  • 第一次询问,字符串为 aa \texttt{aa} aa,包含 a \texttt{a} a, aa \texttt{aa} aa 2 2 2 种本质不同子串。
  • 第二次询问,字符串为 aba \texttt{aba} aba,包含 a , b , ab , ba , aba \texttt{a},\texttt{b},\texttt{ab},\texttt{ba},\texttt{aba} a,b,ab,ba,aba, 共 5 5 5 种本质不同子串。
  • 第三次询问,字符串为 babc \texttt{babc} babc,包含 a \texttt{a} a, b \texttt{b} b, c \texttt{c} c, ab \texttt{ab} ab, ba \texttt{ba} ba, bc \texttt{bc} bc, bab \texttt{bab} bab, abc \texttt{abc} abc, babc \texttt{babc} babc 9 9 9 种本质不同子串
数据规模与约定
  • 对于 20 % 20\% 20% 的数据,满足 n ≤ 3 × 1 0 3 n\leq 3\times 10^3 n3×103 m ≤ 3 × 1 0 3 m\leq 3\times 10^3 m3×103
  • 对于 100 % 100\% 100% 的数据,满足 1 ≤ n ≤ 1 0 5 1\leq n\leq 10^5 1n105 1 ≤ m ≤ 2 × 1 0 5 1\leq m\leq 2\times 10^5 1m2×105 1 ≤ l i ≤ r i ≤ n ( i ∈ [ 1 , m ] ) 1\leq l_i\leq r_i\leq n(i\in[1,m]) 1lirin(i[1,m])

先离线询问,按照右端点排序。
如果不考虑重复的字符串,每加入一个右端点,对答案的贡献就是 i ∈ [ 1 , r ] i\in[1,r] i[1,r]都加1。
现在我们需要考虑重复字符串的问题,我们只需要计算所有重复出现的子串最后一次出现的位置即可,而对于一个等价类中的所有字符串,也就是后缀树上一个结点能够表示的所有字符串,我们可以同时处理他们的最后一次出现的结束位置。
我们把这个结点的最后一次出现的结束位置记为 v i s x vis_x visx,那么,一个结点对答案的贡献就是: [ v i s x − l e n x + 1 , v i s x − l e n f a ] [vis_x-len_x+1,vis_x-len_{fa}] [visxlenx+1,visxlenfa]这个区间。
在加入一个新的右端点的时候,我们可以把这条链上原有的贡献减去,再加上新的贡献。
然而暴力跳链显然不合适,不难看出, v i s vis vis值相同的结点一定在一条连续的链上,也就 L C T LCT LCT的实链,这个性质可以很好的进行 a c c e s s access access操作。
也就是说,在每插入一个点往根跳的过程中,只需要减去这条实链对答案的贡献并且在实链的 s p l a y splay splay的根结点打上新的 v i s vis vis标记即可。
对于每一个询问,我们只需要在线段树上维护一个区间和即可。

#include<bits/stdc++.h>
#define clean(x) memset(x,0,sizeof(x))
#define fil(x,n) fill(x,x+1+n,0)
#define inf 2000000009
#define maxn 1000005
#define int long long
using namespace std;

int read()
{
    int x=1,res=0;
    char c=getchar();
    while(c<'0'||c>'9')
    {
        if(c=='-')
        x=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        res=res*10+(c-'0');
        c=getchar();
    }
    return res*x;
}

int vis[maxn],len,to[maxn],now;
struct ST{
    int tag[maxn<<2],tr[maxn<<2],a[maxn];
    void build(int k,int l,int r)
    {
        if(l==r)
        {
            tr[k]=a[l];
            return;
        }
        int mid=(l+r)>>1;
        build(k<<1,l,mid);
        build(k<<1|1,mid+1,r);
        tr[k]=tr[k<<1]+tr[k<<1|1];
    }
    void pushdown(int k,int l,int r,int mid)
    {
        tr[k<<1]+=tag[k]*(mid-l+1);
        tr[k<<1|1]+=tag[k]*(r-mid);
        tag[k<<1]+=tag[k];
        tag[k<<1|1]+=tag[k];
        tag[k]=0;
    }
    void modify(int k,int l,int r,int x,int y,int val)
    {
        if(y<x) return;
        if(x<=l&&r<=y) 
        {
            tag[k]+=val;
            tr[k]+=val*(r-l+1);
            return;
        }
        int mid=(l+r)>>1;
        if(tag[k]) pushdown(k,l,r,mid);
        if(x<=mid) modify(k<<1,l,mid,x,y,val);
        if(mid+1<=y) modify(k<<1|1,mid+1,r,x,y,val);
        tr[k]=tr[k<<1]+tr[k<<1|1];
    }
    int query(int k,int l,int r,int x,int y)
    {
        if(x<=l&&r<=y) return tr[k];
        int mid=(l+r)>>1,ans=0;
        if(tag[k]) pushdown(k,l,r,mid);
        if(x<=mid) ans+=query(k<<1,l,mid,x,y);
        if(mid+1<=y) ans+=query(k<<1|1,mid+1,r,x,y);
        return ans;
    }
}st;
struct edge{
    int next,to;
};
struct SAM{
    int id[maxn],pos[maxn],tot=1,lt=1,num,l[maxn],ch[maxn][26],sz[maxn],f[maxn],last[maxn];
    edge g[maxn];
    void init(int len){
        for(int i=0;i<=len;i++){
            id[i]=0;pos[i]=0;f[i]=0;pos[i]=0;
            last[i]=0;l[i]=0;sz[i]=0;
            for(int j=0;j<26;j++) ch[i][j]=0;
        }
        tot=lt=1;num=0;
    }
    void insert(int c,int i){
        int v=++tot,u=lt;lt=tot;pos[tot]=i;id[i]=tot;
        sz[v]=1;l[v]=l[u]+1;
        while(u&&!ch[u][c]) {ch[u][c]=v;u=f[u];}
        if(!u) {f[v]=1;return;}
        int x=ch[u][c];
        if(l[x]==l[u]+1) {f[v]=x;return;}
        int y=++tot;pos[y]=pos[x];
        l[y]=l[u]+1;f[y]=f[x];f[x]=f[v]=y;
        memcpy(ch[y],ch[x],sizeof(ch[x]));
        while(u&&ch[u][c]==x) {ch[u][c]=y;u=f[u];}
    }
    void add(int from,int to)
    {
        g[++num].next=last[from];
        g[num].to=to;
        last[from]=num;
    }
    void dfs(int x)
    {
        for(int i=last[x];i;i=g[i].next)
        {
            int v=g[i].to;
            dfs(v);
            sz[x]+=sz[v];
        }
    }
}sam;
struct LCT{
    int top,ch[maxn][2],f[maxn],xr[maxn],q[maxn],rev[maxn],val[maxn],sum[maxn];
    int tag_mul[maxn],tag_add[maxn],sz[maxn];
    #define ls(x) ch[x][0]
    #define rs(x) ch[x][1]
    void makeroot(int x){access(x);splay(x);rev[x]^=1;}
    int find(int x){access(x);splay(x);while(ch[x][0])x=ch[x][0];return x;}
    void split(int x,int y){makeroot(x);access(y);splay(y);}
    bool isroot(int x){return ch[f[x]][0]!=x&&ch[f[x]][1]!=x;}
    void link(int x,int y){makeroot(x);if(find(y)!=x)f[x]=y;}
    void cut(int x,int y){
        if(find(x)!=find(y)) return; split(x,y);
        if(ch[y][0]==x&&ch[x][1]==0) ch[y][0]=0,f[x]=0;
        if(ch[y][1]==x&&ch[x][0]==0) ch[y][1]=0,f[x]=0;
    }
    void pushup(int x){
        xr[x]=xr[ch[x][0]]^xr[ch[x][1]]^val[x];
        sz[x]=(sz[ls(x)]+sz[rs(x)]+1);
        sum[x]=(val[x]+sum[ls(x)]+sum[rs(x)]);
    }
    void pushdown(int x){
        int l=ch[x][0],r=ch[x][1];
        if(rev[x]){
            rev[l]^=1;rev[r]^=1;rev[x]^=1;
            swap(ch[x][0],ch[x][1]);
        }
        if(vis[x]){
            vis[ls(x)]=vis[x];
            vis[rs(x)]=vis[x];
        }
    }
    void rotate(int x){
        int y=f[x],z=f[y],l,r;
        if(ch[y][0]==x)l=0;else l=1;r=l^1;
        if(!isroot(y)){if(ch[z][0]==y)ch[z][0]=x;else ch[z][1]=x;}
        f[x]=z;f[y]=x;f[ch[x][r]]=y;
        ch[y][l]=ch[x][r];ch[x][r]=y;
        pushup(y);pushup(x);
    }
    void splay(int x){
        top=1;q[top]=x;
        for(int i=x;!isroot(i);i=f[i]) q[++top]=f[i];
        for(int i=top;i;i--) pushdown(q[i]);
        while(!isroot(x)){
            int y=f[x],z=f[y];
            if(!isroot(y)){
                if((ch[y][0]==x)^(ch[z][0]==y)) rotate(x);
                else rotate(y);
            } rotate(x);
        }
    }
    void access(int x){
        int t=0;
        for(;x;t=x,x=f[x]){
            splay(x),ch[x][1]=t,pushup(x);
            int fa=f[x];
            if(vis[x]) 
            st.modify(1,1,len,vis[x]-sam.l[x]+1,vis[x]-sam.l[fa],-1);
        }
        st.modify(1,1,len,1,now,1);vis[t]=now;
    }
}lct;


struct node{
    int l,r,id;
}g[maxn];
char a[maxn];int aa,bb,ans[maxn];

signed main()
{
    cin>>(a+1);int q=read();
    len=strlen(a+1);
    for(int i=1;i<=len;i++) sam.insert(a[i]-'a',i);
    for(int i=1;i<=q;i++){
        aa=read();bb=read();
        g[i]={aa,bb,i};
    }
    sort(g+1,g+q+1,[&](node a,node b){return a.r<b.r;});
    for(int i=2;i<=sam.tot;i++) lct.f[i]=sam.f[i];
    int last=1;
    for(int i=1;i<=q;i++){
        int l=g[i].l,r=g[i].r;
        while(last<=r){
            now=last;
            lct.access(sam.id[last]);
            last++;
        }
        ans[g[i].id]=st.query(1,1,len,l,r);
    }
    for(int i=1;i<=q;i++) cout<<ans[i]<<endl;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

snowy2002

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值