1014: [JSOI2008]火星人prefix
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1478 Solved: 399
[ 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、 询问。语法:Q x y,x, y均为正整数。功能:计算LCQ(x, y) 限制:1 <= x, y <= 当前字符串长度。 2、 修改。语法:R x d,x是正整数,d是字符。功能:将字符串中第x个数修改为字符d。限制:x不超过当前字符串长度。 3、 插入:语法:I x d,x是非负整数,d是字符。功能:在字符串第x个字符之后插入字符d,如果x = 0,则在字符串开头插入。限制:x不超过当前字符串长度。
Output
对于输入文件中每一个询问操作,你都应该输出对应的答案。一个答案一行。
Sample Input
7
Q 1 7
Q 4 8
Q 10 11
R 3 a
Q 1 7
I 10 a
Q 2 11
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
数据规模:
对于100%的数据,满足:
1、 所有字符串自始至终都只有小写字母构成。
2、 M <= 150,000
3、 字符串长度L自始至终都满足L <= 100,000
4、 询问操作的个数不超过10,000个。
对于第1,2个数据,字符串长度自始至终都不超过1,000
对于第3,4,5个数据,没有插入操作。
1
0
2
1
数据规模:
对于100%的数据,满足:
1、 所有字符串自始至终都只有小写字母构成。
2、 M <= 150,000
3、 字符串长度L自始至终都满足L <= 100,000
4、 询问操作的个数不超过10,000个。
对于第1,2个数据,字符串长度自始至终都不超过1,000
对于第3,4,5个数据,没有插入操作。
Solution:二分答案, SPLAY维护Hash值
代码:
#include <cstdio>
#include <cstring>
#define maxn 2000001
#define L ch[x][0]
#define R ch[x][1]
using namespace std;
struct SplayTree
{
int sz[maxn], ch[maxn][2], cnt[maxn], pre[maxn], a[maxn], d[maxn], val[maxn], h[maxn], rt, top;
char st[maxn];
void up(int x)
{//{{{
if (!x) return;
sz[x] = sz[L] + sz[R] + 1;
if (x != 1 && x != 2)
{
cnt[x] = cnt[L] + cnt[R] + 1;
h[x] = (h[L] + val[x] * d[cnt[L] + 1] + h[R] * d[cnt[L] + 2]);
}
else
{
cnt[x] = cnt[L] + cnt[R];
h[x] = h[L] + h[R];
}
}//}}}
inline void Rotate (int x, int f)
{//{{{
int y = pre[x];
ch[y][!f] = ch[x][f];
pre[ ch[x][f] ] = y;
pre[x] = pre[y];
if (pre[x]) ch[ pre[y] ][ ch[pre[y]][1] == y ] = x;
ch[x][f] = y;
pre[y] = x;
up (y);
}//}}}
inline void splay (int x, int goal)
{//{{{
while (pre[x] != goal)
{
if (pre[pre[x]] == goal) Rotate (x ,ch[pre[x]][0] == x);
else
{
int y = pre[x], z = pre[y];
int f = (ch[z][0] == y);
if (ch[y][f] == x) Rotate (x, !f), Rotate (x, f);
else Rotate (y, f), Rotate (x, f);
}
}
up (x);
if (goal == 0) rt = x;
}//}}}
inline int find(int x, int k)
{//{{{
while (1)
{
if (k < sz[L] + 1) x = L;
else
if (k > sz[L] + 1)
{
k -= sz[L] + 1;
x = R;
}
else return x;
}
}//}}}
inline void rep(int x, int dat)
{//{{{
int fl = find(rt, x - 1);
int fr = find(rt, x + 1);
splay(fl, 0), splay(fr, fl);
int v = ch[fr][0];
val[v] = dat;
up(v);
splay(v, 0);
}//}}}
inline int RHash(int l, int r)
{//{{{
int fl = find(rt, l - 1);
int fr = find(rt, r + 1);
splay(fl, 0), splay(fr, fl);
int v = ch[fr][0];
return h[v];
}//}}}
inline int GA(int a, int b)
{//{{{
int n = sz[rt] - 1;
int ans = 0;
for (int i = 1 << 18; i; i >>= 1)
{
if (a + i - 1 <= n && b + i - 1 <= n)
{
if (RHash(a, a + i - 1) == RHash(b, b + i - 1))
{
ans += i;
a += i;
b += i;
}
}
}
return ans;
}//}}}
void newnode(int &x, int c)
{//{{{
val[x = ++top] = c;
L = R = 0;
sz[x] = cnt[x] = 1;
}//}}}
void build(int &x, int l, int r, int f)
{//{{{
if (l > r) return;
int mid = (l + r) / 2;
newnode(x, a[mid]);
build(L, l, mid - 1, x);
build(R, mid + 1, r, x);
pre[x] = f;
up(x);
}//}}}
void init()
{//{{{
scanf("%s", &st);
int n = strlen(st);
d[1] = 1;
for(int i = 2; i <= 150000; i++) d[i] = d[i - 1] * 27;
for(int i = 1; i <= n; i++) a[i] = st[i - 1] - 'a' + 1;
top = 2, rt = 1;
ch[rt][1] = 2;
sz[2] = sz[rt] = pre[2] = 1;
int r1;
build(r1, 1, n, 2);
ch[2][0] = r1;
splay(r1, 0);
}//}}}
inline void insert(int x, int dat)
{//{{{
int fl = find(rt, x);
int fr = find(rt, x + 1);
splay(fl, 0), splay(fr, fl);
newnode(ch[fr][0], dat);
pre[ ch[fr][0] ] = fr;
splay(ch[fr][0], 0);
}//}}}
inline void getint(int &res)
{//{{{
bool flag = true;
char chr;
res = 0;
do
chr = getchar();
while ((chr < '0' || chr > '9') && (chr != '-'));
if (chr == '-') flag = false, chr = getchar();
do
{
res = res * 10 + chr - '0';
chr = getchar();
}
while (chr >= '0' && chr <= '9');
if (!flag) res = -res;
}//}}}
inline void printint(int x)
{//{{{
if (x < 0) putchar('-');
while (x)
{
putchar(x % 10 + '0');
x /= 10;
}
}//}}}
void solve()
{//{{{
init();
int m, l, r, x;
char chr, ch1;
getint(m);
while(m--)
{
scanf(" ");
scanf("%c", &ch1);
switch (ch1)
{
case 'Q' :getint(l), getint(r), printf("%d\n", GA(l + 1, r + 1)); break;
case 'I' :getint(x), scanf(" %c", &chr), insert(x + 1, chr - 'a' + 1); break;
case 'R' :getint(x), scanf(" %c", &chr), rep(x + 1, chr - 'a' + 1); break;
}
}
}//}}}
}spt;
int main()
{
spt.solve();
return 0;
}