BZOJ 1014: [JSOI2008]火星人prefix

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个数据,没有插入操作。

Solution

乍一眼看上去还以为是后缀数组+离线,然而有修改和从中间插入,这样就不好搞了。

翻了翻题解原来是哈希+二分+平衡树QAQ

用平衡树来维护哈希值,树中节点的hash值表示这棵子树的哈希值

如图,节点x的Hash值为左子树+x的值(x节点的字符转化为数字的值)+右子树(这里的“+”是拼接字符串的意思),即

x->Hash = L->Hash * (R->size + 1) + x->value * R->size + R->Hash

维护这么个东西就好了。

考虑询问操作。我们二分答案ans,这里要注意二分答案中上界与下界的取值。检验答案是否可行的时候,从x位置开始取出ans长度的字符串,再从y位置开始取出ans长度的字符串,比较hash值。区间取hash的话直接splay两次就好了。splay要在头尾加上两个哨兵以防越界。

这题可以不开long long,直接unsigned让他自然溢出。

还需要注意插入字符后字符串长度会+1。这会影响二分时候上下界。一定要注意!

Code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef unsigned ULL;
const int INF = 1009999999;
const int maxn = 300009;
const ULL Base = 29;
char str[maxn], opt[5], chr[5];
ULL T[maxn];
namespace Splay_Tree {
    int c[maxn][2], fa[maxn], siz[maxn], tot, root;
    ULL ha[maxn], val[maxn];
    void pu(int x) {
        int lc = c[x][0], rc = c[x][1];
        siz[x] = siz[lc] + siz[rc] + 1;
        ha[x] = ha[lc] * T[siz[rc] + 1] + val[x] * T[siz[rc]] + ha[rc];
    }
    int build(int l, int r) {
        if (l > r) return 0;
        int mid = (l + r) >> 1;
        int lc = build(l, mid - 1), rc = build(mid + 1, r);
        c[mid][0] = lc; c[mid][1] = rc;
        if (lc) fa[lc] = mid;
        if (rc) fa[rc] = mid;
        val[mid] = max(str[mid] - 'a' + 1, 0);
        pu(mid); return mid;
    }
    int new_node(int v, int father) {
        siz[++tot] = 1; ha[tot] = val[tot] = v; fa[tot] = father;
        return tot;
    }
    void rotate(int x) {
        int y = fa[x], z = fa[y], a = c[y][1]==x, b = a ^ 1;
        if (z) c[z][c[z][1]==y] = x;
        fa[x] = z; fa[y] = x; if (c[x][b]) fa[c[x][b]] = y;
        c[y][a] = c[x][b]; c[x][b] = y;
        pu(y); pu(x);
    }
    void splay(int x, int pos = 0) {
        while (fa[x] != pos) {
            int y = fa[x], z = fa[y];
            if (z != pos) {
                if (c[z][0] == y ^ c[y][0] == x) rotate(x);
                else rotate(y);
            }
            rotate(x);
        }
        if (pos == 0) root = x;
    }
    int Kth(int x, int k) {
        int lc = c[x][0], lsiz = siz[lc];
        if (k == lsiz + 1) return x;
        if (k <= lsiz) return Kth(lc, k);
        return Kth(c[x][1], k - lsiz - 1);
    }
    void modify(int pos, int v) {
        int x = Kth(root, pos + 1);
        splay(x);
        val[x] = v;
        pu(x);
    }
    void insert(int pos, int v) {
        int x = Kth(root, pos + 1), y = Kth(root, pos + 2);
        splay(x); splay(y, x);
        c[y][0] = new_node(v, y);
        pu(y); pu(x);
    }
    ULL query(int Left, int Right) {
        int L = Kth(root, Left), R = Kth(root, Right + 2);
        splay(L); splay(R, L);
        return ha[c[R][0]];
    }
}
using namespace Splay_Tree;
int main() {
    T[0] = 1; for (int i = 1; i < maxn; ++i) T[i] = T[i - 1] * Base;
    scanf("%s", str + 2);
    int n = strlen(str + 2);
    root = build(1, n + 2);
    tot = n + 2;
    int Q, x, y;
    scanf("%d", &Q);
    while (Q--) {
        scanf("%s", opt);
        if (opt[0] == 'Q') {
            scanf("%d%d", &x, &y);
            int l = 0, r = n - max(x, y) + 1, mid;
            while (l < r) {
                mid = (l + r + 1) >> 1;
                if (query(x, x + mid - 1) == query(y, y + mid - 1)) l = mid;
                else r = mid - 1;
            }
            printf("%d\n", l);
        }
        if (opt[0] == 'R') {
            scanf("%d%s", &x, chr);
            modify(x, chr[0] - 'a' + 1);
        }
        if (opt[0] == 'I') {
            scanf("%d%s", &x, chr);
            insert(x, chr[0] - 'a' + 1);
            ++n;
        }
    }
    return 0;
}