P4094 [HEOI2016 / TJOI2016] String

The meaning of problems

Consider binary answer \ (MID \) , and now we have to determine \ (s [c ... c + mid-1] \) is in \ (s [a ... b] \) appeared.

First find \ (s [c ... c + mid-1] \) state where:
build a \ (parent \ Tree \) , from \ (s [1 ... c + mid-1] \) of node (this may be recorded) by multiplying the last jump upward \ (len \ geqslant mid \) node to, this node is referred to \ (now \) .

After we have to determine \ (now \) a \ (endpos \) if they contain the \ ([a + mid-1 , b] \) of a number, for each node we open a segment tree to weight maintenance of the node \ (endpos \) (equivalent barrels), from \ (parent \ tree \) up to merge tree line.

code:

#include<bits/stdc++.h>
using namespace std;
const int maxn=500010;
int n,m,tot,cnt,t;
int id[maxn],root[maxn],head[maxn];
int f[maxn][20];
char s[maxn];
struct edge{int to,nxt;}e[maxn<<1];
struct Seg
{
    #define lc(p) (seg[p].lc)
    #define rc(p) (seg[p].rc)
    #define sum(p) (seg[p].sum)
    int lc,rc,sum;
}seg[maxn*60];
inline int read()
{
    char c=getchar();int res=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9')res=res*10+c-'0',c=getchar();
    return res*f;
}
inline void add_edge(int u,int v)
{
    e[++cnt].nxt=head[u];
    head[u]=cnt;
    e[cnt].to=v;
}
inline void up(int p){sum(p)=sum(lc(p))+sum(rc(p));}
void insert(int &p,int l,int r,int pos)
{
    if(!p)p=++tot;
    sum(p)++;
    if(l==r)return;
    int mid=(l+r)>>1;
    if(pos<=mid)insert(lc(p),l,mid,pos);
    else insert(rc(p),mid+1,r,pos);
}
int query(int p,int l,int r,int ql,int qr)
{
    if(l>=ql&&r<=qr)return sum(p);
    int mid=(l+r)>>1,res=0;
    if(ql<=mid)res+=query(lc(p),l,mid,ql,qr);
    if(qr>mid)res+=query(rc(p),mid+1,r,ql,qr);
    return res;
}
int merge(int p,int q,int l,int r)
{
    if(!p||!q)return p+q;   
    int x=++tot,mid=(l+r)>>1;sum(x)=sum(p)+sum(q);
    if(l==r)return x; 
    lc(x)=merge(lc(p),lc(q),l,mid);
    rc(x)=merge(rc(p),rc(q),mid+1,r);
    return x;
}
struct SAM
{
    int tot,last;
    int fa[maxn],len[maxn];
    int ch[maxn][30];
    SAM(){last=tot=1;}
    inline void add(int c)
    {
        int now=++tot;len[now]=len[last]+1;
        int p=last;last=now;
        while(p&&!ch[p][c])ch[p][c]=now,p=fa[p];
        if(!p){fa[now]=1;return;}
        int q=ch[p][c];
        if(len[q]==len[p]+1)fa[now]=q;
        else
        {
            int nowq=++tot;
            len[nowq]=len[p]+1;
            memcpy(ch[nowq],ch[q],sizeof(ch[q]));
            fa[nowq]=fa[q];fa[q]=fa[now]=nowq;
            while(p&&ch[p][c]==q)ch[p][c]=nowq,p=fa[p];
        }
    }
}sam;
void dfs(int x)
{
    for(int i=1;i<=t;i++)f[x][i]=f[f[x][i-1]][i-1];
    for(int i=head[x];i;i=e[i].nxt)
    {
        int y=e[i].to;
        f[y][0]=x;dfs(y);
        root[x]=merge(root[x],root[y],1,n);
    }
}
inline bool check(int mid,int a,int b,int c,int d)
{
    int now=id[c+mid-1];
    for(int i=t;~i;i--)if(f[now][i]&&sam.len[f[now][i]]>=mid)now=f[now][i];
    return query(root[now],1,n,a+mid-1,b)>0;
}
int main()
{
    n=read(),m=read();
    scanf("%s",s+1);
    id[0]=1;
    for(int i=1;i<=n;i++)sam.add(s[i]-'a'),id[i]=sam.last,insert(root[sam.last],1,n,i);
    for(int i=2;i<=sam.tot;i++)add_edge(sam.fa[i],i);
    t=(int)log2(sam.tot)+1;dfs(1);
    while(m--)
    {
        int a=read(),b=read(),c=read(),d=read();
        int l=0,r=min(b-a+1,d-c+1),ans=0;
        while(l<=r)
        {
            int mid=(l+r)>>1;
            if(check(mid,a,b,c,d))ans=mid,l=mid+1;
            else r=mid-1;
        }
        printf("%d\n",ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/nofind/p/12056476.html