BZOJ 1014 [JSOI2008]火星人prefix (splay+二分答案+字符串hash)
题目大意:维护一个字符串,支持插入字符和替换字符的操作,以及查询该字符串两个后缀的最长公共前缀长度
乍一看以为是后缀数组,然而并没有可持久化后缀数组(雾)
看题解才知道这是一道splay题,首先要对splay维护区间信息有一定了解
splay维护,插入字符,替换字符
而它的字树内所有儿子的中序遍历的hash值也可以通过splay维护
(这个推导式似乎烂大街了)
而后缀就是把i-1拎到根节点,然后把n+1拎到根节点的右儿子上,它的左儿子表示的就是hash值
至于如何查公共前缀呢?二分答案啊!询问的总时间是
这个二分答案的check函数 的满足条件并非常规的>=或者<=,而是==,所以为了防止正确答案被略掉,每次二分的过程中都记录一次mid作为答案,而加下来mid在查询范围内已经没有意义了,所以是r=mid-1而不是r=mid
我一开始插入打错了竟然还有80分,数据太水了hhhhh
#include <cstdio>
#include <cstring>
#include <algorithm>
#define il inline
#define ui unsigned int
#define ull unsigned long long
#define seed 13131
#define N 110000
#define root d[0].ch[1]
using namespace std; char str[N],Q[],q[];
ull pw[N];
int n,m,tot;
struct SPLAY{int fa,ch[],sz;ull hsh,val;}d[N];
int gc()
{
int rett=,fh=;char p=getchar();
while(p<''||p>''){if(p=='-')fh=-;p=getchar();}
while(p>=''&&p<=''){rett=(rett<<)+(rett<<)+p-'';p=getchar();}
return rett*fh;
}
il ull idx(char c){return c-'a'+;}
il void con(int x,int ff,int p){d[x].fa=ff,d[ff].ch[p]=x;}
il int idf(int x){return d[d[x].fa].ch[]==x?:;}
il int cre(ull w){tot++;d[tot].val=w,d[tot].sz=,d[tot].hsh=w;return tot;}
il void pushup(int x)
{
d[x].sz=d[d[x].ch[]].sz+d[d[x].ch[]].sz+;
d[x].hsh=d[d[x].ch[]].hsh*pw[d[d[x].ch[]].sz+]+(ull)d[x].val*pw[d[d[x].ch[]].sz]+d[d[x].ch[]].hsh;
}
il void rot(int x)
{
int y=d[x].fa;int ff=d[y].fa;int px=idf(x);int py=idf(y);
con(d[x].ch[px^],y,px),con(y,x,px^),con(x,ff,py);
pushup(y),pushup(x);
}
void splay(int x,int to)
{
to=d[to].fa;
while(d[x].fa!=to){
int y=d[x].fa;
if(d[y].fa==to) rot(x);
else if(idf(x)==idf(y)){rot(y);rot(x);}
else{rot(x);rot(x);}
}
}
int build(int l,int r,int ff)
{
if(l>r) return ;
int mid=(l+r)>>;
int pos=cre(idx(str[mid]));
d[pos].fa=ff;
d[pos].ch[]=build(l,mid-,pos);
d[pos].ch[]=build(mid+,r,pos);
pushup(pos);
return pos;
}
int find_pos(int pos)
{
int x=root;
while(x)
{
if(d[d[x].ch[]].sz>=pos) x=d[x].ch[];
else{
pos-=d[d[x].ch[]].sz;
if(pos==) return x;
pos--,x=d[x].ch[];
}
}
return x;
}
void ins(int pos,ull w)
{
int ff=find_pos(pos+),x,y;splay(ff,root);
if(!d[ff].ch[])
x=cre(w),con(x,ff,),pushup(ff);
else{
x=d[ff].ch[];
while(x)
if(d[x].ch[]) x=d[x].ch[];
else break;
y=cre(w),con(y,x,),pushup(x);
splay(x,root);
}
}
void replac(int pos,ull w)
{
int x=find_pos(pos+);splay(x,root);
d[x].val=w;pushup(x);
}
il ull get_hsh(int pos)
{
int x=find_pos(pos);
splay(x,root);
int y=find_pos(n+);
splay(y,d[x].ch[]);
return d[d[d[x].ch[]].ch[]].hsh;
}
il int check(int x,int y,int mid)
{
ull s1=,s2=,s3=,s4=;
s1=get_hsh(x);
if(x+mid<=n) s2=get_hsh(x+mid);
s3=get_hsh(y);
if(y+mid<=n) s4=get_hsh(y+mid);
return (s1-s2==(s3-s4)*pw[y-x])?:;
}
int Query(int a,int b)
{
if(a>b){int t=a;a=b;b=t;}
int l=,r=n-b+,ans=;
while(l<=r){
int mid=(l+r)>>;
if(check(a,b,mid)) ans=mid,l=mid+;
else r=mid-;
}
return ans;
}
void stt()
{
n=strlen(str+);pw[]=;
for(int i=;i<=;i++) pw[i]=pw[i-]*seed;
int x=cre(),y=cre(),z;
con(x,,),con(y,,);
z=build(,n,y);
con(z,y,);
pushup(y),pushup(x);
} int main()
{
scanf("%s",str+);
stt();
scanf("%d",&m);
int x,y;
for(int i=;i<=m;i++)
{
scanf("%s",Q);
if(Q[]=='Q'){
x=gc(),y=gc();
printf("%d\n",Query(x,y));
}else if(Q[]=='I'){
x=gc();scanf("%s",q);
ins(x,idx(q[]));
n++;
}else{
x=gc();scanf("%s",q);
replac(x,idx(q[]));
}
}
return ;
}
BZOJ 1014 [JSOI2008]火星人prefix (splay+二分答案+字符串hash)的更多相关文章
- 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 + 二分)
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 8112 Solved: 2569[Submit] ...
- BZOJ 1014: [JSOI2008]火星人prefix( splay + hash )
用splay维护序列, 二分+hash来判断LCQ.. #include<bits/stdc++.h> using namespace std; typedef unsigned long ...
- [BZOJ1014] [JSOI2008] 火星人prefix (splay & 二分答案)
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- bzoj 1014 [JSOI2008]火星人prefix——splay+哈希
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1014 用splay维护字符串,每个点记录子树的哈希值,然后二分查询. 二分不是把两个点的哈希 ...
- BZOJ 1014 [JSOI2008]火星人prefix | Splay维护哈希值
题目: 题解: #include<cstdio> #include<algorithm> #include<cstring> typedef long long l ...
- 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] ...
随机推荐
- 再见,OI(2019退役祭)
有些话应该藏在心里,有些事情只属于自己. (想了一下,自己的OI生涯.自己所经历的事情还是留在自己的心里吧,一是自己文笔不好,二是每个人的世界观不同对事情的看法不同) 不要轻易地去评价一个人,每个人背 ...
- nyoj254-编号统计
编号统计 时间限制:2000 ms | 内存限制:65535 KB 难度:2 描述 zyc最近比较无聊,于是他想去做一次无聊的统计一下.他把全校同学的地址都统计了一下(zyc都将地址转化成了编码) ...
- [NoiPlus2016]换教室
flag++ //Writer : Hsz %WJMZBMR%tourist%hzwer #include <iostream> #include <cstdio> #incl ...
- Java Web学习总结(27)——JavaEE中Web服务器、Web容器、Application服务器区别及联系
在JavaEE 开发Web中,我们经常会听到Web服务器(Web Server).Web容器(Web Container).应用服务器(Application Server),等容易混淆不好理解名词. ...
- 修改struts2自定义标签的源代码,在原有基础上增加功能(用于OA项目权限判断,是否显示某个权限)
OA项目在做权限判断时 原始方式: 现在完成的功能 :通过改变struts2自定标签源代码 在原有的基础上 增加判断权限的功能 而页面上使用标签的方式 还是下图 步骤: 打开文件 搜索< ...
- Innodb性能优化之参数设置
现在,Innodb是Mysql最多使用的存储引擎.其性能一直广受关注.本文通过基本的参数设置来提高其性能. innodb_buffer_pool_size 缓冲池大小.这是innodb参数中最重要的设 ...
- Eclipse设置jdk相关
2.window->preferences->java->Compiler->设置右侧的Compiler compliance level 3.window->prefe ...
- Zookeeper源代码编译为Eclipseproject(win7下Ant编译)
为了深入学习ZooKeeper源代码,首先就想到将其导入到Eclispe中,所以要先将其编译为Eclispeproject. 1.什么是Ant??? Apache Ant™ Apache Ant is ...
- oracle常见sql语句优化
1.* 号引起的运行效率 尽量降低使用select * 来进行查询,当你查询使用*, 数据库会进行解析并将*转换为所有列. select count(si.student_id) from Stud ...
- Chisel Tutorial(一)——Chisel介绍
Chisel是由伯克利大学公布的一种开源硬件构建语言,建立在Scala语言之上,是Scala特定领域语言的一个应用,具有高度參数化的生成器(highly parameterized generator ...