用splay维护序列, 二分+hash来判断LCQ..

#include<bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;

const int maxn = 100009;
const int P = 1000173169; ull K[maxn];
int N;
char S[maxn]; struct Node {
Node *ch[2], *p;
int s, v;
ull h;
inline void setc(Node* c, int d) {
ch[d] = c;
c->p = this;
}
inline int d() {
return this == p->ch[1];
}
inline void upd() {
s = ch[0]->s + ch[1]->s + 1;
h = ull(v) * K[ch[1]->s] + ull(ch[0]->h) * K[ch[1]->s + 1] + ch[1]->h;
}
} pool[maxn], *null, *root, *pt = pool; Node* newNode(int _v) {
Node* t = pt++;
t->ch[0] = t->ch[1] = t->p = null;
t->v = t->h = _v; t->s = 1;
return t;
} void rot(Node* t) {
Node* p = t->p;
int d = t->d();
p->p->setc(t, p->d());
p->setc(t->ch[d ^ 1], d);
t->setc(p, d ^ 1);
p->upd();
if(p == root)
root = t;
} void splay(Node* t, Node* f = null) {
for(Node* p = t->p; p != f; p = t->p) {
if(p->p != f)
p->d() != t->d() ? rot(t) : rot(p);
rot(t);
}
t->upd();
} Node* select(int k) {
for(Node* t = root; ;) {
int s = t->ch[0]->s;
if(k == s)
return t;
if(k > s)
k -= s + 1, t = t->ch[1];
else
t = t->ch[0];
}
} Node* range(int l, int r) {
l--; r++;
Node *L = select(l), *R = select(r);
splay(L); splay(R, L);
return R->ch[0];
} //[l, r)
Node* build(int l, int r) {
if(l >= r) return null;
int m = (l + r) >> 1;
Node* t = newNode(S[m]);
t->setc(build(l, m), 0);
t->setc(build(m + 1, r), 1);
t->upd();
return t;
} void init() {
null = pt++;
null->s = null->v = null->h = 0;
null->setc(null, 0);
null->setc(null, 1);
root = null; K[0] = 1;
for(int i = 1; i < maxn; i++)
K[i] = K[i - 1] * P;
} int LCQ(int x, int y) {
if(x == y) return N - x + 1;
if(x > y) swap(x, y);
int L = 0, R = N - y + 1, ans;
while(L <= R) {
int m = (L + R) >> 1;
ull h = range(x, x + m - 1)->h;
if(range(y, y + m - 1)->h == h)
ans = m, L = m + 1;
else
R = m - 1;
}
return ans;
} int main() {
freopen("test.in", "r", stdin); init();
scanf("%s", S + 1); N = strlen(S + 1);
root = build(0, N + 2); int m; scanf("%d", &m);
while(m--) {
char op; scanf(" %c", &op);
if(op == 'Q') {
int x, y; scanf("%d%d", &x, &y);
printf("%d\n", LCQ(x, y));
} else if(op == 'R') {
int x; scanf("%d", &x);
char c; scanf(" %c", &c);
Node* t = range(x, x);
t->v = c; t->upd();
t->p->upd(); root->upd();
} else {
N++;
int x; scanf("%d", &x);
char c; scanf(" %c", &c);
Node *L = select(x), *R = select(x + 1);
splay(L); splay(R, L);
R->setc(newNode(c), 0);
R->upd(); L->upd();
}
} return 0;
}

  

