传送门

很久以前xzz大佬就喊我做这题,结果现在才做qwq

因为要在序列中插入,所以直接用\(Splay\)维护这个串的哈希值,插入就直接把那个点插♂进去,修改就把点旋到根,然后修改和pushup,询问的话可以考虑二分,check就看两个对应区间哈希值是否相等

#include<bits/stdc++.h>
#define LL long long
#define il inline
#define re register
#define ull unsigned long long using namespace std;
const int N=150000+10;
il int rd()
{
int x=0,w=1;char ch=0;
while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
return x*w;
}
int n,q;
char cc[N];
int rt,tt,fa[N],ch[N][2],sz[N];
ull a[N],ha[N],bs=377,bb[N];
il void psup(int x)
{
sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;
ha[x]=ha[ch[x][0]]*bb[sz[ch[x][1]]+1]+a[x]*bb[sz[ch[x][1]]]+ha[ch[x][1]];
}
il void rot(int x)
{
int y=fa[x],z=fa[y],yy=ch[y][1]==x,w=ch[x][!yy];
ch[z][ch[z][1]==y]=x;fa[x]=z;
ch[y][yy]=w;fa[w]=y;
ch[x][!yy]=y,fa[y]=x;
psup(y),psup(x);
}
il void spl(int x,int en)
{
if(!x) return;
while(fa[x]!=en)
{
int y=fa[x],z=fa[y];
if(z!=en) ((ch[y][1]==x)^(ch[z][1]==y))?rot(x):rot(y);
rot(x);
}
if(!en) rt=x;
}
il int gkth(int x)
{
if(x<=0||x>n+2) return 0;
int nw=rt;
while(233)
{
if(x<=sz[ch[nw][0]]) nw=ch[nw][0];
else if(x>sz[ch[nw][0]]+1) x-=sz[ch[nw][0]]+1,nw=ch[nw][1];
else return nw;
}
}
il int bui(int l,int r)
{
if(l>r) return 0;
int mid=(l+r)>>1,nw=++tt;
sz[nw]=1,a[nw]=(cc[mid-1]>='a')?(ull)(cc[mid-1]-'a'):0;
fa[ch[nw][0]=bui(l,mid-1)]=nw;
fa[ch[nw][1]=bui(mid+1,r)]=nw;
psup(nw);
return nw;
} int main()
{
bb[0]=1;
for(int i=1;i<=150000;++i) bb[i]=bb[i-1]*bs;
scanf("%s",cc+1);
n=strlen(cc+1);
rt=bui(1,n+2);
q=rd();
char ss[2];
while(q--)
{
scanf("%s",ss);
if(ss[0]=='Q')
{
int x=rd(),y=rd();
if(x>y) swap(x,y);
int ll=gkth(x),rr=gkth(y),l=1,r=n-y+1,an=0;
while(l<=r)
{
int mid=(l+r)>>1;
ull h1,h2;
int zz=gkth(x+mid+1);//x+mid-1+2
spl(ll,0),spl(zz,ll),h1=ha[ch[zz][0]];
zz=gkth(y+mid+1);
spl(rr,0),spl(zz,rr),h2=ha[ch[zz][0]];
if(h1==h2) an=mid,l=mid+1;
else r=mid-1;
}
printf("%d\n",an);
}
else if(ss[0]=='R')
{
int x=gkth(rd()+1);
scanf("%s",ss);
spl(x,0),a[x]=ss[0]-'a',psup(x);
}
else
{
int x=gkth(rd()+1),nw=++tt;
++n;
scanf("%s",ss);
spl(x,0);
a[nw]=ss[0]-'a',fa[nw]=x;
fa[ch[x][1]]=nw,ch[nw][1]=ch[x][1],ch[x][1]=nw;
psup(nw),psup(x);
}
}
return 0;
}

