【题目链接】:http://www.lydsy.com/JudgeOnline/problem.php?id=1014

【题意】



让你在线查询最长公共前缀.

支持单节点修改;

插入操作;

【题解】

/*
伸展树会保证
这棵树的中序遍历的结果是s[1..n]
即整个序列;
在进行旋转操作的时候,这个性质能被保持住;
伸展树在维护的时候;
每次会把需要操作的节点转到根节点;
然后进行对应的操作;
我们在进行
LCQ(x,y)的时候,
先二分枚举长度len;
然后把
x-1,x+len的节点编号获取a1,a2;
y-1,y+len的节点编号也获取b1,b2;
然后把a1转到根节点,a2放到根节点(也就是a1)的下面;
因为x+len>x-1所以a2肯定是在a1的右儿子处;
而这个时候a2的左子树代表的字符就是
s[x..x+len-1]了;
相应的对b1,b2也做同样的事情
也能获取s[y..y+len-1];
根据伸展树维护的hash值判断这两个子串是否相同。。
如果相同的话,就可以让len边长一点;
不同的话,肯定不能变长了,就变短一点呗.
然后返回答案就好 插入操作的话;
先提取x节点,把它转到根节点的位置;
然后再提取x+1号节点,把它转到根节点的下方
这里x+1号节点的左儿子肯定是空的,因为x和x+1是连在一起的;
中间不可能还有比x+1小的了;
则把这个新插入的节点放在x+1号节点的左边. 修改操作就简答多了
直接找到那个节点;
然后把它转到根节点去;
再修改它的值;
这里记住每次都把节点转到根节点就好 头部和尾部都要加一个空节点;
这样做写插入操作会好写一点吧? hash值可以搜一下RKhash;
这里不用管它会溢出;
你开一个unsigned long long就可以毁天灭地了;
let it go~~
*/

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL unsigned long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define which(x) (ch[fa[x]][1]==x) typedef pair<int, int> pii;
typedef pair<LL, LL> pll; const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 1e5 + 200;
const LL seed = 131; char s[N], str[N], op[10], val[10];
int m, n, fa[N], tot, ch[N][2], root, x, siz[N];
LL po_w[N], has[N]; void push_up(int x)
{
siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + 1;
has[x] = has[ch[x][0]] + po_w[siz[ch[x][0]]] * str[x] + po_w[siz[ch[x][0]] + 1] * has[ch[x][1]];
} int build(int l, int r, int rt)
{
if (l > r) return 0;
int mid = (l + r) >> 1;
int x = ++tot;
fa[x] = rt; str[x] = s[mid];
ch[x][0] = build(l, mid - 1, x);
ch[x][1] = build(mid + 1, r, x);
push_up(x);
return x;
} int Rank(int x, int k)
{
if (siz[ch[x][0]] >= k)
return Rank(ch[x][0], k);
else
if (k == siz[ch[x][0]] + 1)
return x;
else
return Rank(ch[x][1], k - siz[ch[x][0]] - 1);
} void Rotate(int x)
{
int f = fa[x];
bool k = which(x);
ch[f][k] = ch[x][!k];
ch[x][!k] = f;
ch[fa[f]][which(f)] = x;
fa[ch[f][k]] = f;
fa[x] = fa[f];
fa[f] = x;
siz[x] = siz[f], has[x] = has[f];
push_up(f);
} void Splay(int x, int g)
{
while (fa[x] != g)
{
int f = fa[x];
if (fa[f] == g)
{
Rotate(x);
break;
}
if (which(x) ^ which(f))
Rotate(x);
else
Rotate(f);
Rotate(x);
}
if (!g) root = x;
} void Change(int pos, char val)
{
int x = Rank(root, pos);
Splay(x, 0);
str[x] = val;
push_up(x);
} void Insert(int pos, char val)
{
int x = Rank(root, pos), y = Rank(root, pos + 1);
Splay(x, 0), Splay(y, x);
ch[y][0] = ++tot;
str[tot] = val, fa[tot] = y;
push_up(tot), push_up(y), push_up(x);
} int lcq(int tx, int ty)
{
int l = 0, r = n, ans = 0;
while (l <= r)
{
int mid = (l + r) >> 1;
if (ty + mid - 1 > n + 1) //?????
{
r = mid - 1;
continue;
}
//[tx..tx+mid-1] but (tx-1,tx+mid)
int x = Rank(root, tx - 1), y = Rank(root, tx + mid);
Splay(x, 0), Splay(y, x);
LL temp1 = has[ch[y][0]];
x = Rank(root, ty - 1), y = Rank(root, ty + mid);
Splay(x, 0), Splay(y, x);
if (temp1 == has[ch[y][0]])
{
ans = mid;
l = mid + 1;
}
else
r = mid - 1;
}
return ans;
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
po_w[0] = 1;
rep1(i, 1, N - 2)
po_w[i] = po_w[i - 1] * seed;
scanf("%s", s + 1);
n = strlen(s + 1);
root = build(0, n + 1, 0);
rei(m);
rep1(i, 1, m)
{
scanf("%s", op);
if (op[0] == 'R')
{
scanf("%d%s", &x, val);
Change(x + 1, val[0]);
}
else
if (op[0] == 'I')
{
scanf("%d%s", &x, val);
Insert(x + 1, val[0]);
n++;
}
else
if (op[0] == 'Q')
{
int x, y;
scanf("%d%d", &x, &y);
if (x > y)
swap(x, y);
if (x != y)
printf("%d\n", lcq(x + 1, y + 1));
else
printf("%d\n", n - x + 1);
}
}
return 0;
}

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

  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 hash && splay

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

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

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

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

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

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

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

  7. [BZOJ 1014] [JSOI2008] 火星人prefix 【Splay + Hash】

    题目链接:BZOJ - 1014 题目分析 求两个串的 LCP ,一种常见的方法就是 二分+Hash,对于一个二分的长度 l,如果两个串的长度为 l 的前缀的Hash相等,就认为他们相等. 这里有修改 ...

  8. BZOJ 1014: [JSOI2008]火星人prefix

    Sol Splay+Hash+二分答案. 用Splay维护Hash,二分答案判断. 复杂度 \(O(nlog^2n)\) PS:这题调了两个晚上因为没开long long.许久不写数据结构题感觉写完整 ...

  9. bzoj 1014 [JSOI2008]火星人prefix(splay+hash)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1014 [题意] 给定一个字符串,要求提供修改一个字符,插入一个字符,查询两个后缀LCP ...

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

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