1014: [JSOI2008]火星人prefix

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 4228  Solved: 1295
[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

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

数据规模:

对于100%的数据,满足:

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

2、 M <= 150,000

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

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

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

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

Source

BZOJ 1014: [JSOI2008]火星人prefix( splay + hash )的更多相关文章

  1. BZOJ 1014 [JSOI2008]火星人prefix (Splay + Hash + 二分)

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 8112  Solved: 2569[Submit] ...

  2. BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6243  Solved: 2007[Submit] ...

  3. BZOJ 1014: [JSOI2008]火星人prefix Splay+二分

    1014: [JSOI2008]火星人prefix 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1014 Description 火星人 ...

  4. BZOJ 1014 [JSOI2008]火星人prefix (splay+二分答案+字符串hash)

    题目大意:维护一个字符串,支持插入字符和替换字符的操作,以及查询该字符串两个后缀的最长公共前缀长度 乍一看以为是后缀数组,然而并没有可持久化后缀数组(雾) 看题解才知道这是一道splay题,首先要对s ...

  5. bzoj 1014 [JSOI2008]火星人prefix——splay+哈希

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1014 用splay维护字符串,每个点记录子树的哈希值,然后二分查询. 二分不是把两个点的哈希 ...

  6. BZOJ 1014 [JSOI2008]火星人prefix | Splay维护哈希值

    题目: 题解: #include<cstdio> #include<algorithm> #include<cstring> typedef long long l ...

  7. bzoj 1014: [JSOI2008]火星人prefix hash && splay

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3154  Solved: 948[Submit][ ...

  8. 求帮看!!!!BZOJ 1014 [JSOI2008]火星人prefix

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4164  Solved: 1277[Submit] ...

  9. 【BZOJ1014】[JSOI2008]火星人prefix Splay+hash

    [BZOJ1014][JSOI2008]火星人prefix Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个 ...

随机推荐

  1. [dfs+水] hdu 4462 Scaring the Birds

    题意: N*N的矩阵中有M个点能够放稻草人.且给覆盖距离R 每一个稻草人能覆曼哈顿距离R以内的点 问最少须要多少个稻草人 思路: 由于范围非常小,直接能够暴力 注意稻草人所在的位置是不须要被覆盖的 代 ...

  2. Android Blur效果之FastBlur

    Blur 自从iOS系统引入了Blur效果,也就是所谓的毛玻璃.模糊化效果,磨砂效果,各大系统就開始竞相模仿,这是一个怎样的效果呢,我们现来看一些图: 这些就是典型的Blur效果,在iOS和MIUI中 ...

  3. asp.net Page_Load事件加载两次

    Page_Load 即使加上 if(!IsPostBack){ ……} 还时走了2次 这时候 或者看看你的程序和脚本,是不是刷新页面了 或者页面的样式有错误的地方  例如: background:ur ...

  4. 《Java虚拟机原理图解》 1.2.2、Class文件里的常量池具体解释(上)

    [last updated:2014/11/27] NO1.常量池在class文件的什么位置? 我的上一篇文章<Java虚拟机原理图解> 1.class文件基本组织结构中已经提到了clas ...

  5. 字符串String类

    1. String类是一个密封类.用关键字sealed修饰: 2. 字符串的两个特性:     ·不可变性:string类型变量,一旦声明就表明它是不会被改变的.因此,string中的方法对strin ...

  6. D - 1sting(相当于斐波那契数列,用大数写)

    Description You will be given a string which only contains ‘1’; You can merge two adjacent ‘1’ to be ...

  7. C陷阱与缺陷(三)

    第三章 语义陷阱 3.1 指针与数组 C语言中只有一维数组,而且数组的大小必须字编译期就作为一个常数确定下来.数组中的元素可以是另外一个数组.任何一个数组下标运算都等同于一个对应的指针运算.int a ...

  8. VC生成lib的_stdcall函数名与mingw生成的不一致

    Qt Creator在Windows系统中,怎样链接VC生成的动态链接库 这个问题曾经困扰了我一整天.我想的是按照VC中的方法,增加include文件,增加lib文件,然后编译即可.谁知链接时总是出现 ...

  9. Qt中的对象类型转换(Qstring 转换char*有三种方法)

    char * 与 const char *的转换 char *ch1="hello11"; const char *ch2="hello22"; ch2 = c ...

  10. windows 下一个mysql password忘记改变

    到场mysql简介 my.ini 于[mysqld]以下被加入 skip-grant-tables win+R 热键 进cmd 然后输入命令net stop mysql  最后一点,使文件夹mysql ...