標籤:bzoj oi splay
1014: [JSOI2008]火星人prefixTime Limit: 10 Sec Memory Limit: 162 MB
Submit: 3471 Solved: 1065
[Submit][Status]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、 詢問。文法:Q x y,x, y均為正整數。功能:計算LCQ(x, y) 限制:1 <= x, y <= 當前字串長度。 2、 修改。文法:R x d,x是正整數,d是字元。功能:將字串中第x個數修改為字元d。限制:x不超過當前字串長度。 3、 插入:文法:I x d,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
資料規模:
對於100%的資料,滿足:
1、 所有字串自始至終都只有小寫字母構成。
2、 M <= 150,000
3、 字串長度L自始至終都滿足L <= 100,000
4、 詢問操作的個數不超過10,000個。
對於第1,2個資料,字串長度自始至終都不超過1,000
對於第3,4,5個資料,沒有插入操作。
splay+hash+二分
用splay來維護字串的hash值。
每個結點的hash值為右兒子hash值+該結點hash值*29^(rson.size+1)+左兒子的hash值*29^(rson.size+2)
對於詢問,是一個常用的hash求最長公用首碼的方法:
二分ans,判斷ans長度下的hash值是否相同。若相同繼續增加ans,否則減小ans
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <string>#define LL long long#define mod 9875321using namespace std;char s[100005];LL b[100010];int tot=0,root,L,m;struct splay{int size,fa,v,l,r;LL hash;}a[300005];void Push_up(int x){a[x].size=a[a[x].l].size+a[a[x].r].size+1;a[x].hash=(a[a[x].l].hash*b[a[a[x].r].size+2]+a[x].v*b[a[a[x].r].size+1]+a[a[x].r].hash)%mod;}void New_node(int &x,int fa,char c){x=++tot;a[x].fa=fa;a[x].v=a[x].hash=c-'a'+1;a[x].l=a[x].r=0;a[x].size=1;}void Build(int &x,int fa,int l,int r){if (l>r) return;int m=(l+r)>>1;New_node(x,fa,s[m]);Build(a[x].l,x,l,m-1);Build(a[x].r,x,m+1,r);Push_up(x);}void zig(int x){int y=a[x].fa;int z=a[y].fa;a[x].fa=z,a[y].fa=x;a[y].l=a[x].r,a[a[x].r].fa=y,a[x].r=y;if (a[z].l==y) a[z].l=x;else a[z].r=x;Push_up(y);}void zag(int x){int y=a[x].fa;int z=a[y].fa;a[x].fa=z,a[y].fa=x;a[y].r=a[x].l,a[a[x].l].fa=y,a[x].l=y;if (a[z].l==y) a[z].l=x;else a[z].r=x;Push_up(y);}void splay(int x,int s){while (a[x].fa!=s){int y=a[x].fa;int z=a[y].fa;if (z==s){if (x==a[y].l) zig(x);else zag(x);break;}if (y==a[z].l){if (x==a[y].l) zig(y),zig(x);else zag(x),zig(x);}else{if (x==a[y].r) zag(y),zag(x);else zig(x),zag(x);}}Push_up(x);if (!s) root=x;}int Findkth(int x,int k){int p=a[a[x].l].size;if (p+1==k) return x;if (k<=p) return Findkth(a[x].l,k);return Findkth(a[x].r,k-1-p);}int Getmin(int x){while (a[x].l)x=a[x].l;return x;}void Insert(int x,char c){int k=Findkth(root,x+1);splay(k,0);k=Getmin(a[k].r);splay(k,root);New_node(a[k].l,k,c);splay(tot,0);}void Modify(int x,char c){int k=Findkth(root,x+1);splay(k,0);a[root].v=c-'a'+1;a[x].hash=(a[a[x].l].hash*b[a[a[x].r].size+2]+a[x].v*b[a[a[x].r].size+1]+a[a[x].r].hash)%mod;}int Query(int x,int l){x++;int k=x;x=Findkth(root,x);int y=Findkth(root,k+l);splay(y,0);splay(x,y); return (a[x].hash+mod-(a[a[x].l].hash*b[a[a[x].r].size+2])%mod)%mod;}int Solve(int x,int y){int ans=0,l=1,r=min(L-x,L-y)+1;while (l<=r){int m=(l+r)>>1;if (Query(x,m)==Query(y,m)) l=m+1,ans=m;else r=m-1;}return ans;}int main(){b[1]=1;for (int i=2;i<=100005;i++)b[i]=(b[i-1]*29LL)%mod;s[1]=s[2]='`';Build(root,0,1,2); scanf("%s",s);L=strlen(s);Build(a[2].l,2,0,L-1); splay(a[2].l,0);scanf("%d",&m);while (m--){char q[2],c[2];int x,y;scanf("%s",q);switch (q[0]){case 'Q':scanf("%d%d",&x,&y);printf("%d\n",Solve(x,y));break;case 'R':scanf("%d%s",&x,c);Modify(x,c[0]);break;case 'I':scanf("%d%s",&x,c);Insert(x,c[0]);L++;break;}}return 0;}
感悟:
1.看到這種包括修改查詢一個數列或字串的要想到splay~
【BZOJ 1014】 [JSOI2008]火星人prefix