题目:


题解:

#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维护哈希值的更多相关文章

  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 )

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

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

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

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

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

  6. BZOJ 1014 [JSOI2008]火星人prefix (splay+二分答案+字符串hash)

    题目大意:维护一个字符串,支持插入字符和替换字符的操作,以及查询该字符串两个后缀的最长公共前缀长度 乍一看以为是后缀数组,然而并没有可持久化后缀数组(雾) 看题解才知道这是一道splay题,首先要对s ...

  7. [bzoj1014](JSOI2008)火星人 prefix (Splay维护哈希)

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

  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. 详解MessageBox(),MsgBox函数的正确使用

    //或者使用chr(13),chr(10)效果一样 MsgBox "a"&chr(13)&"b"&chr(10)&"c ...

  2. pwn的一些环境搭建

    <1>pwntools库安装 pwntools是一个CTF框架和漏洞利用开发库,用Python开发,由rapid设计,旨在让使用者简单快速的编写exploit. 本文将基于KUbuntu ...

  3. Centos7-Mysql-5.6.41一主两从的搭建

    01.准备工作 首先的前提条件你必须安装了mysql,而且知道你安装mysql配置文件的位置,接下来的事情就好办了. 我的搭建环境: 服务器1: 10.233.17.20 mysql-master(主 ...

  4. PHP实现qq三方登录

    除了qq第三方登录外.还有微博,微信等第三方登录 qq第三方登录,遵循oauth2.0协议 这里是说明http://www.cnblogs.com/yx520zhao/p/6616686.html q ...

  5. 关于ZYNQ-700是否支持大容量SD卡汇报

    关于ZYNQ-700是否支持大容量SD卡 不支持. 下午问了客服的FAE给的答案是不清楚,我自己调研了一下为什么. 调查结果: 1. 大容量的SD卡为什么不支持? SD2.0规范中(SDHC)硬件支持 ...

  6. C# 不同窗口传递参数

    form1: private void button1_Click(object sender, System.EventArgs e) { Form2 frm = new Form2(); frm. ...

  7. POJ:1222-EXTENDED LIGHTS OUT(矩阵反转)

    EXTENDED LIGHTS OUT Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12956 Accepted: 8186 ...

  8. 基于HDP版本的YDB安装部署(转)

    第三章 YDB依赖环境准备 一.硬件环境 硬件如何搭配,能做到比较高的性价比,不存在短板.合理的硬件搭配,对系统的稳定性也很关键. 1.CPU不是核数越高越好,性价比才是关键. 经常遇到很多的企业级客 ...

  9. 自定义注解不能拦截controller层

    1,首先在springMVC的配置文件中,webapp/WEB-INF/servlet.xml,加上AOP的相关内容: <beans xmlns="http://www.springf ...

  10. Android面试收集录12 View测量、布局及绘制原理

    一.View绘制的流程框架 View的绘制是从上往下一层层迭代下来的.DecorView-->ViewGroup(--->ViewGroup)-->View ,按照这个流程从上往下, ...