洛谷 P4036 [JSOI2008]火星人(splay+字符串hash)
题面
题解
首先,我们知道求最长公共前缀可以用二分答案+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)的更多相关文章
- bzoj 1014: 洛谷 P4036: [JSOI2008]火星人
题目传送门:洛谷P4036. 题意简述: 有一个字符串,支持插入字符,修改字符. 每次需要查询两个后缀的LCP长度. 最终字符串长度\(\le 100,\!000\),修改和询问的总个数\(\le 1 ...
- BZOJ.1014.[JSOI2008]火星人(Splay 二分 Hash)
题目链接 后缀数组显然不行啊.求LCP还可以哈希+二分,于是考虑用平衡树维护哈希值. \[某一节点的哈希值 = hs[lson]*base^{sz[rson]+1} + s[rt]*base^{sz[ ...
- 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$ ...
- 洛谷 P1198 [JSOI2008]最大数
洛谷 P1198 [JSOI2008]最大数 题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. ...
- 洛谷P1198 [JSOI2008]最大数(单点修改,区间查询)
洛谷P1198 [JSOI2008]最大数 简单的线段树单点问题. 问题:读入A和Q时,按照读入一个字符会MLE,换成读入字符串就可以了. #include<bits/stdc++.h> ...
- 「线段树」「单点修改」洛谷P1198 [JSOI2008]最大数
「线段树」「单点修改」洛谷P1198 [JSOI2008]最大数 题面描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数, ...
- 洛谷 P3370 【模板】字符串哈希
洛谷 P3370 [模板]字符串哈希 题目描述 如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字.大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串. 友情提醒:如果真的 ...
- BZOJ1014:[JSOI2008]火星人(Splay,hash)
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam, 我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 ...
- 洛谷P1198 [JSOI2008]最大数
P1198 [JSOI2008]最大数 267通过 1.2K提交 题目提供者该用户不存在 标签线段树各省省选 难度提高+/省选- 提交该题 讨论 题解 记录 最新讨论 WA80的戳这QwQ BZOJ都 ...
随机推荐
- MySQL - pt-query-digest的下载与使用
对于脚本文件,是可以执行的,我们不用安装.所以,但是这个脚本文件没有执行的权限,所以,我们首先赋予这个脚本文件的可执行的权限. 再次查看文件的信息后. 已经有了执行的权限了. 运行脚本的时候,可要注意 ...
- 安装 SQL Server 2014 Express
安装 SQL Server 2014 Express 我的电脑系统: Windows 10 64位 一 . 下载 安装Microsoft SQL Server 2014 Express 软甲下载地址: ...
- Java学习——JSTL标签与EL表达式之间的微妙关系
原文总结的太好了,忍不住记录.转发. 原文地址:http://blog.csdn.net/u010168160/article/details/49182867 目录(?)[-] 一EL表达式 EL相 ...
- CSS概念 - 可视化格式模型(二) 定位概述(普通流、绝对定位)
2.定位概念 上一节熟悉了盒模型, 现在来看一下可视化格式模型和定位模型. 理解这两个模型的细微差异是非常重要的, 因为它们一起控制着如何在页面上布置每个元素 2.1 可视化格式模型 CSS有三种基本 ...
- HackTwo
使用延迟加载以及避免代码重复 一.概要: <include />标签是整理布局的有效工具,提供了合理组织XML布局文件的有效方法. ViewStub是实现延迟加载视图的优 ...
- 检查Windows上安装的.net版本
cmd reg query "HKLM\Software\Microsoft\NET Framework Setup\NDP" /s /v version | findstr /i ...
- 码云&Github 个人代码资源快速查找
1.Siri SiriShortCut
- virtueBox实现虚拟机的复制和粘贴
1.在设备--共享粘贴板--勾选双向. 2.在设备--拖放--勾选双向. 3.在设备--安装增强功能,然后进入虚拟机安装增强功能即可.
- { "result": null, "log_id": 304592860300941982, "error_msg": "image check fail", "cached": 0, "error_code": 222203, "timestamp": 1556030094 }
这个是人脸识别时无法检测到图片报的错,有时候我们检测一张图片是否在库里面,当一张图片明显在里面,还检测不到,如下面是我的代码 package Test1; import java.io.IOExcep ...
- kali linux之sqlmap
一款开源的命令行自动SQL注入工具,它能够对多种主流数据库进行扫描支持,基于Python环境. 检测动态页面中get/post参数,cookie,http头 数据榨取/文件系统访问 操作系统命令执行 ...