题目大意:维护一个字符串,支持插入字符和替换字符的操作,以及查询该字符串两个后缀的最长公共前缀长度

乍一看以为是后缀数组,然而并没有可持久化后缀数组(雾)

看题解才知道这是一道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)的更多相关文章

  1. BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6243  Solved: 2007[Submit] ...

  2. BZOJ 1014: [JSOI2008]火星人prefix Splay+二分

    1014: [JSOI2008]火星人prefix 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1014 Description 火星人 ...

  3. BZOJ 1014 [JSOI2008]火星人prefix (Splay + Hash + 二分)

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 8112  Solved: 2569[Submit] ...

  4. BZOJ 1014: [JSOI2008]火星人prefix( splay + hash )

    用splay维护序列, 二分+hash来判断LCQ.. #include<bits/stdc++.h> using namespace std; typedef unsigned long ...

  5. [BZOJ1014] [JSOI2008] 火星人prefix (splay & 二分答案)

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

  6. bzoj 1014 [JSOI2008]火星人prefix——splay+哈希

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1014 用splay维护字符串,每个点记录子树的哈希值,然后二分查询. 二分不是把两个点的哈希 ...

  7. BZOJ 1014 [JSOI2008]火星人prefix | Splay维护哈希值

    题目: 题解: #include<cstdio> #include<algorithm> #include<cstring> typedef long long l ...

  8. bzoj 1014: [JSOI2008]火星人prefix hash && splay

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3154  Solved: 948[Submit][ ...

  9. 求帮看!!!!BZOJ 1014 [JSOI2008]火星人prefix

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4164  Solved: 1277[Submit] ...

随机推荐

  1. UID和GID(详细说明)

    一.UID(User Identify)中文用户ID,相当于身份证一样,在系统中是唯一的. 用户分类centos6超级用户 UID=0 root普通用户 UID=500起 oldboy虚拟用户 UID ...

  2. 【codeforces 799A】Carrot Cakes

    [题目链接]:http://codeforces.com/contest/799/problem/A [题意] 你有一个烤炉; 每t秒能同时烤出k个蛋糕; 你可以在第一个烤炉在烤的时候;同时花费d秒建 ...

  3. 16 个 Linux 服务器监控命令

    如果你想知道你的服务器正在做干什么,你就需要了解一些基本的命令,一旦你精通了这些命令,那你就是一个 专业的 Linux 系统管理员. 有些 Linux 发行版会提供 GUI 程序来进行系统的监控,例如 ...

  4. 教你十分钟构建好 SpringBoot + SSM 框架

    目前最主流的 java web 框架应该是 SSM,而 SSM 框架由于更轻便与灵活目前受到了许多人的青睐.而 SpringBoot 的轻量化,简化项目配置, 没有 XML 配置要求等优点现在也得到了 ...

  5. 仿照CIFAR-10数据集格式,制作自己的数据集

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50801226 前一篇博客:C/C++ ...

  6. hdu 2491 贪心

    #include<stdio.h> #include<stdlib.h> #define N 110000 struct node { int u,v,len,time; }m ...

  7. Oracle-查看用户对象信息

    --视图(可查看拥有者.对象名称.创建时间.上次修改时间) SELECT t.OBJECT_NAME, t.CREATED, t.LAST_DDL_TIME FROM user_objects t o ...

  8. Linux用户管理案例(第二版)

    批量添加用户 1.按照/etc/passwd文件格式编写用户信息文件users.info xiaofang01::1001:503::/home/xiaofang01:/bin/bash  #注意不能 ...

  9. 低调、奢华、有内涵的敏捷式大数据方案:Flume+Cassandra+Presto+SpagoBI

    基于FacebookPresto+Cassandra的敏捷式大数据 文件夹 1 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 2 2.1 2.2 2.3 2.4 2.5 2.6 3 ...

  10. js保留两位小数的解决的方法

    var a = 123.456; a = a..toFixed(2); alert(a);//结果:123.46