[BZOJ1014] [JSOI2008] Martians prefix (splay & Two-point answer)

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

SourceSolution

I believe a lot of people think of the suffix array at first sight. However, the suffix array cannot effectively cope with the modification operation

The special point of this problem is that the number of queries is very small, basically focused on inserting and modifying operations

We can use $splay$ to maintain this string, insert and modify by ordinary $splay$ do, thelongest Common Qianzhui$LCQ $ need two-point answer:

Each node maintains a subtree $hash$ value, two-point answer $ans$, constantly check $s[x]\sim s[x +ans]$ and $s[y]\sim s[y+ans]$ values are the same

$treap$ is not sufficient due to the operation of the extraction interval.

The total complexity of insertions and modifications is $o (Q_1LOGN) $, the total complexity of $LCQ $ is $o (Q_2LOG^2N) $, because $q_2$ is relatively small, so the whole can be seen as $o (QLOGN) $

1#include <bits/stdc++.h>2 using namespacestd;3 structSpaly4 {5     intc[2], FA, Val, siz, hash;6     int&operator[] (intx)7     {8         returnC[x];9     }Ten}a[100005]; One Chars[100005], op[5]; A intpwr[100005]; -  - voidPUSH_UP (intk) the { -     intx = a[a[k][1]].siz; -A[k].siz = a[a[k][0]].siz + x +1; -A[k].hash = a[a[k][0]].hash * pwr[x +1] + A[k].val * Pwr[x] + a[a[k][1]].hash; + } -  + voidRotateint&k,intx) A { at     inty = A[x].fa, z = a[y].fa, dy = a[y][1] ==x; -     if(k = = y) k =x; -     Elsea[z][a[z][1] = = Y] =x; -A[y][dy] = A[x][!dy], A[a[x][!dy]].fa =y; -A[x][!dy] = y, A[y].fa = x, A[x].fa =Z; - push_up (y); in } -  to voidSplay (int&k,intx) + { -      while(k! =x) the     { *         inty = A[x].fa, z =A[y].fa; $         if(k! =y)Panax Notoginseng             if(a[y][1] = = x ^ a[z][1] ==y) rotate (k, x); -             Elserotate (k, y); the Rotate (k, x); +     } A push_up (x); the } +  - intFind_kth (intKintx) $ { $     if(x <= a[a[k][0]].siz)returnFind_kth (a[k][0], x); -     if(x = = a[a[k][0]].siz +1)returnK; -     returnFind_kth (a[k][1], x-a[a[k][0]].siz-1); the } - Wuyi intMain () the { -     intN, m, x, Y, Z, Root, L, R, Mid, Ptot; Wuscanf"%s%d", S, &m); -n =strlen (s); Aboutpwr[0] =1; $      for(inti =1; I <=100002; ++i) -Pwr[i] = pwr[i-1] *137; -a[1].FA =2, a[1].siz =1; -      for(inti =2; I <= n +2; ++i) A     { +a[i][0] = i-1, A[I].FA = i +1; theA[i].val = S[i-2], push_up (i); -     } $A[n +2].FA =0, root = Ptot = n +2; the      while(m--) the     { thescanf"%s", op); the         if(op[0] =='R') -         { inscanf"%d%s", &x, op); theSplay (Root, find_kth (root, x +1)); theA[root].val = op[0], push_up (root); About         } the         Else if(op[0] =='I') the         { thescanf"%d%s", &x, op), + +N; +Splay (Root, find_kth (root, x +1)); -Splay (a[root][1], find_kth (root, x +2)); thea[a[root][1]][0] = ++Ptot;BayiA[ptot].val = A[ptot].hash = op[0]; theA[PTOT].FA = a[root][1], A[ptot].siz =1; thePUSH_UP (a[root][1]), push_up (root); -         } -         Else the         { thescanf"%d%d", &x, &y); the             if(X >y) Swap (x, y); theL =0, r = n-y +2, z =0; -              while(L < R-1) the             { theMid = (L + r) >>1; the splay (Root, find_kth (root, x));94Splay (a[root][1], find_kth (root, x + mid +1)); thez = a[a[a[root][1]][0]].hash; the splay (Root, find_kth (root, y)); theSplay (a[root][1], find_kth (root, y + mid +1));98Z-= a[a[a[root][1]][0]].hash; AboutZ? r = Mid:l =mid; -             }101printf"%d\n", L);102         }103     }104     return 0; the}
View Code

[BZOJ1014] [JSOI2008] Martians prefix (splay & Two-point answer)

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.