题面

洛谷

题解

首先,我们知道求最长公共前缀可以用二分答案+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. 【bzoj2749】[HAOI2012]外星人

    2749: [HAOI2012]外星人 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 677  Solved: 360[Submit][Status][ ...

  2. bootstrap设计网站中添加代码高亮插件

    这款插件的名字叫做google-code-prettify 使用该插件之前的效果: 使用插件之后的效果: 接下来说步骤: (1)下载两个文件 http://codecloud.sinaapp.com/ ...

  3. ROS naviagtion analysis: costmap_2d--StaticLayer

    博客转载自:https://blog.csdn.net/u013158492/article/details/50493246 从UML中能够看到,StaticLayer主要是在实现Layer层要求实 ...

  4. Entity Framework 6.0 Tutorials(11):Download Sample Project

    Download Sample Project: Download a sample project for Entity Framework 6 Database-First model below ...

  5. Linux下的fdlisk - l 用法解析-入门篇

    fdlisk - l 的含义是查看linux下面的磁盘分区大小.这个大小包含了很多信息. 我们来看度娘的一则介绍: FDISK进行硬盘分区从实质上说就是对硬盘的一种格式化.当我们创建分区时,就已经设置 ...

  6. OpenSSH/PuTTY/SSH使用

    OpenSSH/PuTTY/SSH 常用SSH服务指令 ① 启动SSH服务的命令 service sshd start ② 停止SSH服务的命令 service sshd stop ③ 重新启动SSH ...

  7. java中为什么要使用代理

    引入代理: 我们为什么要引入java的代理,除了当前类能够提供的功能外,我们还需要补充一些其他功能. 最容易想到的情况就是权限过滤,我有一个类做某项业务,但是由于安全原因只有某些用户才可以调用这个类, ...

  8. 博客迁移到http://pengliu.cf

    博客迁移到http://pengliu.cf

  9. 【转】android中layout_weight的理解

    android Layout_weight的理解 SDK中的解释: Indicates how much of the extra space in the LinearLayout will be ...

  10. Linux 命令之chmod

    立贴今日吉,不断更新,欢迎斧正,支持为感! 1. chmod --权限控制 chmod [-cfvR] [--help] [--version] mode file... 说明 : Linux/Uni ...