[BZOJ1014] [JSOI2008] 火星人prefix (splay & 二分答案)

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

Solution

  相信有很多人第一眼觉得是后缀数组吧。然而后缀数组无法有效地应付修改操作

  这道题比较特殊的一点是询问个数很少,基本集中在插入和修改操作

  我们可以用$splay$维护这个字符串,插入和修改按普通$splay$做,the Longest Common Qianzhui$LCQ$需要二分答案:

  每个节点维护一个子树$hash$值,二分答案$ans$,不断检查$s[x]\sim s[x   +ans]$和$s[y]\sim s[y+ans]$的$hash$值是否一样即可

  由于有提取区间的操作,所以$treap$是无法满足的

  插入和修改的总复杂度是$O(q_1logn)$,$LCQ$的总复杂度是$O(q_2log^2n)$,因为$q_2$相对较小,所以整体可以看成$O(qlogn)$的

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 struct spaly
  4 {
  5     int c[2], fa, val, siz, hash;
  6     int& operator[] (int x)
  7     {
  8         return c[x];
  9     }
 10 }a[100005];
 11 char s[100005], op[5];
 12 int pwr[100005];
 13 
 14 void push_up(int k)
 15 {
 16     int x = a[a[k][1]].siz;
 17     a[k].siz = a[a[k][0]].siz + x + 1;
 18     a[k].hash = a[a[k][0]].hash * pwr[x + 1] + a[k].val * pwr[x] + a[a[k][1]].hash;
 19 }
 20 
 21 void rotate(int &k, int x)
 22 {
 23     int y = a[x].fa, z = a[y].fa, dy = a[y][1] == x;
 24     if(k == y) k = x;
 25     else a[z][a[z][1] == y] = x;
 26     a[y][dy] = a[x][!dy], a[a[x][!dy]].fa = y;
 27     a[x][!dy] = y, a[y].fa = x, a[x].fa = z;
 28     push_up(y);
 29 }
 30 
 31 void splay(int &k, int x)
 32 {
 33     while(k != x)
 34     {
 35         int y = a[x].fa, z = a[y].fa;
 36         if(k != y)
 37             if(a[y][1] == x ^ a[z][1] == y) rotate(k, x);
 38             else rotate(k, y);
 39         rotate(k, x);
 40     }
 41     push_up(x);
 42 }
 43 
 44 int find_kth(int k, int x)
 45 {
 46     if(x <= a[a[k][0]].siz) return find_kth(a[k][0], x);
 47     if(x == a[a[k][0]].siz + 1) return k;
 48     return find_kth(a[k][1], x - a[a[k][0]].siz - 1);
 49 }
 50 
 51 int main()
 52 {
 53     int n, m, x, y, z, root, l, r, mid, ptot;
 54     scanf("%s%d", s, &m);
 55     n = strlen(s);
 56     pwr[0] = 1;
 57     for(int i = 1; i <= 100002; ++i)
 58         pwr[i] = pwr[i - 1] * 137;
 59     a[1].fa = 2, a[1].siz = 1;
 60     for(int i = 2; i <= n + 2; ++i)
 61     {
 62         a[i][0] = i - 1, a[i].fa = i + 1;
 63         a[i].val = s[i - 2], push_up(i);
 64     }
 65     a[n + 2].fa = 0, root = ptot = n + 2;
 66     while(m--)
 67     {
 68         scanf("%s", op);
 69         if(op[0] == 'R')
 70         {
 71             scanf("%d%s", &x, op);
 72             splay(root, find_kth(root, x + 1));
 73             a[root].val = op[0], push_up(root);
 74         }
 75         else if(op[0] == 'I')
 76         {
 77             scanf("%d%s", &x, op), ++n;
 78             splay(root, find_kth(root, x + 1));
 79             splay(a[root][1], find_kth(root, x + 2));
 80             a[a[root][1]][0] = ++ptot;
 81             a[ptot].val = a[ptot].hash = op[0];
 82             a[ptot].fa = a[root][1], a[ptot].siz = 1;
 83             push_up(a[root][1]), push_up(root);
 84         }
 85         else
 86         {
 87             scanf("%d%d", &x, &y);
 88             if(x > y) swap(x, y);
 89             l = 0, r = n - y + 2, z = 0;
 90             while(l < r - 1)
 91             {
 92                 mid = (l + r) >> 1;
 93                 splay(root, find_kth(root, x));
 94                 splay(a[root][1], find_kth(root, x + mid + 1));
 95                 z = a[a[a[root][1]][0]].hash;
 96                 splay(root, find_kth(root, y));
 97                 splay(a[root][1], find_kth(root, y + mid + 1));
 98                 z -= a[a[a[root][1]][0]].hash;
 99                 z ? r = mid : l = mid;
100             }
101             printf("%d\n", l);
102         }
103     }
104     return 0;
105 }
View Code

 

posted @ 2016-06-24 19:08  CtrlCV  阅读(647)  评论(0编辑  收藏  举报