http://www.lydsy.com/JudgeOnline/problem.php?id=1014
By SB wrong tune cry Qaq ... Insert there. Insert to x behind ... I...... Written in the front of the first x ... It's a tune! Good! Long!
Qaq
The God LCP procedure .... Indicates that only the height of the SA is offline .... This kind of online I just qaq to make a sad expression ...
Then worshipped the puzzle .... Good god. Splay Maintenance Interval ... hash+ Two-point maintenance LCP .... Qaq seems to be the White Book said ...
But this kind of probability problem is really good ...
So take the mold there directly to copy someone else's ... Because there is no guarantee that you take the MoD correctly qaq ....
Then is the right to multiply the hash in the interval, because the multiplication distribution rate, so it can be maintained in intervals.
。。
#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream > #include <algorithm> #include <queue> #include <set> #include <map>using namespace std; typedef long Long LL; #define REP (i, n) for (int i=0; i< (n); ++i) #define FOR1 (i,a,n) for (int i= (a); i<= (n); ++i) #define For2 (i,a,n) for (int i= (a);i< (n), ++i) #define FOR3 (i,a,n) for (int i= (a); i>= (n); i.) #define FOR4 (i,a,n) for (int i= ( a);i> (n); i) #define CC (i,a) memset (i,a,sizeof (i)) #define READ (a) a=getint () #define PRINT (a) printf ("%d", a) # Define DBG (x) cout << (#x) << "=" << (x) << endl#define error (x) (! x) puts ("error"): 0) #define RDM (x, i) for (int i=ihead[x]; i; i=e[i].next) inline const int Getint () {int r=0, k=1; Char c=g Etchar (); for (; c< ' 0 ' | | C> ' 9 '; C=getchar ()) if (c== '-') k=-1; for (; c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) r=r*10+c-' 0 '; return k*r; }const int n=100005;const ll m=9875321;int p[n];struct node *null;struct Node {node *f, *c[2];int K, S, h;node (int _k=0) {k=_k; s=1; h=_k; f=c[0]=c[1]=null;} void SetC (node *x, bool D) {c[d]=x; x->f=this;} BOOL D () {return f->c[1]==this;} void Pushup () {s=c[0]->s+c[1]->s+1;h= (c[0]->h+ (LL) k*p[c[0]->s])%m+ ((LL) c[1]->h*p[c[0]->s+1])% M)%M;}} *root;void Rot (node *x) {node *f=x->f; bool D=x->d (); f->f->setc (x, F->d ()); F->setc (X->c[!d], D); X->setc (f,!d); F->pushup (); if (F==root) root=x;} void splay (node *x, node *f=null) {while (x->f!=f) if (x->f->f==f) rot (x); else X->d () ==x->f->d ()? ( Rot (x->f), rot (x)):(rot (x), rot (x)); X->pushup (); Node *sel (node *x, int k) {int s=x->c[0]->s;if (S==K) return x;if (s<k) return sel (x->c[1], k-s-1); return sel (x ->c[0], k);} Node *getrange (int l, int r) {splay (sel (root, L-1)), splay (sel (Root, r+1), root); return root->c[1];} void ins (int k, int pos) {node *f=getrange (pos+1, POS); F->setc (new node (k), 0); Splay (F->c[0]);} void fix (int k, intPOS) {node *f=getrange (POS, POS);f->c[0]->k=k; f->c[0]->pushup (); splay (F->c[0]);} int Gethash (int l, int r) {return GetRange (L, R)->c[0]->h;} int ask (int x, int y) {int s=root->s-2;int l=1, r=min (S-x, s-y) +1, Mid;while (l<=r) {mid= (l+r) >>1;if (Gethash (x , x+mid-1) ==gethash (y, y+mid-1)) L=mid+1;else r=mid-1;} return L-1;} void U (node *x) {if (x==null) return; U (X->c[0]); U (x->c[1]); X->pushup (); }void Pr (node *x) {if (x==null) return; Pr (X->c[0]); printf ("%c", x->k+ ' a '); Pr (x->c[1]); }void D (node *x=root) {Pr (x); puts (""); U (x); Pr (x); }char s[n];int n;void Build (int l, int r, node *&x) {if (l>r) Return;int mid= (L+r) >>1;x=new node (s[mid]-' a '); F (l==r) Return;build (L, Mid-1, x->c[0]), build (Mid+1, R, X->c[1]), if (l<=mid-1) x->c[0]->f=x;if (mid+1 <=R) X->c[1]->f=x;x->pushup ();} void Init () {P[0]=1;for1 (I, 1, 100000) p[i]= (p[i-1]*26)%m;null=new node; null->s=0; null->f=null->c[0]=null- >c[1]=null;roOt=new node; ROOT->SETC (new node, 1), node *x;build (1, N, x), root->c[1]->setc (x, 0); Root->c[1]->pushup (); Root->pushup ();//d ();} Char Rdchar () {char ret=getchar (); while (ret< ' A ' | | Ret> ' z ') Ret=getchar (); return ret;} int main () {scanf ("%s", s+1); N=strlen (s+1); init (); int m=getint (); while (m--) {char c=getchar (); while (c!= ' Q ' && c!= ' R ' &&c!= ' I ') C=getchar (), if (c== ' Q ') {int x=getint (), Y=getint (), printf ("%d\n", ask (x, y));} else if (c== ' R ') {int x=getint (); Fix (Rdchar ()-' a ', x);} else if (c== ' I ') {int x=getint (); Ins (Rdchar ()-' a ', x);}} return 0;}
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 operations, as follows: 1, inquiry. Syntax: Q x y,x, y are positive integers. Function: Calculates LCQ (x, y) limit: 1 <= x, y <= the current string length. 2. Modification. Syntax: R x d,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: I x d,x is a non-negative integer, D is a character. Function: Inserts the character D after the first X character of the string, and inserts it at the beginning of the string if x = 0. 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
Data size:
For 100% of data, meet:
1. All strings are composed of lowercase letters from beginning to finish.
2, M <= 150,000
3, the string length L always meet l <= 100,000
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.
Source
"Bzoj" 1014: [JSOI2008] Martian prefix (splay+hash+ II +LCP)