传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1014

天,写kth()时,把判断条件k <= siz[ch[x][0]]错写成了k <= ch[x][0],RE不停,还爆掉了几个小时,以后写数据结构题一定要头脑清晰啊!

#include <cstdio>
#include <cstring> const int maxn = 300005, base = 131; int fa[maxn], ch[maxn][2], key[maxn], siz[maxn], root, cnt;
unsigned long long hash[maxn], poww[maxn];
int n, m, t1, t2, stk[maxn], top;
char s[maxn], opr, t3; inline void pushup(int x) {
hash[x] = hash[ch[x][0]] * (poww[siz[ch[x][1]] + 1]) + key[x] * poww[siz[ch[x][1]]] + hash[ch[x][1]];
siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + 1;
}
inline void rotate(int x) {
int y = fa[x];
if (y == ch[fa[y]][0]) {
ch[fa[y]][0] = x;
}
else {
ch[fa[y]][1] = x;
}
fa[x] = fa[y];
int dir = x == ch[y][1];
ch[y][dir] = ch[x][dir ^ 1];
fa[ch[x][dir ^ 1]] = y;
ch[x][dir ^ 1] = y;
fa[y] = x;
pushup(y);
pushup(x);
}
inline void splay(int x, int rt) {
int p;
while (fa[x] != rt) {
p = fa[x];
if (fa[p] == rt) {
rotate(x);
}
else {
if ((p == ch[fa[p]][1]) ^ (x == ch[p][1])) {
rotate(x);
}
else {
rotate(p);
}
rotate(x);
}
}
if (!rt) {
root = x;
}
}
int make_tree(int left, int right) {
if (left > right) {
return 0;
}
int rt = (left + right) >> 1;
key[rt] = s[rt];
ch[rt][0] = make_tree(left, rt - 1);
fa[ch[rt][0]] = rt;
ch[rt][1] = make_tree(rt + 1, right);
fa[ch[rt][1]] = rt;
pushup(rt);
return rt;
}
inline int kth(int k) {
int x = root;
while (k != siz[ch[x][0]] + 1) {
if (k <= siz[ch[x][0]]) {
x = ch[x][0];
}
else {
k -= siz[ch[x][0]] + 1;
x = ch[x][1];
}
}
return x;
}
inline void modify(int root1, int root2) {
root1 = kth(root1);
root2 = kth(root2);
splay(root1, 0);
splay(root2, root1);
}
inline void ist(int pos, int _key) {
modify(pos, pos + 1);
++cnt;
hash[cnt] = key[cnt] = _key;
siz[cnt] = 1;
ch[ch[root][1]][0] = cnt;
fa[cnt] = ch[root][1];
pushup(ch[root][1]);
pushup(root);
}
/*void ist(int x, int k, int _key) {
if (k == siz[ch[x][0]] + 1) {
if (ch[x][1]) {
top = 0;
int i;
for (i = ch[x][1]; ch[i][0]; i = ch[i][0]) {
stk[top++] = i;
}
++cnt;
ch[i][0] = cnt;
fa[cnt] = i;
key[cnt] = _key;
siz[cnt] = 1;
pushup(i);
for (i = top - 1; ~i; --i) {
pushup(stk[i]);
}
}
else {
++cnt;
ch[x][1] = cnt;
fa[cnt] = x;
key[cnt] = _key;
}
pushup(x);
splay(cnt, 0);
return;
}
if (k <= siz[ch[x][0]]) {
ist(ch[x][0], k, _key);
}
else {
ist(ch[x][1], k - siz[ch[x][0]] - 1, _key);
}
}*/
void upd(int x, int k, int _key) {
if (k == siz[ch[x][0]] + 1) {
key[x] = _key;
pushup(x);
return;
}
if (k <= siz[ch[x][0]]) {
upd(ch[x][0], k, _key);
}
else {
upd(ch[x][1], k - siz[ch[x][0]] - 1, _key);
}
pushup(x);
}
unsigned long long gethash(int start, int end) {
modify(start - 1, end + 1);
return hash[ch[ch[root][1]][0]];
} int main(void) {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
scanf("%s", s + 2);
n = strlen(s + 2);
s[1] = s[n + 2] = '$';
n += 2;
cnt = n;
poww[0] = 1;
for (int i = 1; i < maxn; ++i) {
poww[i] = poww[i - 1] * base;
}
root = make_tree(1, n);
scanf("%d", &m);
int left, right, mid;
while (m--) {
while ((opr = getchar()) < 'A');
scanf("%d", &t1);
++t1;
if (opr == 'I') {
while ((t3 = getchar()) < 'a');
ist(t1, t3);
++n;
}
else if (opr == 'R') {
while ((t3 = getchar()) < 'a');
upd(root, t1, t3);
}
else {
scanf("%d", &t2);
++t2;
left = 0;
right = n - (t1 > t2? t1: t2);
while (left != right) {
mid = (left + right + 1) >> 1;
if (gethash(t1, t1 + mid - 1) == gethash(t2, t2 + mid - 1)) {
left = mid;
}
else {
right = mid - 1;
}
}
printf("%d\n", left);
}
}
return 0;
}

  

