1014: [JSOI2008] mars man prefix time limit:10 Sec Memory limit:162 MB
submit:8112 solved:2569
[Submit] [Status] [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 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.
Source
Analysis: First because there is the insertion and modification, operation, with splay can be solved, and then is to find LCP, this can be solved with two points +hash, so the combination is good.
The code is as follows:
#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <cstdio> #include <string> #include < cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include < queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include < cmath> #include <stack> #include <sstream> #include <list> #include <assert.h> #include < bitset> #include <numeric> #define DEBUG () puts ("++++") #define GCD (A, B) __gcd (A, b) #define Lson l,m,rt<<1 #define Rson m+1,r,rt<<1|1#define fi first#define se second#define pb push_back#define sqr (x) ((x) * (x)) #define MS (a , b) memset (A, B, sizeof a)//#define SZ size () #define PU push_up#define PD push_down#define CL Clear () #define ALL 1,n,1#def ine for (i,x,n) for (int i = (x); I < (n); ++i) #define FREOPENR freopen ("In.txt", "R", stdin) #define FREOPENW freopen ("O Ut.txt "," w ", stdout) using namespace Std;typedef long longLl;typedef unsigned long long ull;typedef pair<int, int> p;const int INF = 0x3f3f3f3f;const LL LNF = 1e17;const Doub Le inf = 1e20;const double PI = ACOs ( -1.0); const double EPS = 1e-6;const int maxn = 1e5 + 10;const int maxm = 3e5 + 10;con St ULL mod = 3;const int dr[] = {-1, 0, 1, 0};const int dc[] = {0,-1, 0, 1};const char *de[] = {"0000", "0001", "0010", " 0011 "," 0100 "," 0101 "," 0110 "," 0111 "," $ "," 1001 "," 1010 "," 1011 "," 1100 "," 1101 "," 1110 "," 1111 "};int N, M;const int Mon[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 0, +, +, +, +,, +, 31};const int monn[] = 31};inline bool Is_in (int r, int c) {return R >= 0 && r < n && C >= 0 && c < m; } #define Key_value Ch[ch[root][1]][0]int PRE[MAXN], ch[maxn][2], key[maxn], sz[maxn];int root, Tot1;int S[MAXN], Tot2; Char A[MAXN]; ULL H[MAXN], xp[maxn];void NewNode (int &rt, int fa, int x) {if (tot2) RT = s[tot2--]; else RT = ++TOT1; PRE[RT] = FA; KEY[RT] =X Ch[rt][0] = ch[rt][1] = 0; SZ[RT] = 1;} void push_up (int rt) {int L = ch[rt][0], r = ch[rt][1]; SZ[RT] = Sz[l] + sz[r] + 1; H[RT] = H[r] + key[rt] * Xp[sz[r]] + h[l] * xp[sz[r]+1];} void Update_setv (int rt, int val) {if (!RT) return; KEY[RT] = val; PUSH_UP (RT);} void Build (int &rt, int l, int r, int fa) {if (L > R) return; int m = l+r >> 1; NewNode (RT, FA, A[m]); Build (Ch[rt][0], L, M-1, RT); Build (Ch[rt][1], m+1, R, RT); PUSH_UP (RT);} void Init () {tot1 = root = Tot2 = 0; Ch[root][0] = ch[root][1] = Sz[root] = Pre[root] = 0; Key[root] = 0; scanf ("%s", a); n = strlen (a); NewNode (root, 0,-1); NewNode (ch[root][1], root,-1); Build (key_value, 0, n-1, ch[root][1]); PUSH_UP (ch[root][1]); Push_up (root);} int get_kth (int rt, int k) {int t = sz[ch[rt][0]] + 1; if (t = = k) return RT; if (T > K) return get_kth (Ch[rt][0], k); Return get_kth (ch[rt][1], k-t);} void Rotate (int x, int k) {int y = pre[x]; CH[Y][!K] = ch[x][k]; Pre[ch[x][k]] = y; if (Pre[y]) Ch[pre[y]][ch[pre[y]][1]==y] = x; PRE[X] = Pre[y]; Ch[x][k] = y; Pre[y] = x; Push_up (y);} void splay (int rt, int goal) {while (Pre[rt]! = goal) {if (Pre[pre[rt]] = = goal) {Rotate (RT, ch[pre[rt]][0] = = RT); Continue } int y = Pre[rt]; int k = Ch[pre[y]][0] = = y; if (ch[y][k] = = RT) {Rotate (RT,!k); Rotate (RT, k); } else{Rotate (Y, k); Rotate (RT, k); }} push_up (RT); if (goal = = 0) root = RT;} void Insert () {int pos, tot = 1; scanf ("%d", &pos); scanf ("%s", a); Splay (get_kth (Root, pos+1), 0); Splay (get_kth (Root, pos+2), root); Build (key_value, 0, Tot-1, ch[root][1]); PUSH_UP (ch[root][1]); Push_up (root); ++n;} BOOL Judge (int x, int y, int mid) {splay (get_kth (root, X), 0); Splay (get_kth (root, x + mid + 1), root); ULL ans = h[key_value]; Splay (get_kth (root, y), 0); Splay (get_kth (root, y + mid + 1), root); return ans = = H[key_value];} int query () {int x, y; scanf ("%d%d", &x, &y); int L = 1, r = min (n-x+ 1, n-y + 1); while (L <= r) {int m = l + R >> 1; if (judge (x, Y, m)) L = m + 1; else R = m-1; } return l-1;} void Make_setv () {int pos, tot = 1; scanf ("%d", &pos); scanf ("%s", a); Splay (get_kth (Root, POS), 0); Splay (get_kth (Root, pos+tot+1), root); Update_setv (Key_value, a[0]); PUSH_UP (ch[root][1]); Push_up (root);} int main () {xp[0] = 1; for (int i = 1; i < MAXN; ++i) xp[i] = xp[i-1] * MOD; Init (); scanf ("%d", &m); Char op[5]; while (m--) {scanf ("%s", op); if (op[0] = = ' Q ') printf ("%d\n", Query ()); else if (op[0] = = ' R ') Make_setv (); else Insert (); } return 0;}
Bzoj 1014 [JSOI2008] Mars man prefix (splay + Hash + two)