bzoj 1014: [JSOI2008]火星人prefix

8 篇文章 0 订阅
7 篇文章 0 订阅

题意:

带修改,插入的sa的lcq。

题解:

splay维护hash,二分找答案。
维护hash还挺好写的把。
然后二分答案,看下这两段区间的hash值是否相同。
写update头脑要清醒。
code:

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
using namespace std;
struct trnode{
    int son[2],fa,c,n,d;
    unsigned long long hash;
}tr[100010];int tot=0,root=0;
unsigned long long pre[100010];
unsigned long long bas=9875321;
void update(int x)
{
    int lc=tr[x].son[0],rc=tr[x].son[1];
    tr[x].n=tr[lc].n+tr[rc].n+1;
    tr[x].hash=tr[lc].hash+tr[x].d*pre[tr[lc].n+1]+tr[rc].hash*pre[tr[lc].n+2];
}
void pushup(int x)
{
    update(x);
    if(tr[x].fa) pushup(tr[x].fa);
}
void add(int fa,int w,int d)
{
    int x=++tot;
    tr[x].fa=fa;tr[x].d=d;tr[x].son[0]=tr[x].son[1]=0;
    tr[x].c=tr[x].n=1;tr[x].hash=d;
    tr[fa].son[w]=x;
}
void rotate(int x)
{
    int y=tr[x].fa,z=tr[y].fa,w=(tr[y].son[0]==x),R,r;
    R=y;r=tr[x].son[w];tr[R].son[1-w]=r;if(r) tr[r].fa=R;
    R=z;r=x;tr[R].son[tr[z].son[1]==y]=r;if(r) tr[r].fa=R;
    R=x;r=y;tr[R].son[w]=r;if(r) tr[r].fa=R;
    update(y);update(x);
}
void splay(int x,int fa)
{
    while(tr[x].fa!=fa)
    {
        int y=tr[x].fa,z=tr[y].fa;
        if(z==fa) rotate(x);
        else
            if((tr[z].son[0]==y)==(tr[y].son[0]==x)) rotate(y),rotate(x);
            else rotate(x),rotate(x);
    }
    if(fa==0) root=x;
}
char s[100010];
void bt(int fa,int w,int l,int r)
{
    int mid=(l+r)/2;
    add(fa,w,s[mid]-'a');int x=tot;
    pushup(x);
    if(l<mid) bt(x,0,l,mid-1);
    if(r>mid) bt(x,1,mid+1,r);
}
int findid(int k)
{
    int x=root;
    while(1)
    {
        int lc=tr[x].son[0],rc=tr[x].son[1];
        if(k<=tr[lc].n) x=lc;
        else if(k>tr[lc].n+1) x=rc,k-=(tr[lc].n+1);
        else break;
    }
    return x;
}
int len;
void qs(int l,int r)
{
    int x=findid(l);splay(x,0);
    int y=findid(r+2);splay(y,x);
}
void print(int x)
{
    if(!x) return;
    update(x);
}
bool check(int l,int x,int y)
{
    if(l==0) return true;
    unsigned long long t1,t2;
    qs(x,x+l-1);t1=tr[tr[tr[root].son[1]].son[0]].hash;
    qs(y,y+l-1);t2=tr[tr[tr[root].son[1]].son[0]].hash;
    return t1==t2;
}
void solve(int x,int y)
{
    int l=0,r=min(len-x+1,len-y+1),ans;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(check(mid,x,y)) ans=mid,l=mid+1;
        else r=mid-1;
    }
    printf("%d\n",ans);
}
int main()
{
    pre[1]=1;
    for(int i=2;i<=100000;i++) pre[i]=pre[i-1]*bas;
    add(0,0,0);add(1,1,0);root=1;pushup(2);
    scanf("%s",s+1);
    len=strlen(s+1);
    bt(2,0,1,len);
    int m;scanf("%d",&m);
    while(m--)
    {
        scanf("%s",s+1);
        if(s[1]=='Q')
        {
            int x,y;scanf("%d %d",&x,&y);
            solve(x,y);
        }
        else if(s[1]=='R')
        {
            int k;scanf("%d %c",&k,s+1);
            int x=findid(k+1);splay(x,0);
            tr[x].d=s[1]-'a';update(x);
        }
        else
        {
            int k;scanf("%d %s",&k,s+1);
            int x=findid(k+1);
            if(tr[x].son[1]==0) add(x,1,s[1]-'a'),splay(x,0);
            else
            {
                int p=tr[x].son[1];while(tr[p].son[0]) p=tr[p].son[0];
                add(p,0,s[1]-'a'),splay(p,0);
            }
            len++;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值