求公共前缀的问题可以用hash+二分来解决,但这个是动态的,所以我们用平衡树来维护区间的hash值

复杂度$O(mlog^2n)$

 #include<bits/stdc++.h>
#define CLR(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pa;
const int maxn=1e5+,P=;
const ull RUA=1145141919810ll; inline ll rd(){
ll x=;char c=getchar();int neg=;
while(c<''||c>''){if(c=='-') neg=-;c=getchar();}
while(c>=''&&c<='') x=x*+c-'',c=getchar();
return x*neg;
} int ch[maxn][],pct,fa[maxn],siz[maxn];
char v[maxn],s[maxn];
ull sum[maxn],bin[maxn];
int M,rt; inline void print(int x){
if(!x) return;
print(ch[x][]);
// printf("!%d %d %d %d\n",x,ch[x][0],ch[x][1],fa[x]);
print(ch[x][]);
} inline bool isrc(int x){return x==ch[fa[x]][];} inline void update(int x){
siz[x]=siz[ch[x][]]+siz[ch[x][]]+;
sum[x]=(sum[ch[x][]]*bin[]+v[x])*bin[siz[ch[x][]]]+sum[ch[x][]];
} inline void rotate(int x){
int f=fa[x],ff=fa[f];bool b=isrc(x);
if(ff) ch[ff][isrc(f)]=x; fa[x]=ff;
if(ch[x][!b]) fa[ch[x][!b]]=f; ch[f][b]=ch[x][!b];
fa[f]=x;ch[x][!b]=f;
update(f),update(x);
} inline void splay(int x,int tar){
while(fa[x]!=tar&&fa[fa[x]]!=tar){
if(isrc(x)==isrc(fa[x])) rotate(fa[x]);
else rotate(x);rotate(x);
}if(fa[x]!=tar) rotate(x);
if(!tar) rt=x;
} inline int findkth(int k){
if(k>pct) return ;
int x=rt;
while(){
int w=siz[ch[x][]];
if(k<=w) x=ch[x][];
else if(k==w+) return x;
else k-=w+,x=ch[x][];
}
} inline void insert(int x,char y){
int p=findkth(x+);
splay(p,);
int q=findkth(x+);
splay(q,p);
int n=++pct;
sum[n]=v[n]=y,siz[n]=;fa[n]=q,ch[q][]=n;
splay(n,);
} inline void change(int x,char y){
int p=findkth(x+);
v[p]=y;update(p);
splay(p,);
} inline ull query(int x,int y){
int p=findkth(x);
if(!p) return RUA;
splay(p,);
int q=findkth(y+);
if(!q) return RUA;
splay(q,p);
return sum[ch[q][]];
} int main(){
//freopen("","r",stdin);
int i,j,k;
bin[]=;for(i=;i<=1e5;i++) bin[i]=bin[i-]*P;
ch[rt=][]=;siz[]=;
fa[pct=]=;siz[]=;
scanf("%s",s+);int len=strlen(s+);
for(i=;i<=len;i++){
insert(i-,s[i]);
}
M=rd();
for(i=;i<=M;i++){
char op[];scanf("%s",op);
if(op[]=='Q'){
int x=rd(),y=rd();
int l=,r=pct-,ans=;
while(l<=r){
int m=l+r>>;
ull a=query(x,x+m-),b=query(y,y+m-);
if(a==b&&a!=RUA) ans=m,l=m+;
else r=m-;
}
printf("%d\n",ans);
}else if(op[]=='I'){
int x=rd();scanf("%s",op);
insert(x,op[]);
}else{
int x=rd();scanf("%s",op);
change(x,op[]);
}
}
return ;
}

bzoj1014 火星人 (hash+splay+二分答案)的更多相关文章

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

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

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

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

  3. BZOJ1014: [JSOI2008]火星人prefix(splay 二分 hash)

    题意 题目链接 Sol 一眼splay + 二分hash,不过区间splay怎么写来着呀 试着写了两个小时发现死活不对 看了一下yyb的代码发现自己根本就不会splay.... // luogu-ju ...

  4. 【BZOJ-1014】火星人prefix Splay + 二分 + Hash

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

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

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

  6. BZOJ1014火星人prefix Splay維護序列 + 字符串哈希

    @[Splay, 哈希] Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:\(madamimadam\), 我们将这个字符串的各个字符予以标号 ...

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

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

  8. [JSOI2008]火星人 hash+splay

    题目描述: 现在,火星人定义了一个函数 LCQ(x, y)LCQ(x,y),表示:该字符串中第 xx 个字符开始的字串,与该字符串中第 yy 个字符开始的字串,两个字串的公共前缀的长度.比方说,LCQ ...

  9. 【BZOJ1014】火星人(Splay,哈希)

    [BZOJ1014]火星人(Splay,哈希) 题面 BZOJ 题解 要动态维护这个串,一脸的平衡树. 那么用\(Splay\)维护这个哈希值就好了. 每次计算答案的时候二分+Splay计算区间哈希值 ...

随机推荐

  1. Is there a way to avoid undeployment memory leaks in Tomcat?

    tomcat 项目部署问题 - yshy - 博客园http://www.cnblogs.com/yshyee/p/3973293.html jsp - tomcat - their classes ...

  2. C语言操作WINDOWS系统存储区数字证书相关函数详解及实例

     C语言操作WINDOWS系统存储区数字证书相关函数详解及实例 以下代码使用C++实现遍历存储区证书及使用UI选择一个证书 --使用CertOpenSystemStore打开证书存储区. --在循环中 ...

  3. React-Native之截图组件view-shot的介绍与使用

    React-Native之截图组件view-shot的介绍与使用 一,需求分析 1,需要将分享页生成图片,并分享到微信好友与朋友圈. 二,react-native-view-shot介绍 1,可以截取 ...

  4. class用法

    自 PHP 5.5 起,关键词 class 也可用于类名的解析.使用 ClassName::class 你可以获取一个字符串,包含了类 ClassName 的完全限定名称.这对使用了 命名空间 的类尤 ...

  5. innerText兼容问题处理

    IE.Safari.Opera和Chrome支持innerText属性.低版本的火狐浏览器不支持,但支持作用类似的textContent属性.textContent是DOM3级规定的一个属性,而且也得 ...

  6. C# Note14: Editable WPF ListView

    (1)https://stackoverflow.com/questions/5652527/editable-wpf-listview (2)How to: Create a ListView wi ...

  7. 使用Elasticsearch 出现的拒绝连接

    pom 文件 spring: elasticsearch: jest: uris: http://192.168.124.142:9201 # data: # elasticsearch: # clu ...

  8. Spring 配置详解

    spring4配置文件详解 一.配置数据源 基本的加载properties配置文件 <context:property-placeholder location="classpath* ...

  9. 一、linux扩展

    1.linux-解压bz2文件提示tar (child): bzip2: Cannot exec: No such file or directory 原因,linux下没有bzip2解压工具 安装b ...

  10. Hibernate 配置文件hibernate.cfg.xml的详细

    <!--标准的XML文件的起始行,version='1.0'表明XML的版本,encoding='gb2312'表明XML文件的编码方式-->               <?xml ...