1014: [JSOI2008] mars man prefix

Source: Internet
Author: User

1014: [JSOI2008] mars man prefix time limit:10 Sec Memory limit:162 MB submit:7820 solved:2487 [submit][st Atus][discuss] 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), table Example: The string that starts with the first x character, the length of the common prefix of the two-character string, which starts with the first word in the substring. 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 similarly, if the LCQ letter is obtained Number, or the suffix of the string can 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 operations, as follows 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 2Sample 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.

For cases where there is no insertion, the hash value is preprocessed with O (l) time, then the length of the second, then the time of the O (1) is used to determine if there is a time complexity of O (log (L)) for the single query.

After inserting, you can use splay to maintain the interval hash value, then similar to the above, just need additional O (log (L)) time to extract the interval, a single query time complexity of O (log (l) * log (L))

#include <cstdio>#include<cstring>Const intMAXN =110000+Ten; typedef unsignedLong Longll;Constll seed =131;CharVAL[MAXN];intTot, Root, fa[maxn], son[maxn][2], SIZ[MAXN], cnt =0; ll Hash[maxn], Pow[maxn];inlinevoidPushup (intRT) {Siz[rt]= siz[son[rt][0]] + siz[son[rt][1]] +1; HASH[RT]= hash[son[rt][0]] + val[rt] * pow[siz[son[rt][0]] + hash[son[rt][1]] * pow[siz[son[rt][0]] +1];} InlinevoidRotate (intx) {    intf = fa[x], p = son[f][0] ==x; son[f][!P] =Son[x][p]; FA[SON[X][P]]=F; FA[X]=Fa[f]; if(Fa[x]) son[fa[x]][son[fa[x]][1] = = f] =x; SON[X][P]=F; FA[F]=x; Pushup (f);} InlinevoidSplay (intXintt) {    inty, Z;  while(Fa[x]! =t) {        if(Fa[fa[x]] = =t) Rotate (x); Else{y=Fa[x]; Z=Fa[y]; if(son[y][1] = = x ^ son[z][1] ==y) Rotate (x); ElseRotate (y);        Rotate (x);    }} pushup (x); if(!t) root =x;} InlinevoidFind (intKthintt) {    intx =root, tmp;  while(x) {tmp= siz[son[x][0]]; if(KTH = = tmp +1) {splay (x, T); return; }        Else if(KTH < TMP +1) x = son[x][0]; Else{x= son[x][1]; KTH-= tmp +1; }    }}CharS[MAXN];intInsert (intStinten) {    if(St > en)return 0; intMid = st + en >>1, now = + +CNT; Val[now]=S[mid]; son[now][0] = Insert (St, Mid-1); if(son[now][0]) fa[son[now][0]] =Now ; son[now][1] = Insert (mid +1, en); if(son[now][1]) fa[son[now][1]] =Now ;    Pushup (now); returnNow ;} InlinevoidLcq () {intx, y; scanf ("%d%d", &x, &y); intL =1, r = Tot, Mid, ret =0;    ll TMP;  while(L <=R) {Mid= L + R >>1; if(Y + mid-1> Tot) r = mid-1; Else{Find (x,0); Find (x+ Mid +1, Root); TMP= hash[son[son[root][1]][0]]; Find (Y,0); Find (y+ Mid +1, Root); if(TMP = = hash[son[son[root][1]][0]]) {ret=mid; L= Mid +1; }            ElseR = Mid-1; }} printf ("%d\n", ret);}intMain () {pow[0] =1;  for(inti =1; i < MAXN; i++) Pow[i]= Pow[i-1] *seed; siz[0] = hash[0] = fa[0] = son[0][0] = son[0][1] =0; scanf ("%s", S +1); Tot= strlen (S +1); Root= Insert (0, Tot +1); intm; scanf ("%d", &m); Charopt[5], d[5]; intx, y, t;  while(m--) {scanf ("%s", opt); Switch(opt[0]){             Case 'I': scanf ("%d%s", &x, D); Find (x+1,0); Find (x+2, Root); CNT++; VAL[CNT]= d[0]; FA[CNT]= son[root][1]; son[son[root][1]][0] =CNT;                Pushup (CNT); Pushup (son[root][1]);                Pushup (root); Tot++;  Break;  Case 'R': scanf ("%d%s", &x, D); Find (x,0); Find (x+2, Root); T= son[son[root][1]][0]; Val[t]= d[0];                Pushup (t); Pushup (son[root][1]);                Pushup (root);  Break;  Case 'Q': Lcq ();  Break; }    }    return 0;}

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.