Bzoj 1014: [JSOI2008] mars man prefix

Source: Internet
Author: User

Time Limit:10 Sec Memory limit:162 MB
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 like this: Madamimadam,
We label each character of this 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 is a string of characters starting with the first x character of the substring, and a string starting with the nth character
The length of the public prefix of two strings. For example, LCQ (1, 7) = 5, LCQ (2, ten) = 1, LCQ (4, 7) = 0 in the process of studying the LCQ function
, the Martians discovered such an association: if all suffixes of the string are ordered, the value of the LCQ function can be obtained quickly;
If you find the value of the LCQ function, you can also quickly sequence the suffix of the string. Although the Martians were clever enough to find the fast LCQ function
Algorithm, but the people who are unwilling to concede to the earth have made a difficult problem for the Martians: they can also change the string itself while seeking the LCQ function. Specifically
, you can change the value of one of the characters in the string, or you can insert a character at one point in the string. Earth people want to test, in so
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. Exercise
There are 3 types, 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 word
The length of the character string.
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, the word
Inserted at the beginning of the symbol 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.

First, the string becomes a splay, then the hash value of its subtree is computed for each point,x[k].z= (x[x[k].s[0]].z*cf[x[x[k].s[1]].zs+1]%p+x[k].c*cf[x[x[k].s[1] . zs]%p+x[x[k].s[1]].z)%p;

Cf[i] represents the i-th side of a prime number. Insert and modify operations are no different from normal splay. For the answer to the question, we can find the interval, and see if the hash value is the same.

#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;typedef unsignedLong Longll;Const intn=100005;Constll p=9999991;structx{intfa,s[2],zs,c; ll Z;} X[n];CharC[n];intGe=1; ll Cf[n];voidPuintk) {X[k].zs=x[x[k].s[0]].zs+1+x[x[k].s[1]].zs; X[k].z= (x[x[k].s[0]].z*cf[x[x[k].s[1]].zs+1]%p+x[k].c*cf[x[x[k].s[1]].zs]%p+x[x[k].s[1]].Z)%p;}voidChanintKintACharZF) {    if(x[x[k].s[0]].zs>=a) Chan (x[k].s[0],A,ZF); Else if(x[x[k].s[0]].zs+1==a) x[k].c=ZF; ElseChan (x[k].s[1],a-x[x[k].s[0]].zs-1, ZF); Pu (k);}voidXzintAint&k) {    intb=x[a].fa,bb=x[b].s[1]==a,cc=x[x[b].fa].s[1]==b; if(k==b) k=A; Elsex[x[b].fa].s[cc]=A; X[A].FA=X[b].fa; X[X[B].S[BB]=x[a].s[!bb]].fa=b; x[x[a].s[!bb]=b].fa=A; PU (b);}voidSplay (int& K,inta) {     while(k!=a) {intb=X[a].fa; if(b!=k)if(x[b].s[1]==a^x[x[b].fa].s[1]==b) XZ (A,K); ElseXZ (b,k);    XZ (A,K); } PU (a);}intFindintKinta) {    if(x[x[k].s[0]].ZS&GT;=A)returnFind (x[k].s[0],a); Else if(x[x[k].s[0]].zs+1==A)returnK; returnFind (x[k].s[1],a-x[x[k].s[0]].zs-1);} ll ask (intLintR)    {splay (Ge,find (ge,l)); Splay (x[ge].s[1],find (ge,r)); returnx[x[x[ge].s[1]].s[0]].z;}intMain () {Freopen ("a.in","R", stdin), Freopen ("a.out","W", stdout); scanf ("%s", c); intL=strlen (c), sl=l+2; X[l+2].fa=l+1; X[l+2].z=x[l+2].c=0; X[l+2].zs=cf[0]=1;  for(intI=1; i<n;++i) cf[i]=cf[i-1]*9999973%p;  for(inti=l-1; i>=0;--i) {x[i+2].fa=i+1; X[i+2].zs=l-i+1; X[i+2].s[1]=i+3; X[i+2].c=C[i]; X[i+2].z= (x[i+3].z+c[i]*cf[l-i])%p; } x[1].c=0; x[1].z=x[2].z; x[1].zs=l+2; x[1].s[1]=2; scanf ("%d",&l);  while(l--)    {        CharZF; scanf ("\n%c",&ZF); if(zf=='Q')        {            intA,b,l=0, R; scanf ("%d%d",&a,&b); ++a;++b; R=min (sl-a,sl-b) +1;  while(l<r-1)            {                intMid= (l+r) >>1; if(Ask (A-1, A+mid) ==ask (b-1, B+mid)) l=mid; ElseR=mid; } printf ("%d\n", L); }        Else if(zf=='R')        {            intA; scanf ("%d%c",&a,&ZF); Chan (Ge,a+1, ZF); }        Else        {            intA; scanf ("%d%c",&a,&ZF); Splay (Ge,find (Ge,a+1)); Splay (x[ge].s[1],find (ge,a+2)); x[x[ge].s[1]].s[0]=++SL; X[sl].z=x[sl].c=ZF; X[SL].FA=x[ge].s[1]; X[sl].zs=1; PU (x[ge].s[1]);        PU (GE); }    }    return 0;}

Bzoj 1014: [JSOI2008] mars 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.