"Algorithm" splay
The "Keys" for each node maintains its subtree hash value, preceded by a high, followed by a low.
Sum[x]=sum[l]*base[s[r]+1]+a[x]*base[s[r]]+sum[r], where sum is a hash, base is the multiplicative weight, and A is a numeric value (that is, a character).
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intmaxn=100010, inf=0x3f3f3f3f, bases=233;intf[maxn],t[maxn][2],s[maxn],a[maxn],a[maxn],sz=0, root,n;unsignedLong LongSUM[MAXN],A_,B_,B[MAXN];intNode (intFaintnum) {SZ++; F[SZ]=fa;t[sz][0]=t[sz][1]=0; S[SZ]=1; a[sz]=sum[sz]=num; returnsz;}voidCountintx) {S[x]=s[t[x][0]]+1+s[t[x][1]]; SUM[X]=sum[t[x][0]]*b[s[t[x][1]]+1]+1ull*a[x]*b[s[t[x][1]]]+sum[t[x][1]];}voidBuildintFaint&x,intLintR) { if(L>r)return; intMid= (l+r) >>1; X=Node (Fa,a[mid]); Build (x,t[x][0],l,mid-1); Build (x,t[x][1],mid+1, R); Count (x);}voidRotateintx) { intk=x==t[f[x]][1]; inty=F[x]; T[Y][K]=t[x][!k];f[t[x][!k]]=y; if(F[y]) t[f[y]][y==t[f[y]][1]]=x;f[x]=f[y];f[y]=x; t[x][!k]=y; SUM[X]=sum[y];s[x]=S[y]; Count (y);}voidSplay (intXintR) { for(intfa=f[r];f[x]!=FA;) { if(F[F[X]]==FA) {rotate (x);return;} intx=x==t[x][1],y=f[x]==t[f[f[x]]][1]; if(x^Y) rotate (x), rotate (x); Elserotate (f[x]), rotate (x); }}voidFindint&x,intk) { for(intI=x;i;) { if(k<=s[t[i][0]]) {i=t[i][0];Continue;} if(k==s[t[i][0]]+1) {splay (i,x); x=i;return;} K-=s[t[i][0]]+1; i=t[i][1]; }}BOOLWorkintXintYintlongs) { if(x+longs-1>n| | y+longs-1>n)return 0; Find (root,x); Find (t[root][1],longs+1); A_=sum[t[t[root][1]][0]]; Find (Root,y); Find (t[root][1],longs+1); B_=sum[t[t[root][1]][0]]; if(a_==b_)return 1; return 0;}voidAsk () {intx, y; scanf ("%d%d",&x,&y); intL=0, r=MAXN; while(l<r) {intMid= (l+r) >>1; if(Work (X,Y,MID)) l=mid+1; ElseR=mid; } printf ("%d\n", L-1);}voidrepair () {intXCharC; scanf ("%d%c",&x,&c); Find (Root,x+1); A[root]=c-'a'+1; Count (root);voidInsert () {n++; intXCharC; scanf ("%d%c",&x,&c); Find (Root,x+1); Find (t[root][1],1); intY=node (t[root][1],c-'a'+1); t[t[root][1]][0]=y; F[y]=t[root][1]; Count (t[root][1]); Count (root);CharSTR[MAXN];intMain () {scanf ("%s", str+1); N=strlen (str+1); for(intI=1; i<=n;i++) a[i]=str[i]-'a'+1; b[0]=1; for(intI=1; i<=maxn;i++) b[i]=b[i-1]*bases; Root=a[0]=a[n+1]=0; Build (0, Root,0, n+1); intm; scanf ("%d",&m); for(intI=1; i<=m;i++) {scanf ("%s", str);//do not read the space with%c and read the space with%s will stop. if(str[0]=='Q') ask (); if(str[0]=='R') repair (); if(str[0]=='I') insert (); } return 0;}
View Code
"Bzoj" 1014 [JSOI2008] Mars man prefix