随机推荐

  1. hibernate 的映射文件快速生成:使用CodeSmith快速生成映射文件和映射类

    一 CodeSmith简介 本文以表自动生成NHibernate的映射文件和映射类的实例来说明一下本软件的使用方法. CodeSmith是一种基于模板的代码生成工具,其使用类似于ASP.NET的语法来 ...

  2. Android SimpleAdapter

    1.MainActivity.java public class MainActivity extends Activity { private ListView listView; private ...

  3. Altium Designer如何设置pcb尺寸

  4. oracle 10g文件目录结构详解

    一个典型的oralce目录结构如下: /u01└── oracle    ├── admin (数据库管理文件位置,以实例划分)    │   ├── mydb    │   │   ├── adum ...

  5. (转)Windows Server 2012 R2虚拟机自激活(AVMA)技术

    转自: 老丁的技术博客 相信Hyper-v管理员都有这样的经历,安装多台虚拟机后,都要一台一台手工激活,如果虚拟机足够多的话,这是一项很繁琐的工作,但从Windows Server 2012 R2开始 ...

  6. SQL Server 中计算农历

    1.建一表,放初始化资料   因为农历的日期,是由天文学家推算出来的,到现在只有到2049年的,以后的有了还可以加入!   CREATE TABLE SolarData ( yearId int no ...

  7. bootstrap课程4 bootstrap的css样式有哪些内容需要注意

    bootstrap课程4 bootstrap的css样式有哪些内容需要注意 一.总结 一句话总结: 1.如何选择产品(框架)的版本? 大版本下的最后一个版本,但是同时又要选择稳定的版本,也就是如果做产 ...

  8. js闭包(函数内部嵌套一个匿名函数:这个匿名函数可将所在函数的局部变量常驻内存)

    js闭包(函数内部嵌套一个匿名函数:这个匿名函数可将所在函数的局部变量常驻内存) 一.总结 1.闭包:就是在一个函数内部嵌套一个匿名函数,这个匿名函数可以访问这个函数的变量. 二.要点 闭包 闭包的相 ...

  9. HDU 2587 - 很O_O的汉诺塔

    看题传送门 吐槽题目 叫什么很O_O的汉诺塔我还@.@呢. 本来是想过一段时间在来写题解的,不过有人找我要. 本来排名是第8的.然后搞了半天,弄到了第五.不过代码最短~ 截止目前就9个ID过,小小的成 ...

  10. android权限详细

    访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES ,读取或写入登记check-in数据库属性表的权限 获取错略位置 android.permiss ...