[JSOI2008] Mars people

Source: Internet
Author: User

This topic I successfully burst zero,\ (\color{blue}{tttttle}\) the whole point ... I'm really good.

\ (\color{red}{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 beginning with the first character of the y\
The length of the public prefix of two strings. For example,\ (LCQ (1, 7) = 5\), \ (LCQ (2) = 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 quickly calculated;
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 a quick way to get the \ (lcq\) function
Algorithm, but the people who do not willingly concede to the earth have a problem for the Martians: in the lcq\ function, you can also change the string itself. 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.

\ (\color{red}{input}\)

The first line gives the initial string. The second line is a non-negative integer\ (m\)That represents the number of actions. The next\ (m\)Row, each line describes an action. Exercise
There are 3 types, as shown below
\ (1\)Ask. Grammar:\ (qxy\),\ (x,y\)are positive integers. Function: Calculation\ (LCQ (x, y) \)Limit: 1<=x,y<= the current string length.
\ (2\)Modify. Grammar:\ (rxd\),\ (x\)is a positive integer,\ (d\)is a character. Function: The string in the\ (x\)Number modified to character\ (d\)。 Limit:\ (x\)No more than the current word
The length of the character string.
\ (3\), insert: Syntax:\ (ixd\),\ (x\)is a non-negative integer,\ (d\)is a character. Function: in string section\ (x\)Characters and then insert the character D, if\ (x=0\), then in the word
Inserted at the beginning of the symbol string. Limit:\ (x\)Does not exceed the current string length

\ (\color{red}{output}\)

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

\ (\color{red}{sample \ \ input}\)

Madamimadam
7
Q 1 7
Q 4 8
Q 10 11
R 3 A
Q 1 7
I Ten A
Q 2 11

\ (\color{red}{sample \ \ output}\)

5
1
0
2
1

\ (\color{red}{hint}\)

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.

\ (\color{purple}{half-solution}\)

Well, compare the record, I will only temporarily tle\ version of violence, first posted to record my glorious history .

Insert is inserted, modification is modified, but because in fact I was only half- splay\ , so now the interval \ (splay\) at a loss (otz\)...

Well, the query, then, is to constantly query their parent node, or right child node, but because you \ (splay\) words will make the sequence unordered, so it is not possible to do.

So in fact, you see, this is actually a (wa\) but first \ (tle\) algorithm.

hahaha I also debugged for half a day.

That's funny.

#include <iostream>#include <cstdio>#define MAXN 250001#define IL inlineusing namespaceStdstructchars{unsigned intson[2],f,sub;CharV;} S[MAXN];CharT,BASE[MAXN],CC;unsigned intK,res;unsigned intXx,yy,m,a,b,i,qwq,root,wz,now,fnow,ffnow;ilBOOLWunsigned intx) {returnx==s[s[x].f].son[1];} IlvoidPUSH_UP (unsigned intx) {if(x) {s[x].sub=1;if(s[x].son[1]) s[x].sub+=s[s[x].son[1]].sub;if(s[x].son[0]) s[x].sub+=s[s[x].son[0]].sub; }}ilvoidRotateunsigned intx) {fnow=s[x].f,ffnow=s[fnow].f;BOOLWw=w (x); s[fnow].son[ww]=s[x].son[ww^1];    S[s[fnow].son[ww]].f=fnow;    S[fnow].f=x;    S[x].f=ffnow; s[x].son[ww^1]=fnow;if(Ffnow) {s[ffnow].son[s[ffnow].son[1]==fnow]=x;    } push_up (Fnow); PUSH_UP (x);}voidSplay (unsigned intXunsigned intGoal) { for(QWQ; (QWQ=S[X].F)!=goal;rotate (x)) {if(S[qwq].f!=goal)        {Rotate (w (x) ==w (QWQ) qwq:x); }    }if(goal==0) {root=x; }}voidInsertunsigned intPosCharc) {if(!WZ)        {wz++;        ROOT=WZ; s[wz].f=s[wz].son[1]=s[wz].son[0]=0;        s[wz].sub++; S[wz].v=c;return; }if(!pos)        {wz++; s[wz].son[1]=root,s[wz].v=c;        S[ROOT].F=WZ;        ROOT=WZ; PUSH_UP (WZ);return; } now=root; while(1){if(s[now].son[0]&&pos<=s[s[now].son[0]].sub) now=s[now].son[0];Else{unsigned intTemp= (s[now].son[0]?s[s[now].son[0]].sub:0)+1;if(pos<=temp) Break;            Pos-=temp; now=s[now].son[1];    }} wz++; s[wz].son[1]=s[now].son[1]; s[s[now].son[1]].F=WZ; s[now].son[1]=WZ;    S[wz].f=now;    S[wz].v=c;    PUSH_UP (WZ); PUSH_UP (now);//splay (wz,0);}ilvoidUpdateunsigned intPosCharc) {now=root; while(1){if(s[now].son[0]&&pos<=s[s[now].son[0]].sub) now=s[now].son[0];Else{unsigned intTemp= (s[now].son[0]?s[s[now].son[0]].sub:0)+1;if(pos<=temp) Break;            Pos-=temp; now=s[now].son[1]; }} s[now].v=c;//Splay (now,0);}ilunsigned intFindunsigned intSxunsigned intSY) {xx=root,res=0, Yy=root; while(1){if(s[xx].son[0]&&sx<=s[s[xx].son[0]].sub) xx=s[xx].son[0];Else{unsigned intTemp= (s[xx].son[0]?s[s[xx].son[0]].sub:0)+1;if(sx<=temp) Break;            Sx-=temp; xx=s[xx].son[1]; }    } while(1){if(s[yy].son[0]&&sy<=s[s[yy].son[0]].sub) yy=s[yy].son[0];Else{unsigned intTemp= (s[yy].son[0]?s[s[yy].son[0]].sub:0)+1;if(sy<=temp) Break;            Sy-=temp; yy=s[yy].son[1]; }    } while(1){if(S[YY].V==S[XX].V) res++; yy=s[yy].son[1]; xx=s[xx].son[1];if(!yy| |! xx| | S[YY].V!=S[XX].V) {returnRes }}}ilunsigned intQread () {k=0, Cc=getchar (); while(!isdigit (CC))    {Cc=getchar (); } while(IsDigit (CC)) {k= (k<<1) + (k<<3) +CC-48;    Cc=getchar (); }returnK;}intMain () {scanf ("%s", base); M=qread (); for(i=0; base[i];i++) {insert (i,base[i]); } for(i=1; i<=m;i++) {scanf ("%c", &t);if(t==' I ') {a=qread (); scanf"%c", &t); Insert (A,T);Continue; }Else if(t==' R ') {a=qread (); scanf"%c", &t); Update (A,T);Continue;        } a=qread ();        B=qread (); printf"%d\n", find (b)); }return 0;}

The above has been hung on the stigma of history

[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.