BZOJ1014: [JSOI2008] mars man prefix (splay+hash+ II)

Source: Internet
Author: User
Tags mul

Main topic: A string of three operations: ① two suffix Lcp② insert a character ③ modify one character.

A few days ago just learned hash+ two points for LCP, see this question.

The original splay can still use this?! Original splay template So good to write? I used to write splay that was fake, WOC.

Splay each node represents a character and maintains that subtree represents a hash value for a substring. Because the splay rotation does not destroy the tree structure, the splay tree can represent the string no matter how it is rotated.

The pretreatment process is less than a half-day purring.

Run to update your splay template

#include <iostream>#include<cstring>#include<cstdlib>#include<cstdio>#definewhich (x) (son[fa[x]][1]==x)#defineull unsigned long Longusing namespacestd;Const intmaxn=500010;intN,m,x,y,tot,root;intfa[maxn],son[maxn][2],size[maxn],s[maxn];ull HS[MAXN],MUL[MAXN];CharST[MAXN],CH[MAXN],CH2[MAXN];voidReadint&k) {    intf=1; k=0;CharC=GetChar ();  while(c<'0'|| C>'9') c=='-'&& (f=-1), c=GetChar ();  while(c<='9'&&c>='0') k=k*Ten+c-'0', c=GetChar (); K*=F; }voidUpdateintx) {Size[x]=size[son[x][0]]+size[son[x][1]]+1; HS[X]=hs[son[x][0]]+mul[size[son[x][0]]]*s[x]+mul[size[son[x][0]]+1]*hs[son[x][1]];}voidRotateintx) {    intf=Fa[x]; BOOLk=which (x); SON[F][K]=son[x][!K]; son[x][!k]=F; Son[fa[f]][which (f)]=x; FA[SON[F][K]]=F; FA[X]=Fa[f]; FA[F]=x; SIZE[X]=Size[f]; HS[X]=Hs[f]; Update (f);}voidSplay (intXintg) {     while(fa[x]!=g) {intf=Fa[x]; if(fa[f]==g) {rotate (x);  Break; }        if(which (x) ^which (f)) rotate (x); Elserotate (f);    Rotate (x); }    if(!g) root=x;}intRankintXintk) {    if(k<=size[son[x][0]])returnRank (son[x][0],k); Else if(k== (size[son[x][0]]+1))returnx; Else returnRank (son[x][1],k-size[son[x][0]]-1);}intBuildintLintRintf) {    if(L>r)return 0; intX=++tot,mid= (l+r) >>1; FA[X]=f;s[x]= (int) (st[mid]-'a')+1; son[x][0]=build (l,mid-1, x); son[x][1]=build (mid+1, r,x);    Update (x); returnx;}voidInsertintKintch) {    intX=rank (Root,k), Y=rank (root,k+1); Splay (x,0); splay (y,x); s[++tot]=ch; Fa[tot]=y;son[y][0]=tot; Update (TOT); update (y); update (x);}voidChangeintKintch) {    intx=rank (root,k); Splay (x,0); S[X]=ch; Update (x);}intLcpintKxintky) {    intL=0, r=N;  while(l<r) {intMid= (l+r+1) >>1; if(ky+mid>n+2) {R=mid-1; Continue; }        intX=rank (root,kx-1), Y=rank (root,kx+mid); Splay (x,0); splay (y,x); ull Haxi=hs[son[y][0]]; X=rank (root,ky-1), Y=rank (root,ky+mid); Splay (x,0); splay (y,x); if(haxi==hs[son[y][0]]) l=mid; Elser=mid-1; }    returnl;}intMain () {scanf ("%s", st+1); N=strlen (st+1); mul[0]=1;  for(intI=1; i<=150010; i++) mul[i]=mul[i-1]* -; Root=build (0, n+1,0);    Read (m);  for(intI=1; i<=m;i++) {scanf ("%s", CH); if(ch[0]=='Q'{read (x); Read (y); if(x>y) Swap (x, y); if(x!=y) printf ("%d\n", LCP (x+1, y+1)); Elseprintf"%d\n", n-x+1); }        if(ch[0]=='R') {read (x); scanf ("%s", CH2); Change (x+1,(int) (ch2[0]-'a')+1); }        if(ch[0]=='I') {read (x); scanf ("%s", CH2); Insert (x+1,(int) (ch2[0]-'a')+1); N++; }    }    return 0;}
View Code

BZOJ1014: [JSOI2008] mars man prefix (splay+hash+ II)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.