標籤:highlight inline upd ota roo struct 快速 oid ++i
Description
火星人最近研究了一種操作:求一個字串兩個尾碼的公用首碼。比方說,有這樣一個字串:madamimadam,
我們將這個字串的各個字元予以標號:序號: 1 2 3 4 5 6 7 8 9 10 11 字元 m a d a m i m a d a m 現在,
火星人定義了一個函數LCQ(x, y),表示:該字串中第x個字元開始的字串,與該字串中第y個字元開始的字串
,兩個字串的公用首碼的長度。比方說,LCQ(1, 7) = 5, LCQ(2, 10) = 1, LCQ(4, 7) = 0 在研究LCQ函數的過程
中,火星人發現了這樣的一個關聯:如果把該字串的所有尾碼排好序,就可以很快地求出LCQ函數的值;同樣,
如果求出了LCQ函數的值,也可以很快地將該字串的尾碼排好序。 儘管火星人聰明地找到了求取LCQ函數的快速
演算法,但不甘心認輸的地球人又給火星人出了個難題:在求取LCQ函數的同時,還可以改變字串本身。具體地說
,可以更改字串中某一個字元的值,也可以在字串中的某一個位置插入一個字元。地球人想考驗一下,在如此
複雜的問題中,火星人是否還能夠做到很快地求取LCQ函數的值。
Input
第一行給出初始的字串。第二行是一個非負整數M,表示操作的個數。接下來的M行,每行描述一個操作。操
作有3種,如下所示
1、詢問。文法:Qxy,x,y均為正整數。功能:計算LCQ(x,y)限制:1<=x,y<=當前字串長度。
2、修改。文法:Rxd,x是正整數,d是字元。功能:將字串中第x個數修改為字元d。限制:x不超過當前字
符串長度。
3、插入:文法:Ixd,x是非負整數,d是字元。功能:在字串第x個字元之後插入字元d,如果x=0,則在字
符串開頭插入。限制:x不超過當前字串長度
Output
對於輸入檔案中每一個詢問操作,你都應該輸出對應的答案。一個答案一行。
Sample Inputmadamimadam
7
Q 1 7
Q 4 8
Q 10 11
R 3 a
Q 1 7
I 10 a
Q 2 11Sample Output5
1
0
2
1HINT
1、所有字串自始至終都只有小寫字母構成。
2、M<=150,000
3、字串長度L自始至終都滿足L<=100,000
4、詢問操作的個數不超過10,000個。
對於第1,2個資料,字串長度自始至終都不超過1,000
對於第3,4,5個資料,沒有插入操作。
題解
用平衡樹維護字串hash,查詢時二分。
不自然溢出的話在bzoj上會爆。
代碼:
#include <algorithm>#include <cctype>#include <cstdio>#include <cstring>typedef long long LL;inline int readInt() { int ans = 0, c; while (!isdigit(c = getchar())); do ans = ans * 10 + c - ‘0‘; while (isdigit(c = getchar())); return ans;}const int base = 31;const int N = 100050;int pbase[N];inline int getHash(int h1, int h2, int l2) { return h1 * pbase[l2] + h2;}struct Splay{ int son[N][2], fa[N]; int v[N], len[N], hash[N]; int root, cnt; inline void upd(int x) { int lc = son[x][0], rc = son[x][1]; len[x] = len[lc] + len[rc] + 1; hash[x] = getHash(getHash(hash[lc], v[x], 1), hash[rc], len[rc]); } inline int dir(int x) { return son[fa[x]][1] == x; } inline void rotate(int x) { int f = fa[x], d = dir(x); if (son[f][d] = son[x][d ^ 1]) fa[son[f][d]] = f; if (fa[x] = fa[f]) son[fa[f]][dir(f)] = x; upd(son[fa[f] = x][d ^ 1] = f); upd(x); } void splay(int x, int p = 0) { for (; fa[x] != p; rotate(x)) if (fa[fa[x]] != p) rotate(dir(fa[x]) == dir(x) ? fa[x] : x); if (!p) root = x; } void kth(int k, int x = -1) { if (x == -1) x = root; int y = fa[x]; while (k != len[son[x][0]]) { if (k < len[son[x][0]]) { x = son[x][0]; } else { k -= len[son[x][0]] + 1; x = son[x][1]; } } splay(x, y); } void insert(int k, int w) { int p = ++cnt; v[p] = w; kth(k); if (!root) { root = p; } else { if (son[p][1] = son[root][1]) fa[son[p][1]] = p; if (son[p][0] = root) fa[root] = p; son[root][1] = 0; upd(root); upd(root = p); } } inline void modify(int k, int w) { kth(k); v[root] = w; upd(root); } inline int query(int l, int r) { kth(r + 1); kth(l - 1, son[root][0]); return hash[son[son[root][0]][1]]; }};Splay T;int n, m;char s[N];bool check(int a, int b, int l) { return T.query(a, a + l - 1) == T.query(b, b + l - 1);}int query(int a, int b) { int l = 0, r = n - std::max(a, b) - 1; while (l < r) { int mid = r + (l - r) / 2; if (check(a, b, mid)) l = mid; else r = mid - 1; } return l;}int main() { pbase[0] = 1; for (int i = 1; i < N; ++i) pbase[i] = (LL)pbase[i - 1] * base; scanf("%s", s + 1); n = strlen(s + 1) + 2; s[0] = s[n - 1] = ‘z‘ + 1; for (int i = 0; i < n; ++i) T.insert(std::max(0, i - 1), s[i] - ‘a‘); scanf("%d", &m); while (m--) { int x; while (!isalpha(*s = getchar())); if (*s == ‘Q‘) { printf("%d\n", query(readInt(), readInt())); } else if (*s == ‘R‘) { x = readInt(); while (!isalpha(*s = getchar())); T.modify(x, *s - ‘a‘); } else { x = readInt(); while (!isalpha(*s = getchar())); T.insert(x, *s - ‘a‘); ++n; } } return 0;}
BZOJ1014 [JSOI2008]火星人