BZOJ 1014 [JSOI2008]火星人prefix | Splay维护哈希值
题目:
题解:
#include<cstdio>
#include<algorithm>
#include<cstring>
typedef long long ll;
#define N 100010
#define Base 29
#define which(x) (ls[fa[(x)]]==(x))
using namespace std;
int n,m,idx,root,fa[N],ls[N],rs[N],val[N],sz[N],a,b;
ll hsh[N],pw[N];
char s[N],op[];
void upt(int u)
{
sz[u]=sz[ls[u]]+sz[rs[u]]+;
hsh[u]=hsh[ls[u]]+val[u]*pw[sz[ls[u]]]+hsh[rs[u]]*pw[sz[ls[u]]+];
}
void rotate(int u)
{
int v=fa[u],w=fa[v],b=which(u)?rs[u]:ls[u];
if (w) which(v)?ls[w]=u:rs[w]=u;
which(u)?(ls[v]=b,rs[u]=v):(rs[v]=b,ls[u]=v);
fa[u]=w,fa[v]=u;
if (b) fa[b]=v;
upt(v),upt(u);
}
void splay(int u,int tar)
{
while (fa[u]!=tar)
{
if (fa[fa[u]]!=tar)
(which(u)==which(fa[u]))?rotate(fa[u]):rotate(u);
rotate(u);
}
if (!tar) root=u;
}
int build(int l,int r,int pre)
{
if (l>r) return ;
int u=++idx,mid=l+r>>;
val[u]=s[mid]-'a'+,fa[u]=pre;
ls[u]=build(l,mid-,u);
rs[u]=build(mid+,r,u);
upt(u);
return u;
}
int find(int x)
{
int u=root;
while(sz[ls[u]]!=x)
if (x<=sz[ls[u]]-) u=ls[u];
else x-=sz[ls[u]]+,u=rs[u];
return u;
}
void insert(int pos,int x)
{
int u=find(pos),v=find(pos+);
splay(u,),splay(v,u);
ls[v]=++idx,fa[idx]=v,val[idx]=x,sz[idx]=;
splay(idx,);
}
void change(int pos,int x)
{
int u=find(pos);
val[u]=x;
splay(u,);
}
ll gethsh(int pos,int len)
{
int u=find(pos-),v=find(pos+len);
splay(u,),splay(v,u);
return hsh[ls[v]];
}
int query(int a,int b)
{
int l=,r=sz[root]-max(a,b)-,mid;
while (l<r)
{
mid=l+r+>>;
if (gethsh(a,mid)==gethsh(b,mid)) l=mid;
else r=mid-;
}
return l;
}
int main()
{
scanf("%s",s+);
n=strlen(s+);
pw[]=;
for (int i=;i<N;i++)
pw[i]=pw[i-]*Base;
root=build(,n+,);
scanf("%d",&m);
while (m--)
{
scanf("%s",op);
if (op[]=='Q')
{
scanf("%d%d",&a,&b);
printf("%d\n",query(a,b));
}
else if (op[]=='I')
{
int pos;
scanf("%d%s",&pos,op);
insert(pos,op[]-'a'+);
}
else if (op[]=='R')
{
int pos;
scanf("%d%s",&pos,op);
change(pos,op[]-'a'+);
}
}
return ;
}
BZOJ 1014 [JSOI2008]火星人prefix | Splay维护哈希值的更多相关文章
- BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6243 Solved: 2007[Submit] ...
- BZOJ 1014: [JSOI2008]火星人prefix Splay+二分
1014: [JSOI2008]火星人prefix 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1014 Description 火星人 ...
- BZOJ 1014: [JSOI2008]火星人prefix( splay + hash )
用splay维护序列, 二分+hash来判断LCQ.. #include<bits/stdc++.h> using namespace std; typedef unsigned long ...
- BZOJ 1014 [JSOI2008]火星人prefix (Splay + Hash + 二分)
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 8112 Solved: 2569[Submit] ...
- bzoj 1014 [JSOI2008]火星人prefix——splay+哈希
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1014 用splay维护字符串,每个点记录子树的哈希值,然后二分查询. 二分不是把两个点的哈希 ...
- BZOJ 1014 [JSOI2008]火星人prefix (splay+二分答案+字符串hash)
题目大意:维护一个字符串,支持插入字符和替换字符的操作,以及查询该字符串两个后缀的最长公共前缀长度 乍一看以为是后缀数组,然而并没有可持久化后缀数组(雾) 看题解才知道这是一道splay题,首先要对s ...
- [bzoj1014](JSOI2008)火星人 prefix (Splay维护哈希)
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀. 比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 ...
- bzoj 1014: [JSOI2008]火星人prefix hash && splay
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3154 Solved: 948[Submit][ ...
- 求帮看!!!!BZOJ 1014 [JSOI2008]火星人prefix
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4164 Solved: 1277[Submit] ...
随机推荐
- 可复用 React 的 HOC 以及的 Render Props
重复是不可能的,这辈子都不可能写重复的代码 当然,这句话分分钟都要被产品(领导)打脸,真的最后一次改需求,我们烦恼于频繁修改的需求 虽然我们不能改变别人,但我们却可以尝试去做的更好,我们需要抽象,封装 ...
- flask实现基于elasticsearch的关键词搜索建议
1.实现效果 2.fuzzy模糊查询和suggest查询 fuzzy模糊查询 GET chaxun/job/_search { "query": { "fuzzy&quo ...
- 即将开始的python之路
准备开始学py 记录一下 加油
- Python常用函数记录
Python常用函数/方法记录 一. Python的random模块: 导入模块: import random 1. random()方法: 如上如可知该函数返回一个[0,1)(左闭右开)的一个随机的 ...
- 为 dll (类库) 解决方案添加测试项目
解决方案中新建项目, 添加引用, "解决方案" -> "项目", 选中即可, 而非直接添加 dll, 这会导致编译出错
- Spark 源码阅读——任务提交过程
当我们在使用spark编写mr作业是,最后都要涉及到调用reduce,foreach或者是count这类action来触发作业的提交,所以,当我们查看这些方法的源码时,发现底层都调用了SparkCon ...
- TP-常见错误1
1.无法加载模块 FILE: D:\wamp64\www\www.test_shop.com\ThinkPHP\Library\Think\Dispatcher.class.php LINE: 178 ...
- 15.9,python操作redis集群
上代码 .对redis的单实例进行连接操作 python3 >>>import redis >>>r = redis.StrictRedis(host=, db ...
- label标签的作用
在用户注册的时候,常常用户点击文字就需要将光标聚焦到对应的表单上面,这个是怎么实现的呢?就是下面我要介绍的<label>标签的for属性 定义:for 属性规定 label 与哪个表单元素 ...
- Centos 7.X 安装JDK1.8
一.查看本机jdk版本并卸载原有openjdk 查看 # java -version openjdk version "1.8.0_144" ...