_bzoj1014 [JSOI2008]火星人prefix【Splay】的更多相关文章

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

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

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

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

  3. BZOJ 1014: [JSOI2008]火星人prefix( splay + hash )

    用splay维护序列, 二分+hash来判断LCQ.. #include<bits/stdc++.h> using namespace std; typedef unsigned long ...

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

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

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

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

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

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  7. bzoj1014: [JSOI2008]火星人prefix splay+hash+二分

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  8. [bzoj1014](JSOI2008)火星人 prefix (Splay维护哈希)

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀. 比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 ...

  9. 【bzoj1014】[JSOI2008]火星人prefix Splay+Hash+二分

    题目描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 8 9 10 ...

随机推荐

  1. MySQL入门笔记 - 视图

    参考书籍<MySQL入门很简单> 1.视图定义 视图是从一个或者多个表中导出来的虚拟的表,透过这个窗口可以看到系统专门提供的数据,使用户可以只关心对自己有用的数据,方便用户对数据操作,同时 ...

  2. Teamviewer ubuntu 提示 TeamViewer Daemon is not running

    http://blog.csdn.net/laohuang1122/article/details/12657343 Ubunut 12.04下面安装了Teamviewer,刚安装完启动是没有问题的, ...

  3. EJB学习(三)——java.lang.ClassCastException: com.sun.proxy.$Proxy2 cannot be cast to..

    在上一篇博客介绍了怎样使用使用Eclipse+JBOSS创建第一个EJB项目,在这期间就遇到一个错误: Exception in thread "main" java.lang.C ...

  4. SQL 视图(Views)

    SQL 视图(Views) 视图是可视化的表. 本章讲解如何创建.更新和删除视图. SQL CREATE VIEW 语句 在 SQL 中,视图是基于 SQL 语句的结果集的可视化的表. 视图包含行和列 ...

  5. Android耳机线控具体解释,蓝牙耳机button监听(仿酷狗线控效果)

    转载请注明出处:http://blog.csdn.net/fengyuzhengfan/article/details/46461253 当耳机的媒体按键被单击后.Android系统会发出一个广播.该 ...

  6. Android NDK编程浅入深出之--Android.mk

        Android.mk Android.mk是一个向Android NDK构建系统描写叙述NDK项目的GUN Makefile片段.它是每个NDK项目的必备组件. 构建系统希望它出如今jni子文 ...

  7. C++标准库和stl差别

    C++库文件夹:开发工具和语言-visual studio文档-visual C++-參考信息-库參考-standard C++ library C#库文件夹:C#使用.NET Framework 类 ...

  8. BZOJ 3550 ONTAK2010 Vacation 单纯形

    题目大意:给定一个长度为3n的区间.要求选一些数,且随意一段长度为n的区间内最多选k个数.求选择数的和的最大值 单纯形直接搞 注意一个数仅仅能被选一次 因此要加上xi<=1这个约束条件 不明确3 ...

  9. JAVA程序员常用软件整理

    Java类软件:-------------------------------JDK7.0:http://pan.baidu.com/s/1jGFYvAYMyclipse8.5破解版:http://p ...

  10. ABAP 检查全角半角

    check全角or半角的方法 第一种方法SJIS_DBC_TO_SBC 全角转半角 SJIS_SBC_TO_DBC 半角转换为全角 设定 import all =xtext = 文本全角-〉半角,返回 ...