bzoj1014 splay

#include
#define N 165009
#define MO 998244353
#define MO2 1000000009
using namespace std;
inline int read()
{
	char c=getchar(); int ret=0;
	while(c<48||c>57)c=getchar();
	while(c>=48&&c<=57)ret=ret*10+c-48,c=getchar();
	return ret;
}

int h3,h4,op2[N],t2[N],h[55],h1,h2,t[N],op[N],mid,l,r,ans,nn,x,y,u,v,c,rt,fa[N],a[N],ls[N],rs[N],si[N],g[N],cnt,n,m;
char s[N],cc[N],ss[N];

inline void upt(int p)
{
	si[p]=si[ls[p]]+si[rs[p]]+1;  
	t[p]=((long long)t[ls[p]]*op[si[rs[p]]+1]+(long long)g[p]*op[si[rs[p]]]+t[rs[p]])%MO;
	t2[p]=((long long)t2[ls[p]]*op2[si[rs[p]]+1]+(long long)g[p]*op2[si[rs[p]]]+t2[rs[p]])%MO2;
}

void build(int & p, int l, int r)
{
	if(l>r)return;
	p=++cnt; 
	if(l==r){
		g[p]=t[p]=t2[p]=a[l];
		si[p]=1;
		return;
	}
	int mid=(l+r)>>1;
	g[p]=a[mid];
	build(ls[p],l,mid-1);
	build(rs[p],mid+1,r);
	if(ls[p])fa[ls[p]]=p;
	if(rs[p])fa[rs[p]]=p;
	upt(p);
}

inline void rot(int x) 
{
	int y=fa[x],z=fa[y],b;
	if(x==ls[y])b=rs[x]; else b=ls[x];
	fa[x]=z; fa[y]=x; if(b)fa[b]=y;  
	if(z){
		if(ls[z]==y)ls[z]=x; else rs[z]=x;
	}
	if(x==ls[y]){
		ls[y]=b;
		rs[x]=y;
	}
	else{
		rs[y]=b;
		ls[x]=y;
	}
	upt(y);
}

inline int whi(int p)
{
	return ls[fa[p]]==p;
}

inline void splay(int p, int rt_)
{
	while(fa[p]!=rt_)
	{
		if(fa[fa[p]]!=rt_){
			if(whi(p)==whi(fa[p]))rot(fa[p]);
			else rot(p);
		}
		rot(p);
	}
	if(!rt_)rt=p;
	upt(p);
}

void ch(int p, int k)
{
	if(k<=si[ls[p]]){
		ch(ls[p],k);
		upt(p);
		return;
	}
	k-=si[ls[p]];
	if(k==1){
		g[p]=c;
		upt(p);
		return; 
	}
	ch(rs[p],k-1);
	upt(p);
}

inline int fid(int p, int k)
{
	if(si[ls[p]]>=k)return fid(ls[p],k);
	k-=si[ls[p]];
	if(k==1)return p;
	return fid(rs[p],k-1);
}

inline void ins(int k)
{
	u=fid(rt,k);
	splay(u,0);
	v=fid(rt,k+1);
	splay(v,u);

    t[++cnt]=c; 
    t2[cnt]=c;
    g[cnt]=c;
    si[cnt]=1;
    
	ls[v]=cnt;
	fa[cnt]=v;
	upt(v);
	upt(u);
}
inline int che(int k)
{
	u=fid(rt,x-1);
	v=fid(rt,x+k);
	splay(u,0);
	splay(v,u);
	h1=t[ls[v]];
	h3=t2[ls[v]];
	
	u=fid(rt,y-1);
	v=fid(rt,y+k);
	splay(u,0);
	splay(v,u);
	h2=t[ls[v]];
	h4=t2[ls[v]];
	
	return h1==h2&&h3==h4;
}

int main()
{
	scanf("%s",s+1);
	n=strlen(s+1);
	a[0]=30; a[n+1]=34;
	for(int i=1; i<=n; ++i)a[i]=s[i]-'a'+1;
	op[0]=1; op2[0]=1; nn=n+1; 
	for(int i=1; i<=150005; ++i)op[i]=(long long)op[i-1]*37%MO;
	for(int i=1; i<=150005; ++i)op2[i]=(long long)op2[i-1]*41%MO2;
	build(rt,0,n+1);
	
	m=read();
	while(m--)
	{
		scanf("%s",ss);
		if(ss[0]=='Q'){
			ans=0; x=read(); y=read(); ++x; ++y; 
			l=1; r=min(n-x+2,n-y+2);
			while(l<=r){
				mid=(l+r)>>1;
				if(che(mid)){
					ans=mid;
					l=mid+1;
				}
				else r=mid-1;
			}
			printf("%d\n",ans);
			continue;
		}
		if(ss[0]=='R'){  
			x=read(); ++x;
			scanf("%s",cc); c=cc[0]-96;
			ch(rt,x);
			continue;
		}
		x=read(); scanf("%s",cc); c=cc[0]-96;
		++x;
		++n;
		ins(x);
	}
}

 
 

你可能感兴趣的:(bzoj,splay)