bzoj1014 [JSOI2008]火星人

來源:互聯網
上載者:User

標籤:tchar   接下來   stdin   ada   out   std   cstring   des   注意   

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個資料,沒有插入操作。

 

正解:$splay$+$hash$。

這題要修改和插入字元,那麼我們可以使用$splay$來維護整個串。維護每個結點表示的字母,子樹區間形成的字串的$hash$值,還有子樹大小,對於各種操作討論一下就行了。

注意一點,$a$的$hash$值不能是$0$。話說我以前一直這麼用也沒被坑。。

 

  1 //It is made by wfj_2048~  2 #include <algorithm>  3 #include <iostream>  4 #include <cstring>  5 #include <cstdlib>  6 #include <cstdio>  7 #include <vector>  8 #include <cmath>  9 #include <queue> 10 #include <stack> 11 #include <map> 12 #include <set> 13 #define base (157ULL) 14 #define inf (1<<30) 15 #define N (500010) 16 #define il inline 17 #define RG register 18 #define ull unsigned long long 19 #define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout) 20  21 using namespace std; 22  23 int ch[N][2],fa[N],sz[N],n,m,rt,tot; 24 ull sum[N],val[N],bin[N]; 25 char s[N],c[5]; 26  27 il int gi(){ 28     RG int x=0,q=1; RG char ch=getchar(); 29     while ((ch<‘0‘ || ch>‘9‘) && ch!=‘-‘) ch=getchar(); 30     if (ch==‘-‘) q=-1,ch=getchar(); 31     while (ch>=‘0‘ && ch<=‘9‘) x=x*10+ch-48,ch=getchar(); 32     return q*x; 33 } 34  35 il void pushup(RG int x){ 36     RG int l=ch[x][0],r=ch[x][1]; sz[x]=sz[l]+sz[r]+1; 37     sum[x]=sum[l]*bin[sz[x]-sz[l]]+val[x]*bin[sz[r]]+sum[r]; return; 38 } 39  40 il void rotate(RG int x){ 41     RG int y=fa[x],z=fa[y],k=ch[y][0]==x; 42     ch[z][ch[z][1]==y]=x,fa[x]=z; 43     ch[y][k^1]=ch[x][k],fa[ch[x][k]]=y; 44     ch[x][k]=y,fa[y]=x,pushup(y),pushup(x); return; 45 } 46  47 il void splay(RG int x,RG int goal){ 48     while (fa[x]!=goal){ 49     RG int y=fa[x],z=fa[y]; 50     if (fa[y]!=goal){ 51         if ((ch[z][0]==y)^(ch[y][0]==x)) rotate(x); 52         else rotate(y); 53     } 54     rotate(x); 55     } 56     if (!goal) rt=x; return; 57 } 58  59 il int build(RG int l,RG int r){ 60     RG int mid=(l+r)>>1,x; if (!mid) x=n+2; else x=mid; 61     if (l<mid) ch[x][0]=build(l,mid-1),fa[ch[x][0]]=x; 62     if (r>mid) ch[x][1]=build(mid+1,r),fa[ch[x][1]]=x; 63     val[x]=s[mid]-‘a‘+1,pushup(x); return x; 64 } 65  66 il int find(RG int x,RG int k){ 67     if (k==sz[ch[x][0]]+1) return x; 68     if (k<=sz[ch[x][0]]) return find(ch[x][0],k); 69     else return find(ch[x][1],k-sz[ch[x][0]]-1); 70 } 71  72 il int calc(RG int x,RG int y){ 73     RG int l=1,r=n-y+1,mid,k,ans=0; RG ull hsh1,hsh2; 74     while (l<=r){ 75     mid=(l+r)>>1,k=find(rt,x),splay(k,0); 76     k=find(rt,x+mid+1),splay(k,rt),hsh1=sum[ch[k][0]]; 77     k=find(rt,y),splay(k,0); 78     k=find(rt,y+mid+1),splay(k,rt),hsh2=sum[ch[k][0]]; 79     if (hsh1==hsh2) ans=mid,l=mid+1; else r=mid-1; 80     } 81     return ans; 82 } 83  84 il void work(){ 85     bin[0]=1; for (RG int i=1;i<=100000;++i) bin[i]=bin[i-1]*base; 86     scanf("%s",s+1),m=gi(),n=strlen(s+1); 87     s[0]=s[n+1]=‘a‘-1,rt=build(0,n+1),tot=n+2; 88     for (RG int i=1,l,r,x,k;i<=m;++i){ 89     scanf("%s",c); 90     if (c[0]==‘Q‘) l=gi(),r=gi(),printf("%d\n",calc(l,r)); 91     if (c[0]==‘R‘){ 92         x=gi(),scanf("%s",c),k=find(rt,x+1); 93         val[k]=c[0]-‘a‘+1,pushup(k),splay(k,0); 94     } 95     if (c[0]==‘I‘){ 96         x=gi(),scanf("%s",c),++n; 97         k=find(rt,x+1),splay(k,0); 98         k=find(rt,x+2),splay(k,rt); 99         ch[k][0]=++tot,fa[tot]=k,sz[tot]=1;100         val[tot]=c[0]-‘a‘+1,splay(tot,0);101     }102     }103     return;104 }105 106 int main(){107     File("prefix");108     work();109     return 0;110 }

 

bzoj1014 [JSOI2008]火星人

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.