bzoj 1014: [JSOI2008]火星人prefix (hash+splay)

来源:互联网 发布:网络故障诊断的目的 编辑:程序博客网 时间:2024/05/10 03:50

1014: [JSOI2008]火星人prefix

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 5995  Solved: 1905
[Submit][Status][Discuss]

Description

  火星人最近研究了一种操作:求一个字串两个后缀的公共前缀。比方说,有这样一个字符串:madamimadam,
我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 8 9 10 11 字符 m a d a m i m a d a m 现在,
火星人定义了一个函数LCQ(x, y),表示:该字符串中第x个字符开始的字串,与该字符串中第y个字符开始的字串
,两个字串的公共前缀的长度。比方说,LCQ(1, 7) = 5, LCQ(2, 10) = 1, LCQ(4, 7) = 0 在研究LCQ函数的过程
中,火星人发现了这样的一个关联:如果把该字符串的所有后缀排好序,就可以很快地求出LCQ函数的值;同样,
如果求出了LCQ函数的值,也可以很快地将该字符串的后缀排好序。 尽管火星人聪明地找到了求取LCQ函数的快速
算法,但不甘心认输的地球人又给火星人出了个难题:在求取LCQ函数的同时,还可以改变字符串本身。具体地说
,可以更改字符串中某一个字符的值,也可以在字符串中的某一个位置插入一个字符。地球人想考验一下,在如此
复杂的问题中,火星人是否还能够做到很快地求取LCQ函数的值。

Input

  第一行给出初始的字符串。第二行是一个非负整数M,表示操作的个数。接下来的M行,每行描述一个操作。操
作有3种,如下所示
1、询问。语法:Qxy,x,y均为正整数。功能:计算LCQ(x,y)限制:1<=x,y<=当前字符串长度。
2、修改。语法:Rxd,x是正整数,d是字符。功能:将字符串中第x个数修改为字符d。限制:x不超过当前字
符串长度。
3、插入:语法:Ixd,x是非负整数,d是字符。功能:在字符串第x个字符之后插入字符d,如果x=0,则在字
符串开头插入。限制:x不超过当前字符串长度

Output

  对于输入文件中每一个询问操作,你都应该输出对应的答案。一个答案一行。

Sample Input

madamimadam
7
Q 1 7
Q 4 8
Q 10 11
R 3 a
Q 1 7
I 10 a
Q 2 11

Sample Output

5
1
0
2
1

HINT

1、所有字符串自始至终都只有小写字母构成。

2、M<=150,000

3、字符串长度L自始至终都满足L<=100,000

4、询问操作的个数不超过10,000个。

对于第1,2个数据,字符串长度自始至终都不超过1,000

对于第3,4,5个数据,没有插入操作。

Source

[Submit][Status][Discuss]

题解:splay维护hash。

按顺序将节点插入splay,对于splay中的每个点维护子树中这段区间的hash值。

查询的区间[l,r]的hash值,只需要将l-1转到根,r+1转到根的右儿子,所要查询的区间就在根右子树的左子树中

在查询的时候二分答案mid,二分公共前缀的长度,将区间[x,x+mid]与[y,y+mid]的hash值即可。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define N 2000003#define p 2000001001#define ul unsigned long longusing namespace std;int fa[N],ch[N][3],size[N],root,cnt,len;ul val[N],a[N],mi[N];int n,m;char s[N];int get(int x){    return ch[fa[x]][1]==x;}void update(int x){    size[x]=size[ch[x][1]]+size[ch[x][0]]+1;    val[x]=val[ch[x][1]]+a[x]*mi[size[ch[x][1]]]+val[ch[x][0]]*mi[size[ch[x][1]]+1];}void rotate(int x){    int y=fa[x]; int z=fa[y]; int which=get(x);    ch[y][which]=ch[x][which^1]; fa[ch[x][which^1]]=y;    ch[x][which^1]=y; fa[y]=x;    if (z) ch[z][ch[z][1]==y]=x;    fa[x]=z;    update(y); update(x);}void splay(int x,int tar){    for (int f;(f=fa[x])!=tar;rotate(x))     if (fa[f]!=tar)      rotate(get(x)==get(f)?f:x);    if (!tar)  root=x;}int next(){    int now=ch[root][1];    while (ch[now][0])  now=ch[now][0];    return now;}int pre(){    int now=ch[root][0];     while (ch[now][1]) now=ch[now][1];    return now; }void insert(int pos,ul x){    if (root==0)     {        root=++cnt; fa[cnt]=ch[cnt][0]=ch[cnt][1]=0;        val[cnt]=a[cnt]=x;        return;     }    int now=root,f;    while (true)    {       if (pos<=size[ch[now][0]])        now=ch[now][0];       else       {          int k=size[ch[now][0]];          if (k+1==pos) {            splay(now,0);            int t=next();            if (!t)  {                ch[root][1]=++cnt; fa[cnt]=root;                val[cnt]=a[cnt]=x; update(root);                splay(cnt,0);  return;              }            splay(t,now);            ch[ch[root][1]][0]=++cnt; fa[cnt]=ch[root][1];            val[cnt]=a[cnt]=x; update(ch[root][1]); update(root);            splay(cnt,0); return;          }           pos-=(k+1);          now=ch[now][1];       }        }}int  find(int x){    int now=root;    while (true)    {        if (x<=size[ch[now][0]])         now=ch[now][0];        else         {            int k=size[ch[now][0]]+1;            if (k==x) return now;            x-=k;            now=ch[now][1];         }    }}bool pd(int x,int posl,int posr,int xx,int yy){    splay(posl,0); int prel=pre();     int t=find (xx+x); splay(prel,0); splay(t,prel);    ul ans=val[ch[ch[root][1]][0]];    splay(posr,0); int prer=pre();    t=find(yy+x); splay(prer,0); splay(t,prer);    if (ans==val[ch[ch[root][1]][0]])  return true;    return false;}int solve(int x,int y){    if (x>y) swap(x,y);    int posl=find(x); int posr=find(y);    int l=0; int r=len-y+2; int ans=0;    while (l<=r)    {        int mid=(l+r)/2;        if (pd(mid,posl,posr,x,y))  ans=max(ans,mid),l=mid+1;        else r=mid-1;    }    return ans;}int main(){    mi[0]=1; for (int i=1;i<=1000001;i++) mi[i]=mi[i-1]*p;    scanf("%s",s); len=strlen(s);    insert(0,0);    for (int i=0;i<len;i++)     {        ul x=s[i]-95;        insert(i+1,x);     }    insert(len+1,0);    scanf("%d",&n);    for (int i=1;i<=n;i++)    {        char c[10],c1[10]; int x; scanf("%s",c);        if (c[0]=='R')         {            scanf("%d%s",&x,c1); x++;            ul y=c1[0]-95;            int t=find(x);  a[t]=y; update(t); splay(t,0);         }        if (c[0]=='I')         {            scanf("%d%s",&x,c1); x++;            ul y=c1[0]-95;             insert(x,y); len++;         }        if (c[0]=='Q')         {            int x,y;            scanf("%d%d",&x,&y); x++; y++;             printf("%d\n",solve(x,y));         }    }}


0 0
原创粉丝点击