1014: [JSOI2008]火星人prefix
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2918 Solved: 866
[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 Input
7
Q 1 7
Q 4 8
Q 10 11
R 3 a
Q 1 7
I 10 a
Q 2 11
Sample Output
1
0
2
1
数据规模:
对于100%的数据,满足:
1、 所有字符串自始至终都只有小写字母构成。
2、 M <= 150,000
3、 字符串长度L自始至终都满足L <= 100,000
4、 询问操作的个数不超过10,000个。
对于第1,2个数据,字符串长度自始至终都不超过1,000
对于第3,4,5个数据,没有插入操作。
HINT
Source
题解:
以前看着这题好麻烦,什么都不会的说。。。
现在看起来用splay似乎是显然的。。。
还是吐槽:
1.第一遍打错了好几个关键的地方竟然还能得70分。。。
2.为什么我的splay总是这么慢。。。
代码:
1.mine 光荣的TLE,不过在tyvj上AC的
1 const maxn=100000+10; 2 p=9875321; 3 var i,n,m,x,t,y,z,l,r,rt,mid,tot:longint; 4 ch:char; 5 st:ansistring; 6 s,v,w,fa,a:array[0..maxn] of int64; 7 c:array[0..maxn,0..1] of longint; 8 procedure pushup(x:longint); 9 begin 10 s[x]:=s[c[x,0]]+s[c[x,1]]+1; 11 w[x]:=(w[c[x,0]]*a[s[c[x,1]]+1] mod p+ 12 v[x]*a[s[c[x,1]]] mod p 13 +w[c[x,1]] mod p) mod p; 14 end; 15 procedure rotate(x:longint;var k:longint); 16 var l,r,y,z:longint; 17 begin 18 y:=fa[x];z:=fa[y]; 19 l:=ord(c[y,1]=x);r:=l xor 1; 20 if y=k then k:=x else c[z,ord(c[z,1]=y)]:=x; 21 fa[x]:=z;fa[y]:=x;fa[c[x,r]]:=y; 22 c[y,l]:=c[x,r];c[x,r]:=y; 23 pushup(y);pushup(x); 24 end; 25 procedure splay(x:longint;var k:longint); 26 var y,z:longint; 27 begin 28 while x<>k do 29 begin 30 y:=fa[x];z:=fa[y]; 31 if y<>k then 32 if (c[z,0]=y) xor (c[y,0]=x) then rotate(x,k) 33 else rotate(y,k); 34 rotate(x,k); 35 end; 36 end; 37 function find(x,rank:longint):longint; 38 var l,r:longint; 39 begin 40 l:=c[x,0];r:=c[x,1]; 41 if s[l]+1=rank then exit(x) 42 else if s[l]>=rank then exit(find(l,rank)) 43 else exit(find(r,rank-s[l]-1)); 44 end; 45 procedure del(k:longint); 46 var x,y,z:longint; 47 begin 48 x:=find(rt,k-1);y:=find(rt,k+1); 49 splay(x,rt);splay(y,c[x,1]); 50 z:=c[y,0];fa[z]:=0;s[z]:=0;c[y,0]:=0; 51 pushup(y);pushup(x); 52 end; 53 54 procedure build(l,r,f:longint); 55 var mid,now,last:longint; 56 begin 57 if l>r then exit; 58 mid:=(l+r)>>1; 59 now:=mid;last:=f; 60 fa[now]:=last; 61 c[last,ord(mid>f)]:=now; 62 if l=r then 63 begin 64 s[now]:=1;w[now]:=v[now]; 65 exit; 66 end; 67 build(l,mid-1,mid);build(mid+1,r,mid); 68 pushup(now); 69 end; 70 71 procedure init; 72 begin 73 a[0]:=1; 74 for i:=1 to maxn do a[i]:=(a[i-1]*31) mod p; 75 readln(st);n:=length(st); 76 for i:=2 to n+1 do v[i]:=ord(st[i-1])-ord('a')+1; 77 build(1,n+2,0);rt:=(n+3)>>1; 78 end; 79 function check(l,r,len:longint):boolean; 80 var x,y,xx,yy:longint; 81 begin 82 if len=0 then exit(true); 83 x:=find(rt,l);y:=find(rt,l+len+1); 84 splay(x,rt);splay(y,c[x,1]); 85 xx:=w[c[y,0]]; 86 x:=find(rt,r);y:=find(rt,r+len+1); 87 splay(x,rt);splay(y,c[x,1]); 88 yy:=w[c[y,0]]; 89 if xx=yy then exit(true) else exit(false); 90 end; 91 function query(x,y:longint):longint; 92 var l,r,mid:longint; 93 begin 94 if x>y then begin t:=x;x:=y;y:=t;end; 95 if x=y then exit(tot-y-1); 96 if not(check(x,y,1)) then exit(0); 97 if check(x,y,tot-y-1) then exit(tot-y-1); 98 l:=2;r:=tot-y-1; 99 while l<r do 100 begin 101 mid:=(l+r)>>1; 102 if check(x,y,mid) then l:=mid+1 else r:=mid; 103 end; 104 exit(l-1); 105 end; 106 procedure main; 107 begin 108 readln(m);tot:=n+2; 109 for i:=1 to m do 110 begin 111 read(ch); 112 case ch of 113 'R':begin 114 read(x);read(ch);readln(ch); 115 x:=find(rt,x+1); 116 splay(x,rt); 117 v[x]:=ord(ch)-ord('a')+1; 118 pushup(x); 119 end; 120 'I':begin 121 read(z);read(ch);readln(ch); 122 x:=find(rt,z+1);y:=find(rt,z+2); 123 splay(x,rt);splay(y,c[x,1]); 124 inc(tot);c[y,0]:=tot;fa[tot]:=y;s[tot]:=1; 125 v[tot]:=ord(ch)-ord('a')+1;w[tot]:=v[tot]; 126 pushup(y);pushup(x); 127 end; 128 'Q':begin 129 readln(x,y); 130 writeln(query(x,y)); 131 end; 132 end; 133 end; 134 end; 135 begin 136 assign(input,'input.txt');assign(output,'output.txt'); 137 reset(input);rewrite(output); 138 init; 139 main; 140 close(input);close(output); 141 end. 142