Topic:
Exercises
#include <cstdio>#include<algorithm>#include<cstring>typedefLong Longll;#defineN 100010#defineBase 29#definewhich (x) (ls[fa[(x)]]== (x))using namespacestd;intN,m,idx,root,fa[n],ls[n],rs[n],val[n],sz[n],a,b;ll Hsh[n],pw[n];Chars[n],op[Ten];voidUpTintu) {Sz[u]=sz[ls[u]]+sz[rs[u]]+1; Hsh[u]=hsh[ls[u]]+val[u]*pw[sz[ls[u]]]+hsh[rs[u]]*pw[sz[ls[u]]+1];}voidRotateintu) { intV=fa[u],w=fa[v],b=which (u)?Rs[u]:ls[u]; if(w) which (v)? ls[w]=u:rs[w]=T; 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);}voidSplay (intUinttar) { 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;}intBuildintLintRintpre) { if(L>r)return 0; intU=++idx,mid=l+r>>1; Val[u]=s[mid]-'a'+1, fa[u]=Pre; Ls[u]=build (l,mid-1, u); Rs[u]=build (mid+1, R,u); UPT (U); returnu;}intFindintx) { intu=Root; while(sz[ls[u]]!=x)if(x<=sz[ls[u]]-1) u=Ls[u]; Elsex-=sz[ls[u]]+1, u=Rs[u]; returnu;}voidInsertintPosintx) { intU=find (POS), V=find (pos+1); Splay (U,0), splay (V,u); LS[V]=++idx,fa[idx]=v,val[idx]=x,sz[idx]=1; Splay (IDX,0);}voidChangeintPosintx) { intu=find (POS); Val[u]=x; Splay (U,0);} ll Gethsh (intPosintLen) { intU=find (pos-1), V=find (pos+Len); Splay (U,0), splay (V,u); returnHsh[ls[v]];}intQueryintAintb) { intL=0, R=sz[root]-max (A, B)-1, Mid; while(l<r) {Mid=l+r+1>>1; if(Gethsh (A,mid) ==gethsh (b,mid)) l=mid; Elser=mid-1; } returnl;}intMain () {scanf ("%s", s+1); N=strlen (s+1); pw[0]=1; for(intI=1; i<n;i++) Pw[i]=pw[i-1]*Base; Root=build (0, n+1,0); scanf ("%d",&m); while(m--) {scanf ("%s", op); if(op[0]=='Q') {scanf ("%d%d",&a,&b); printf ("%d\n", query (A, b)); } Else if(op[0]=='I') { intPOS; scanf ("%d%s",&Pos,op); Insert (pos,op[0]-'a'+1); } Else if(op[0]=='R') { intPOS; scanf ("%d%s",&Pos,op); Change (pos,op[0]-'a'+1); } } return 0;}
Bzoj 1014 [JSOI2008] Mars man prefix | Splay Maintaining hash values