题面

洛谷

题解

首先,我们知道求最长公共前缀可以用二分答案+hash来求

因为有修改操作, 考虑将整个字符串的hash值放入splay中

接着就是splay的基本操作了

Code

#include<bits/stdc++.h>

#define LL long long
#define RG register
const int N = 500010;
typedef unsigned long long ull;
using namespace std;
inline int gi() {
RG int x = 0; RG char c = getchar(); bool f = 0;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') c = getchar(), f = 1;
while (c >= '0' && c <= '9') x = x*10+c-'0', c = getchar();
return f ? -x : x;
} ull p[N];
int tot;
struct Splay {
int fa, ch[2], siz, v;
ull hs;
}t[N];
int rt;
#define get(x) (t[t[x].fa].ch[1] == x)
#define ls t[x].ch[0]
#define rs t[x].ch[1]
inline void pushup(int x) {
t[x].siz = t[ls].siz + t[rs].siz + 1;
t[x].hs = t[rs].hs + (ull)t[x].v*p[t[rs].siz] + t[ls].hs*p[t[rs].siz+1];
}
void build(int l, int r, int x) {
if (l > r) return ;
int mid = (l + r) >> 1;
if (mid >= x) rs = mid;
else ls = mid;
t[mid].fa = x, t[mid].siz = 1;
if (l == r) return ;
build(l, mid-1, mid), build(mid+1, r, mid);
pushup(mid);
return ;
}
void rotate(int x) {
int y = t[x].fa, z = t[y].fa, k = get(x);
t[x].fa = z; t[z].ch[get(y)] = x;
t[t[x].ch[k^1]].fa = y; t[y].ch[k] = t[x].ch[k^1];
t[y].fa = x; t[x].ch[k^1] = y;
pushup(y);
return ;
}
void splay(int x, int goal) {
while (t[x].fa != goal) {
int y = t[x].fa, z = t[y].fa;
if (z != goal)
(get(x)^get(y)) ? rotate(x) : rotate(y);
rotate(x);
}
pushup(x);
if (!goal) rt = x;
}
int kth(int k) {
int x = rt;
while (233) {
if (t[ls].siz+1 == k) return x;
if (t[ls].siz+1 < k) k -= (t[ls].siz+1), x = rs;
else x = ls;
}
}
ull get_hash(int l, int r) {
int x = kth(l), y = kth(r+2);
splay(x, 0); splay(y, rt);
return t[t[t[rt].ch[1]].ch[0]].hs;
}
char ch[N];
int main() {
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
p[0] = 1;
for (int i = 1; i <= 300000; i++) p[i] = (ull)27*p[i-1];
scanf("%s", ch+1);
int n = strlen(ch+1);
for (int i = 2; i <= n+1; i++)
t[i].v = t[i].hs = ch[i-1]-'a'+1;
build(1, n+2, rt);
rt = (n+3) >> 1;
tot = n+2;
int T = gi();
while (T--) {
char s[5];
scanf("%s", s);
if (s[0] == 'Q') {
int x = gi(), y = gi();
if (x > y) swap(x, y);
int l = 1, r = tot-y-1;
while (l <= r) {
int mid = (l + r) >> 1;
if (get_hash(x, x+mid-1) == get_hash(y, y+mid-1))
l = mid+1;
else r = mid-1;
}
printf("%d\n", l-1);
}
else if (s[0] == 'R') {
int x = gi();
scanf("%s", s);
splay(kth(x+1), 0);
t[rt].hs -= (ull)t[rt].v*p[t[t[rt].ch[1]].siz];
t[rt].v = s[0]-'a'+1;
t[rt].hs += (ull)t[rt].v*p[t[t[rt].ch[1]].siz];
}
else {
int x = gi();
scanf("%s", s);
int k1 = kth(x+1), k2 = kth(x+2);
splay(k1, 0); splay(k2, k1);
t[t[rt].ch[1]].ch[0] = ++tot;
t[tot].fa = t[rt].ch[1];
t[tot].v = t[tot].hs = s[0]-'a'+1;
splay(tot, 0);
}
}
return 0;
}

