bzoj1014 [JSOI2008]火星人prefix

1014: [JSOI2008]火星人prefix

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 8583  Solved: 2724
[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个数据,没有插入操作。

分析:这道题很容易想到怎么做,但是很难写对啊.
   对于询问,其实就是要求给定起点的最长公共前缀,很显然,用二分+hash就可以完成,hash可以采用自然溢出.
   后面的修改和插入操作很显然就是splay了吧.对于splay上的每个节点,维护一下区间的hash值和字符的个数.一个区间的hash值可以通过两个子区间的hash值合并得到.
   犯了几个傻逼错误:先pushup了父节点,再pushup子节点,导致一直RE. 提取区间的l,r弄错了,应该是[l-1,r+1],我记成了[l,r + 1]QAQ.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef unsigned long long ull;

const int maxn = 1000010;

char s[maxn];
int len,root,tot = 1,q;
ull pow[maxn];

struct node
{
    int left,right,fa,sizee;
    ull hashh,v;
} e[maxn];

void pushup(int o)
{
    e[o].sizee = 1;
    if (e[o].left != 0)
        e[o].sizee += e[e[o].left].sizee;
    if (e[o].right != 0)
        e[o].sizee += e[e[o].right].sizee;
    e[o].hashh = e[e[o].right].hashh + (e[o].v - 'a' + 1) * pow[e[e[o].right].sizee] + e[e[o].left].hashh * pow[(e[e[o].right].sizee + 1)];
}

void build(int l,int r,int &x,int y)
{
    if (l > r)
        return;
    if (!x)
        x = ++tot;
    int mid = (l + r) >> 1;
    e[x].fa = y;
    e[x].v = s[mid];
    if (l == r)
    {
        e[x].hashh = s[l] - 'a' + 1;
        e[x].sizee = 1;
        return;
    }
    build(l,mid - 1,e[x].left,x);
    build(mid + 1,r,e[x].right,x);
    pushup(x);
}

void turnr(int x)
{
    int y = e[x].fa;
    int z = e[y].fa;
    e[y].left = e[x].right;
    if (e[x].right != 0)
        e[e[x].right].fa = y;
    e[x].fa = z;
    if (z != 0)
    {
        if (e[z].left == y)
            e[z].left = x;
        else
            e[z].right = x;
    }
    e[x].right = y;
    e[y].fa = x;
    pushup(y);
    pushup(x);
}

void turnl(int x)
{
    int y = e[x].fa;
    int z = e[y].fa;
    e[y].right = e[x].left;
    if (e[x].left != 0)
        e[e[x].left].fa = y;
    e[x].fa = z;
    if (z != 0)
    {
        if (e[z].left == y)
            e[z].left = x;
        else
            e[z].right = x;
    }
    e[x].left = y;
    e[y].fa = x;
    pushup(y);
    pushup(x);
}

void splay(int x,int yy)
{
    while (e[x].fa != yy)
    {
        int y = e[x].fa;
        int z = e[y].fa;
        if (z == 0 || z == yy)
        {
            if (e[y].left == x)
                turnr(x);
            else
                turnl(x);
        }
        else
        {
            if (e[z].left == y && e[y].left == x)
            {
                turnr(y);
                turnr(x);
            }
            else
            {
                if (e[z].right == y && e[y].right == x)
                {
                    turnl(y);
                    turnl(x);
                }
                else
                {
                    if (e[z].left == y && e[y].right == x)
                    {
                        turnl(x);
                        turnr(x);
                    }
                    else
                    {
                        turnr(x);
                        turnl(x);
                    }
                }
            }
        }
    }
    if (yy == 0)
        root = x;
    pushup(x);
}

int find(int k,int x)
{
    if (k == 1 + e[e[x].left].sizee)
        return x;
    if (k > 1 + e[e[x].left].sizee)
        return find(k - 1 - e[e[x].left].sizee,e[x].right);
    if (k < 1 + e[e[x].left].sizee)
        return find(k,e[x].left);
}

void update1(int x,char ch)
{
    int pos = find(x + 1,root);
    splay(pos,0);
    e[pos].v = ch;
    pushup(pos);
}

void update2(int x,char ch)
{
    int pos = find(x + 1,root);
    int pos2 = find(x + 2,root);
    splay(pos,0);
    splay(pos2,pos);
    e[pos2].left = ++tot;
    e[tot].sizee = 1;
    e[tot].hashh = (ch - 'a' + 1);
    e[tot].v = ch;
    e[tot].fa = pos2;
    pushup(pos2);
    pushup(pos);
}

ull check(int x,int len)
{
    int pos = find(x,root);
    int pos2 = find(x + len + 1,root);
    splay(pos,0);
    splay(pos2,pos);
    return e[e[pos2].left].hashh;
}

int query(int x,int y)
{
    if (e[find(x + 1,root)].v != e[find(y + 1,root)].v)
        return 0;
    int l = 1,r = min(len - x + 1,len - y + 1),res = 0;
    while (l <= r)
    {
        int mid = (l + r) >> 1;
        if (check(x,mid) == check(y,mid))
        {
            res = mid;
            l = mid + 1;
        }
        else
            r = mid - 1;
    }
    return res;
}

int main()
{
    pow[0] = 1;
    root = 1;
    for (int i = 1; i <= 100010; i++)
        pow[i] = pow[i - 1] * 27;
    scanf("%s",s + 1);
    len = strlen(s + 1);
    build(0,len + 1,root,0);
    scanf("%d",&q);
    while (q--)
    {
        //printf("%d\n",q);
        char ch[2],d;
        int x,y;
        scanf("%s",ch);
        if(ch[0] == 'Q')
        {
            scanf("%d%d",&x,&y);
            printf("%d\n",query(x,y));
        }
        if (ch[0] == 'R')
        {
            scanf("%d",&x);
            scanf("%s",ch);
            update1(x,ch[0]);
        }
        if (ch[0] == 'I')
        {
            scanf("%d",&x);
            scanf("%s",ch);
            len++;   //注意len一定要变化.
            update2(x,ch[0]);
        }
    }

    return 0;
}

 

posted @ 2018-02-11 15:30  zbtrs  阅读(197)  评论(0编辑  收藏  举报