[bzoj1014][splay][JSOI2008]火星人prefix
2017-08-08 16:17
330 查看
1014: [JSOI2008]火星人prefix
Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 7646 Solved: 2452
[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 Input
madamimadam
7
Q 1 7
Q 4 8
Q 10 11
R 3 a
Q 1 7
I 10 a
Q 2 11
Sample Output
5
1
0
2
1
HINT
1、所有字符串自始至终都只有小写字母构成。
2、M<=150,000
3、字符串长度L自始至终都满足L<=100,000
4、询问操作的个数不超过10,000个。
对于第1,2个数据,字符串长度自始至终都不超过1,000
对于第3,4,5个数据,没有插入操作。
Source
sol:
用splay维护一下hash之,询问就是转出目标区间即可。(这题能不能用分块做?我不怎么会分块,第一反应是分块。
Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 7646 Solved: 2452
[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 Input
madamimadam
7
Q 1 7
Q 4 8
Q 10 11
R 3 a
Q 1 7
I 10 a
Q 2 11
Sample Output
5
1
0
2
1
HINT
1、所有字符串自始至终都只有小写字母构成。
2、M<=150,000
3、字符串长度L自始至终都满足L<=100,000
4、询问操作的个数不超过10,000个。
对于第1,2个数据,字符串长度自始至终都不超过1,000
对于第3,4,5个数据,没有插入操作。
Source
sol:
用splay维护一下hash之,询问就是转出目标区间即可。(这题能不能用分块做?我不怎么会分块,第一反应是分块。
#include<cstdio> #include<algorithm> #include<string> #include<cstring> #include<cstdlib> #include<cmath> #include<iostream> using namespace std; typedef long long ll; int n,m; inline int read() { char c; int res,flag=0; while((c=getchar())>'9'||c<'0') if(c=='-')flag=1; res=c-'0'; while((c=getchar())>='0'&&c<='9') res=(res<<3)+(res<<1)+c-'0'; return flag?-res:res; } const int N=260000; int rt,lc ,rc ,fa ,size ,hash ; ll s ; char c ; inline void updata(int x) { size[x]=size[lc[x]]+size[rc[x]]+1; hash[x]=(hash[lc[x]]*1007+c[x])*s[size[rc[x]]]+hash[rc[x]]; } inline void rotate(int x) { int y=fa[x],z=fa[y]; int b=lc[y]==x?rc[x]:lc[x]; fa[x]=z;fa[y]=x; if(b) fa[b]=y; if(z) { if(lc[z]==y) lc[z]=x; else rc[z]=x; } if(lc[y]==x) lc[y]=b,rc[x]=y; else rc[y]=b,lc[x]=y; updata(y); } inline int find_kth(int x,int y) { if(size[lc[x]]>=y) find_kth(lc[x],y); else if(size[lc[x]]+1==y) return x; else find_kth(rc[x],y-size[lc[x]]-1); } inline void splay(int x,int f) { int y,z; while(fa[x]!=f) { y=fa[x];z=fa[y]; if(z!=f) { if((lc[z]==y)==(lc[y]==x)) rotate(y); else rotate(x); } rotate(x); } updata(x); if(!fa[x]) rt=x; } int tot; char sr ; int build(int &x,int l,int r) { x=++tot; size[x]=1; int mid=l+r>>1; c[x]=sr[mid]; hash[x]=c[x]; if(l<=mid-1) {build(lc[x],l,mid-1);fa[lc[x]]=x;} if(mid+1<=r) {build(rc[x],mid+1,r);fa[rc[x]]=x;} updata(x); } inline bool check(int x,int y,int mid) { int pos1,pos2,z; pos1=find_kth(rt,x-1); pos2=find_kth(rt,x+mid); splay(pos1,0); splay(pos2,pos1); z=hash[lc[pos2]]; pos1=find_kth(rt,y-1); pos2=find_kth(rt,y+mid); splay(pos1,0); splay(pos2,pos1); if(z==hash[lc[pos2]]) return 1; return 0; } int main() { // freopen("1014.in","r",stdin); // freopen(".out","w",stdout); scanf("%s",sr+1); s[0]=1; for(int i=1;i<=250000;++i) s[i]=s[i-1]*1007; n=strlen(sr+1); m=read(); build(rt,0,n+1); int x,y,pos,l,r,mid; char op[5]; for(int i=1;i<=m;++i) { scanf("%s",sr+1); if(sr[1]=='Q') { x=read()+1;y=read()+1; if(x>y) swap(x,y); l=0;r=tot-y; while(l+1<r) { mid=l+r>>1; if(check(x,y,mid)) l=mid; else r=mid; } if(check(x,y,r)) printf("%d\n",r); else printf("%d\n",l); } else if(sr[1]=='R') { x=read();scanf("%s",op+1); pos=find_kth(rt,x+1); c[pos]=op[1]; updata(pos); splay(pos,0); } else if(sr[1]=='I') { x=read();scanf("%s",op+1); pos=find_kth(rt,x+1); if(rc[pos]) { pos=rc[pos]; while(lc[pos]) pos=lc[pos]; lc[pos]=++tot; c[tot]=op[1]; fa[tot]=pos; updata(tot); splay(tot,0); } else { rc[pos]=++tot; c[tot]=op[1]; fa[tot]=pos; updata(tot); splay(tot,0); } } } }
相关文章推荐
- [BZOJ]1014 [JSOI]2008 火星人prefix Splay
- [BZOJ1014][JSOI2008]火星人prefix(hash+splay)
- [bzoj1014](JSOI2008)火星人 prefix (Splay维护哈希)
- bzoj1014: [JSOI2008]火星人prefix splay+hash
- bzoj 1014: [JSOI2008]火星人prefix (hash+splay)
- bzoj 1014 [JSOI2008]火星人prefix(splay+hash)
- [BZOJ1014][JSOI2008][Splay][RKHash]火星人prefix
- BZOJ 1014: [JSOI2008]火星人prefix( splay + hash )
- bzoj 1014: [JSOI2008]火星人prefix hash && splay
- [BZOJ1014][JSOI2008]火星人prefix splay+二分+hash
- [BZOJ]1014: [JSOI2008]火星人prefix splay+hash+二分
- BZOJ_1014_[JSOI2008]_火星人prefix_(Splay+LCP_Hash+二分)
- BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】
- 【BZOJ1014】【JSOI2008】火星人prefix Splay处理区间,hash+dichotomy(二分)check出解
- 【bzoj1014】[JSOI2008]火星人prefix splay+hash+二分
- 【BZOJ1014】【JSOI2008】火星人prefix Splay处理区间,hash+dichotomy(二分)check出解
- BZOJ 1014 JSOI2008 火星人prefix Splay+Hash+二分
- 【BZOJ】1014: [JSOI2008]火星人prefix(splay+hash+二分+lcp)
- [bzoj1014][JSOI2008]火星人prefix【splay】【哈希】【二分】
- BZOJ 1014 JSOI2008 火星人prefix Splay+Hash+二分