洛谷 P4036 [JSOI2008]火星人(splay+字符串hash)的更多相关文章

  1. bzoj 1014: 洛谷 P4036: [JSOI2008]火星人

    题目传送门:洛谷P4036. 题意简述: 有一个字符串,支持插入字符,修改字符. 每次需要查询两个后缀的LCP长度. 最终字符串长度\(\le 100,\!000\),修改和询问的总个数\(\le 1 ...

  2. BZOJ.1014.[JSOI2008]火星人(Splay 二分 Hash)

    题目链接 后缀数组显然不行啊.求LCP还可以哈希+二分,于是考虑用平衡树维护哈希值. \[某一节点的哈希值 = hs[lson]*base^{sz[rson]+1} + s[rt]*base^{sz[ ...

  3. P4036 [JSOI2008]火星人(splay+hash+二分)

    P4036 [JSOI2008]火星人 Splay维护hash,查询二分 $a[x].vl=a[lc].vl*ha[a[rc].sz+1]+a[x].w*ha[a[rc].sz]+a[rc].vl$ ...

  4. 洛谷 P1198 [JSOI2008]最大数

    洛谷 P1198 [JSOI2008]最大数 题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. ...

  5. 洛谷P1198 [JSOI2008]最大数(单点修改,区间查询)

    洛谷P1198 [JSOI2008]最大数 简单的线段树单点问题. 问题:读入A和Q时,按照读入一个字符会MLE,换成读入字符串就可以了. #include<bits/stdc++.h> ...

  6. 「线段树」「单点修改」洛谷P1198 [JSOI2008]最大数

    「线段树」「单点修改」洛谷P1198 [JSOI2008]最大数 题面描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数, ...

  7. 洛谷 P3370 【模板】字符串哈希

    洛谷 P3370 [模板]字符串哈希 题目描述 如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字.大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串. 友情提醒:如果真的 ...

  8. BZOJ1014:[JSOI2008]火星人(Splay,hash)

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

  9. 洛谷P1198 [JSOI2008]最大数

    P1198 [JSOI2008]最大数 267通过 1.2K提交 题目提供者该用户不存在 标签线段树各省省选 难度提高+/省选- 提交该题 讨论 题解 记录 最新讨论 WA80的戳这QwQ BZOJ都 ...

随机推荐

  1. 【bzoj2186】[Sdoi2008]沙拉公主的困惑

    2186: [Sdoi2008]沙拉公主的困惑 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 3303  Solved: 1129[Submit][S ...

  2. 2014蓝桥杯B组初赛试题《啤酒和饮料》

    题目描述: 啤酒每罐2.3元,饮料每罐1.9元.小明买了若干啤酒和饮料,一共花了82.3元.     我们还知道他买的啤酒比饮料的数量少,请你计算他买了几罐啤酒.     注意:答案是一个整数.请通过 ...

  3. jQuery 给class附点击事件获取对应的索引

    有一类div标签,class为pointbox,数量不等,有多个.我需要在点击某一个标签的时候实时获取该标签在这类标签中索引值,以便进行其他操作. 代码很简单: $(".pointbox&q ...

  4. The 'Microsoft Jet OLEDB 4.0 Provider' is not registered on the local machine

    在一台Win7 64位的操纵系统上部署的C# Web系统,操作Excel,批量导入数据,报错,提示错误信息: The ‘Microsoft Jet OLEDB 4.0 Provider' is not ...

  5. hdu 4269 Defend Jian Ge

    #include <cctype> #include <algorithm> #include <vector> #include <string> # ...

  6. (BST)升序数组变为BST树

    题目:给定一个数组,其中元素按升序排序,将其转换为高度平衡BST. 思路:因为是升序数组,那么中间的数字一定是根节点值,然后在对左右两边的数组进行查找根节点的递归.一次处理左右子树. /** * De ...

  7. javascript总结16:数组array12

    1 Array 对象 作用:Array 对象用于在变量中存储多个值. 1.1 数组定义 var ary = new Array();//通过创建对象的方式创建数组 var ary1 = [];// 直 ...

  8. 第06章-渲染Web视图

    1. 理解视图解析 将控制器中请求处理的逻辑和视图中的渲染实现解耦是Spring MVC的一个重要特性.如果控制器中的方法直接负责产生HTML的话,就很难在不影响请求处理逻辑的前提下,维护和更新视图. ...

  9. 替归算法获取Treeview所有节点

    treeview.nodes是获取下一级所有子节点,但是如果是多层的话,就不能,想个法子来获取所有的节点(含节点的子节点),想了想 还是替归算法比较方便,如是有了下面的代码 public static ...

  10. 【C#】特性标签中的属性解释

    第一个为特性作用于类,或者接口(interface) 第二个为是否允许重叠定义,就是连续写两个特性标签 第三个为是否继承,当继承时候,除输出子类外,父类也将输出