bzoj1014 [JSOI2008] mars people

Source: Internet
Author: User

Description

The Martians have recently studied an operation that asks for a common prefix of two suffixes of a string. For example, there is a string: Madamimadam, we label each character of the string: Ordinal: 1 2 3 4 5 6 7 8 9 10 11 character M A d a m i m a D a m now the Martians define a function Lcq (x, y), which means: The string of characters starting with the first x character of the strings, and the length of the common prefix of the two string, starting with the nth character in the strings. Let's say, LCQ (1, 7) = 5, LCQ (2, ten) = 1, LCQ (4, 7) = 0 During the study of the LCQ function, the Martians discovered an association: if all suffixes of the string are ordered, the value of the LCQ function can be obtained quickly, and if the LCQ function is obtained Value, the suffix of the string can also be sorted quickly. Although the Martians have cleverly found a fast algorithm for finding LCQ functions, the people who are unwilling to concede the earth have a problem with the Martians: they can also change the string itself while taking LCQ functions. Specifically, you can change the value of one of the characters in a string, or you can insert a character at one point in the string. Earth people want to test, in such a complex question, whether the Martians can also be able to quickly find the value of the LCQ function.

Input

The first line gives the initial string. The second line is a non-negative integer m that represents the number of operations. The next M-line, each line describes an operation. There are 3 types of operations, as shown below

1, inquiry. Syntax: Qxy,x,y are positive integers. Function: Calculates LCQ (x, y) limit: 1<=x,y<= the current string length.

2. Modification. Syntax: Rxd,x is a positive integer and D is a character. Function: Modifies the number of x in the string to character D. Limit: x does not exceed the current string length.

3, insert: Syntax: Ixd,x is a non-negative integer, D is a character. Function: Inserts the character d after the string x character, and if x=0, inserts at the beginning of the string. Limit: x does not exceed the current string length

Output

For each query in the input file, you should output the corresponding answer. One answer line.

Sample Inputmadamimadam
7
Q 1 7
Q 4 8
Q 10 11
R 3 A
Q 1 7
I Ten A
Q 2 11Sample Output5
1
0
2
1HINT

1. All strings are composed of lowercase letters from beginning to finish.
2, m<=150,000
3, String length L meet l<=100,000 from beginning to finish
4, the number of inquiry operation is not more than 10,000.
For the 1th, 2 data, the string length does not exceed 1,000 from beginning to the other
For the 3,4,5 data, there is no insert operation.

Positive solution: $splay $+ $hash $.

To modify and insert characters, we can use $splay$ to maintain the entire string. Maintain the letters represented by each node, the $hash$ value of the string formed by the subtree interval, and the subtree size, and discuss the various operations.

Note that the $hash$ value of $a $ cannot be $0$. So I used to use it and I didn't get a hole.

1 //It's made by wfj_2048~2#include <algorithm>3#include <iostream>4#include <cstring>5#include <cstdlib>6#include <cstdio>7#include <vector>8#include <cmath>9#include <queue>Ten#include <stack> One#include <map> A#include <Set> - #defineBase (157ULL) - #defineINF (1&LT;&LT;30) the #defineN (500010) - #defineIl inline - #defineRG Register - #defineull unsigned long Long + #defineFile (s) freopen (S ".", "R", stdin), Freopen (S ". Out", "w", stdout) -  + using namespacestd; A  at intch[n][2],fa[n],sz[n],n,m,rt,tot; - ull sum[n],val[n],bin[n]; - Chars[n],c[5]; -  -Ilintgi () { -Rgintx=0, q=1; RgCharCh=GetChar (); in      while((ch<'0'|| Ch>'9') && ch!='-') ch=GetChar (); -     if(ch=='-') q=-1, ch=GetChar (); to      while(ch>='0'&& ch<='9') x=x*Ten+ch- -, ch=GetChar (); +     returnq*x; - } the  *IlvoidPushup (RGintx) { $Rgintl=ch[x][0],r=ch[x][1]; sz[x]=sz[l]+sz[r]+1;Panax NotoginsengSUM[X]=SUM[L]*BIN[SZ[X]-SZ[L]]+VAL[X]*BIN[SZ[R]]+SUM[R];return; - } the  +IlvoidRotate (RGintx) { ARginty=fa[x],z=fa[y],k=ch[y][0]==x; thech[z][ch[z][1]==y]=x,fa[x]=Z; +ch[y][k^1]=ch[x][k],fa[ch[x][k]]=y; -Ch[x][k]=y,fa[y]=x,pushup (y), pushup (x);return; $ } $  -IlvoidSplay (RGintX,rgintgoal) { -      while(fa[x]!=goal) { theRginty=fa[x],z=Fa[y]; -     if(fa[y]!=goal) {Wuyi         if((ch[z][0]==y) ^ (ch[y][0]==x)) rotate (x); the         Elserotate (y); -     } Wu rotate (x); -     } About     if(!goal) rt=x;return; $ } -  -IlintBuild (RGintL,rgintR) { -RgintMid= (l+r) >>1, X;if(!mid) x=n+2;Elsex=mid; A     if(L<mid) ch[x][0]=build (l,mid-1), fa[ch[x][0]]=x; +     if(R>mid) ch[x][1]=build (mid+1, R), fa[ch[x][1]]=x; theval[x]=s[mid]-'a'+1, Pushup (x);returnx; - } $  theIlintFind (RGintX,rgintk) { the     if(k==sz[ch[x][0]]+1)returnx; the     if(k<=sz[ch[x][0]])returnFind (ch[x][0],k); the     Else returnFind (ch[x][1],k-sz[ch[x][0]]-1); - } in  theIlintCalc (RGintX,rginty) { theRgintL=1, r=n-y+1, mid,k,ans=0; RG ull hsh1,hsh2; About      while(l<=R) { theMid= (l+r) >>1, K=find (rt,x), splay (K,0); theK=find (rt,x+mid+1), Splay (K,RT), hsh1=sum[ch[k][0]]; theK=find (Rt,y), splay (K,0); +K=find (rt,y+mid+1), Splay (K,RT), hsh2=sum[ch[k][0]]; -     if(HSH1==HSH2) ans=mid,l=mid+1;Elser=mid-1; the     }Bayi     returnans; the } the  -IlvoidWork () { -bin[0]=1; for(RGintI=1; i<=100000; ++i) bin[i]=bin[i-1]*Base; thescanf"%s", s+1), M=gi (), N=strlen (s+1); thes[0]=s[n+1]='a'-1, Rt=build (0, n+1), tot=n+2; the      for(RGintI=1, l,r,x,k;i<=m;++i) { thescanf"%s", c); -     if(c[0]=='Q') L=gi (), R=gi (), printf ("%d\n", Calc (l,r)); the     if(c[0]=='R'){ theX=gi (), scanf ("%s", c), K=find (rt,x+1); theval[k]=c[0]-'a'+1, Pushup (k), Splay (K,0);94     } the     if(c[0]=='I'){ theX=gi (), scanf ("%s", c), + +N; theK=find (rt,x+1), Splay (K,0);98K=find (rt,x+2), splay (K,RT); Aboutch[k][0]=++tot,fa[tot]=k,sz[tot]=1; -val[tot]=c[0]-'a'+1, Splay (Tot,0);101     }102     }103     return;104 } the 106 intMain () {107File ("prefix");108 Work ();109     return 0; the}

bzoj1014 [JSOI2008] mars people

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.