luogu P4036 [JSOI2008]火星人的更多相关文章

  1. P4036 [JSOI2008]火星人(splay+hash+二分)

    P4036 [JSOI2008]火星人 Splay维护hash,查询二分 $a[x].vl=a[lc].vl*ha[a[rc].sz+1]+a[x].w*ha[a[rc].sz]+a[rc].vl$ ...

  2. bzoj 1014: 洛谷 P4036: [JSOI2008]火星人

    题目传送门:洛谷P4036. 题意简述: 有一个字符串,支持插入字符,修改字符. 每次需要查询两个后缀的LCP长度. 最终字符串长度\(\le 100,\!000\),修改和询问的总个数\(\le 1 ...

  3. 洛谷 P4036 [JSOI2008]火星人(splay+字符串hash)

    题面 洛谷 题解 首先,我们知道求最长公共前缀可以用二分答案+hash来求 因为有修改操作, 考虑将整个字符串的hash值放入splay中 接着就是splay的基本操作了 Code #include& ...

  4. 1014: [JSOI2008]火星人prefix

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MB Description 火星人最近研究了一种操作:求一个字串两个后缀 ...

  5. [BZOJ1014][JSOI2008]火星人prefix

    [BZOJ1014][JSOI2008]火星人prefix 试题描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字 ...

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

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

  7. 【bzoj1014】[JSOI2008]火星人prefix

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

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

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

  9. JSOI2008 火星人prefix

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

随机推荐

  1. Delphi7/2007/2009/2010/XE/XE2/XE3/XE4/XE5/XE6/XE7/XE8/10最终版

    RAD Studio 10.1 Berlin(with Update1)http://altd.embarcadero.com/download/radstudio/10.1/delphicbuild ...

  2. Delphi2007精简版加载Borland.Studio.Together.dll错误解决办法

    安装Delphi2007精简版,启动提示Borland.Studio.Together.dll加载错误,错误信息如下: Failed to load IDE add in 'C:\Program Fi ...

  3. 简单谈谈DNS协议

    DNS协议也可以称为DNS服务,全称是Domain Name System,即域名系统,和HTTP协议一样,也是一个位于应用层的协议(服务),它是基于运输层的UDP协议的,关于网络协议的分层介绍,见这 ...

  4. LODOP、C-LODOP注册号的区别

    LODOP是一款免费的web打印控件,预览打印后无水印,是免费的,直接打印会在纸张下方有个水印“本页由XXX试用版输出”,通常商用打印较多,常用直接打印,这种时候可以购买注册号去水印. LODOP注册 ...

  5. codeforces469B

    Chat Online CodeForces - 469B 问题描述 你和你的朋友经常在网上聊天.你的朋友作息规律每天只会在p个时间段[ai,bi]在线.你作息混乱,假设你在t时刻起床,那么你会在q个 ...

  6. shell特殊变量的使用

    $0 当前脚本名$# 传递给脚本的参数个数$1 第一个参数,依次类推$*,$@ 所有参数 $?      上一条命令的执行返回结果$$ 当前shell进程ID $PPID 父进程ID 下面的shell ...

  7. linux shell脚本报错总结

    1  rizhi.sh: line 28: warning: here-document at line 9 delimited by end-of-file (wanted `EOF') 原因是末尾 ...

  8. BZOJ1433[ZJOI2009]假期的宿舍——二分图最大匹配

    题目描述 学校放假了······有些同学回家了,而有些同学则有以前的好朋友来探访,那么住宿就是一个问题.比如A 和B都是学校的学生,A要回家,而C来看B,C与A不认识.我们假设每个人只能睡和自己直接认 ...

  9. Mercurial(Hg)基本操作

    Mercurial(Hg)基本操作 来源 https://www.cnblogs.com/gb2013/archive/2012/05/18/Mercurial_Basic.html Mercuria ...

  10. jsp操作MySQL时报错:Operation not allowed after ResultSet closed

    一个stmt对多个rs进行操作引起的ResultSet关闭的错误 解决办法:创建新的stmt,一个rs对应一个stmt