1014: [JSOI2008]火星人prefix

來源:互聯網
上載者:User

標籤:計算   函數   signed   splay   處理   int   rip   ima   ada   

1014: [JSOI2008]火星人prefixTime Limit: 10 Sec  Memory Limit: 162 MB Submit: 7820  Solved: 2487 [Submit][Status][Discuss]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個資料,沒有插入操作。

 

對於沒有插入的情況,用O(L)的時間預先處理hash值,然後二分長度,再用O(1)的時間判斷是否存在,單次詢問的時間複雜度為O(log(L))

加入插入之後,可以用Splay維護區間hash值,然後類似上面,只是需要額外的O(log(L))的時間提取出區間,單次詢問的時間複雜度為O(log(L) * log(L))

#include <cstdio>#include <cstring>const int maxn = 110000 + 10;typedef unsigned long long ll;const ll seed = 131;char val[maxn];int tot, root, fa[maxn], son[maxn][2], siz[maxn], cnt = 0;ll hash[maxn], pow[maxn];inline void PushUp(int rt){    siz[rt] = siz[son[rt][0]] + siz[son[rt][1]] + 1;    hash[rt] = hash[son[rt][0]] + val[rt] * pow[siz[son[rt][0]]] + hash[son[rt][1]] * pow[siz[son[rt][0]] + 1];}inline void Rotate(int x){    int f = fa[x], p = son[f][0] == x;    son[f][!p] = son[x][p];    fa[son[x][p]] = f;    fa[x] = fa[f];    if(fa[x]) son[fa[x]][son[fa[x]][1] == f] = x;    son[x][p] = f;    fa[f] = x;    PushUp(f);}inline void Splay(int x, int t){    int y, z;    while(fa[x] != t){        if(fa[fa[x]] == t) Rotate(x);        else{            y = fa[x];            z = fa[y];            if(son[y][1] == x ^ son[z][1] == y) Rotate(x);            else Rotate(y);            Rotate(x);        }    }    PushUp(x);    if(!t) root = x;}inline void Find(int kth, int t){    int x = root, tmp;    while(x){        tmp = siz[son[x][0]];        if(kth == tmp + 1){            Splay(x, t);            return;        }        else if(kth < tmp + 1) x = son[x][0];        else{            x = son[x][1];            kth -= tmp + 1;        }    }}char s[maxn];int Insert(int st, int en){    if(st > en) return 0;    int mid = st + en >> 1, now = ++cnt;    val[now] = s[mid];    son[now][0] = Insert(st, mid - 1);    if(son[now][0]) fa[son[now][0]] = now;    son[now][1] = Insert(mid + 1, en);    if(son[now][1]) fa[son[now][1]] = now;    PushUp(now);    return now;}inline void LCQ(){    int x, y;    scanf("%d %d", &x, &y);    int l = 1, r = tot, mid, ret = 0;    ll tmp;    while(l <= r){        mid = l + r >> 1;        if(y + mid - 1 > tot)    r = mid - 1;        else{            Find(x, 0);            Find(x + mid + 1, root);            tmp = hash[son[son[root][1]][0]];            Find(y, 0);            Find(y + mid + 1, root);            if(tmp == hash[son[son[root][1]][0]]){                ret = mid;                l = mid + 1;            }            else r = mid - 1;        }    }    printf("%d\n", ret);}int main(){    pow[0] = 1;    for(int i = 1; i < maxn; i++)        pow[i] = pow[i - 1] * seed;    siz[0] = hash[0] = fa[0] = son[0][0] = son[0][1] = 0;    scanf("%s", s + 1);    tot = strlen(s + 1);    root = Insert(0, tot + 1);    int m;    scanf("%d", &m);    char opt[5], d[5];    int x, y, t;    while(m--){        scanf("%s", opt);        switch(opt[0]){            case ‘I‘:                scanf("%d%s", &x, d);                Find(x + 1, 0);                Find(x + 2, root);                cnt++;                val[cnt] = d[0];                fa[cnt] = son[root][1];                son[son[root][1]][0] = cnt;                PushUp(cnt);                PushUp(son[root][1]);                PushUp(root);                tot++;                break;            case ‘R‘:                scanf("%d%s", &x, d);                Find(x, 0);                Find(x + 2, root);                t = son[son[root][1]][0];                val[t] = d[0];                PushUp(t);                PushUp(son[root][1]);                PushUp(root);                break;            case ‘Q‘:                LCQ();                break;        }    }    return 0;}

 

 

1014: [JSOI2008]火星人prefix

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.