【BZOJ】1014: [JSOI2008]火星人prefix(splay+hash+二分+lcp)

來源:互聯網
上載者:User

標籤:des   blog   http   io   ar   os   sp   for   on   

http://www.lydsy.com/JudgeOnline/problem.php?id=1014

被sb錯調哭了QAQ。。。insert那裡。。插入到第x個後邊。。。我。。。。。。寫成了第x個前面。。。。。。。。。。還調了!好!久!

QAQ

本題神lcp做法。。。。表示只會sa的height的離線。。。。。。。這種線上的我就QAQ做個憂傷的表情。。。

然後膜拜了題解。。。。好神。。splay維護區間。。。hash+二分維護lcp。。。。QAQ似乎是白書上說的麼。。。

但是這種有機率的題這樣搞真的好麼。。。。。

因此模數那裡直接抄人家的。。。因為不保證自己取的mod正確QAQ。。。。

然後就是在區間裡乘上hash的權,因為乘法分配率,所以是可以區間維護的。

。。

#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>#include <queue>#include <set>#include <map>using namespace std;typedef long long ll;#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << (#x) << " = " << (x) << endl#define error(x) (!(x)?puts("error"):0)#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }const int N=100005;const ll M=9875321;int P[N];struct node *null;struct node {node *f, *c[2];int k, s, h;node(int _k=0) { k=_k; s=1; h=_k; f=c[0]=c[1]=null; }void setc(node *x, bool d) { c[d]=x; x->f=this; }bool d() { return f->c[1]==this; }void pushup() {s=c[0]->s+c[1]->s+1;h=(c[0]->h+((ll)k*P[c[0]->s])%M+((ll)c[1]->h*P[c[0]->s+1])%M)%M;}}*root;void rot(node *x) {node *f=x->f; bool d=x->d();f->f->setc(x, f->d());f->setc(x->c[!d], d);x->setc(f, !d);f->pushup();if(f==root) root=x;}void splay(node *x, node *f=null) {while(x->f!=f)if(x->f->f==f) rot(x);else x->d()==x->f->d()?(rot(x->f), rot(x)):(rot(x), rot(x));x->pushup();}node *sel(node *x, int k) {int s=x->c[0]->s;if(s==k) return x;if(s<k) return sel(x->c[1], k-s-1); return sel(x->c[0], k);}node *getrange(int l, int r) {splay(sel(root, l-1));splay(sel(root, r+1), root);return root->c[1];}void ins(int k, int pos) {node *f=getrange(pos+1, pos);f->setc(new node(k), 0); splay(f->c[0]);}void fix(int k, int pos) {node *f=getrange(pos, pos);f->c[0]->k=k; f->c[0]->pushup();splay(f->c[0]);}int gethash(int l, int r) { return getrange(l, r)->c[0]->h; }int ask(int x, int y) {int s=root->s-2;int l=1, r=min(s-x, s-y)+1, mid;while(l<=r) {mid=(l+r)>>1;if(gethash(x, x+mid-1)==gethash(y, y+mid-1)) l=mid+1;else r=mid-1;}return l-1;}void U(node *x) { if(x==null) return; U(x->c[0]); U(x->c[1]); x->pushup(); }void Pr(node *x) { if(x==null) return; Pr(x->c[0]); printf("%c", x->k+‘a‘); Pr(x->c[1]); }void D(node *x=root) { Pr(x); puts(""); U(x); Pr(x); }char s[N];int n;void build(int l, int r, node *&x) {if(l>r) return;int mid=(l+r)>>1;x=new node(s[mid]-‘a‘);if(l==r) return;build(l, mid-1, x->c[0]);build(mid+1, r, x->c[1]);if(l<=mid-1) x->c[0]->f=x;if(mid+1<=r) x->c[1]->f=x;x->pushup();}void init() {P[0]=1;for1(i, 1, 100000) P[i]=(P[i-1]*26)%M;null=new node; null->s=0; null->f=null->c[0]=null->c[1]=null;root=new node; root->setc(new node, 1);node *x;build(1, n, x);root->c[1]->setc(x, 0); root->c[1]->pushup(); root->pushup();//D();}char rdchar() {char ret=getchar();while(ret<‘a‘||ret>‘z‘) ret=getchar();return ret;}int main() {scanf("%s", s+1); n=strlen(s+1);init();int m=getint();while(m--) {char c=getchar(); while(c!=‘Q‘&&c!=‘R‘&&c!=‘I‘) c=getchar();if(c==‘Q‘) { int x=getint(), y=getint(); printf("%d\n", ask(x, y)); }else if(c==‘R‘) { int x=getint(); fix(rdchar()-‘a‘, x); }else if(c==‘I‘) { int x=getint(); ins(rdchar()-‘a‘, x); }}return 0;}

  

 

 

 

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


 

 

Source

【BZOJ】1014: [JSOI2008]火星人prefix(splay+hash+二分+lcp)

聯繫我們

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