HASH_1014: [JSOI2008] mars man prefix

Source: Internet
Author: User

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6 using namespacestd;7typedef unsignedintll;8 #defineMAXN 1500059 #defineP 53Ten intn,m,root,tot,fa[maxn],son[maxn][2],SIZE[MAXN]; One ll VAL[MAXN],SUM[MAXN],BIN[MAXN]; A Charst[maxn],op[5]; - voidReadint&x) { -x=0;intf=1;Charch; the      for(Ch=getchar ();! IsDigit (CH); Ch=getchar ())if(ch=='-') f=-1; -      for(; isdigit (ch); Ch=getchar ()) x=x*Ten+ch-'0'; x*=F; - } - structsplay{ +     intWhich (intx) { -         returnson[fa[x]][1]==x; +     } A     voidUpdata (intx) { at         intls=son[x][0],rs=son[x][1]; -size[x]=size[ls]+size[rs]+1; -sum[x]=sum[rs]+val[x]*bin[size[rs]]+sum[ls]*bin[1+Size[rs]]; -     } -     intBuildintLintR) { -         intXif(L>r)return 0;intMid= (l+r) >>1;  in         if(mid==0) x=n+1, val[x]=0; -         Else if(mid==n+1) x=n+2, val[x]=0; to         Elsex=mid,val[x]=st[x]-'a'; size[x]=1; +son[x][0]=build (l,mid-1), son[x][1]=build (mid+1, R), Updata (x); -         if(son[x][0]) fa[son[x][0]]=x; the         if(son[x][1]) fa[son[x][1]]=x; *         returnx; $     }Panax Notoginseng     voidRotata (intx) { -         intY=fa[x],d=which (x), dd=which (y); the         if(Fa[y]) son[fa[y]][dd]=x; fa[x]=Fa[y]; +fa[son[x][d^1]]=y,son[y][d]=son[x][d^1]; Afa[y]=x,son[x][d^1]=y,updata (y); the     } +     voidSplay (intXintgoal) { -          while(fa[x]!=goal) { $             if(fa[fa[x]]==goal) Rotata (x); $             Else if(which (x) = =which (Fa[x]) rotata (fa[x]), Rotata (x); -             Elserotata (x), Rotata (x); -         } the         if(goal==0) root=x; updata (x); -     }Wuyi     intKthintx) { the         intY=root,ls,rs;if(!root)return 0; -          for (;;) { Wuls=son[y][0],rs=son[y][1]; -             if(size[ls]+1==X)returny; About             Else if(size[ls]>=x) y=ls; $             Elsex-=size[ls]+1, y=rs; -         } -     } -     BOOLCheckintXintYintLen) { A         intT1,t2; T1=kth (x), t2=kth (x+len+1); +ll Z;splay (T1,0), Splay (T2,T1), z=sum[son[t2][0]]; theT1=kth (y), t2=kth (y+len+1); Splay (T1,0), splay (T2,T1); -         returnz==sum[son[t2][0]]; $     } the }splay; the voidQueryintXinty) { the     intL=1, r,mid,ans=0; R=min (tot-2-x+1, tot-2-y+1); the      while(l<=R) { -Mid= (l+r) >>1; in         if(Splay.check (x,y,mid) = =1) ans=mid,l=mid+1; the         Elser=mid-1; the}printf ("%d\n", ans); About } the intMain () { thebin[0]=1; for(intI=1; i<=150000; i++) bin[i]=bin[i-1]*p; thescanf"%s", st+1), N=strlen (st+1); +Root=splay.build (0, n+1); tot=n+2; - read (m); the      for(intx,y;m;m--){Bayiscanf"%s", op+1); the         if(op[1]=='Q') Read (x), read (y), query (x, y); the         Else if(op[1]=='R') Read (x), scanf ("%s", op+1), X=splay.kth (x+1), Splay.splay (x,0), val[x]=op[1]-'a', Splay.updata (x); -         Else{ -Read (x), scanf ("%s", op+1), ++tot,val[tot]=op[1]-'a', size[tot]=1; the             intT1=splay.kth (x+1), T2=splay.kth (x+2); Splay.splay (T1,0), Splay.splay (T2,T1); thefa[tot]=t2,son[t2][0]=tot,splay.updata (T2), Splay.splay (Tot,0); the         } the     } -     return 0; the}
View Code

splay maintenance, ask when the answer, hash decision, I use is 53 binary.

Splay, two points, Hash.

HASH_1014: [JSOI2008] mars man prefix

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.