记录编号 360817 评测结果 AAAAAAAAAA
题目名称 [JSOI 2008]火星人prefix 最终得分 100
用户昵称 GravatarFoolMike 是否通过 通过
代码语言 C++ 运行时间 9.168 s
提交时间 2017-01-01 16:08:59 内存使用 30.30 MiB
显示代码纯文本
  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<cstring>
  4. using namespace std;
  5. typedef unsigned long long ll;
  6. const int N=5e5+10,NP=3;
  7. const ll p[NP]={1e6+7,1e7+7,1e8+7};
  8. char str[N],ch[10];int x,y;
  9. int n,q,son[N][2],fa[N],k[N],s[N],Root,top;
  10. bool root[N];
  11. ll hs[N][NP],mi[N][NP];
  12. #define lc son[x][0]
  13. #define rc son[x][1]
  14. void update(int x){
  15. s[x]=s[lc]+s[rc]+1;
  16. for (int j=0;j<NP;j++)
  17. hs[x][j]=((hs[lc][j]*26+k[x])*mi[s[rc]][j]+hs[rc][j])%p[j];
  18. }
  19. void New(int Fa,int K){//插入右子树
  20. k[++top]=K;
  21. fa[son[Fa][1]=top]=Fa;
  22. update(top);
  23. }
  24. void rot(int x){
  25. int y=fa[x],z=fa[y];
  26. bool b=(x==son[y][1]);
  27. fa[son[y][b]=son[x][!b]]=y;
  28. fa[son[x][!b]=y]=x;
  29. fa[x]=z;
  30. if (son[z][0]==y) son[z][0]=x;else
  31. if (son[z][1]==y) son[z][1]=x;
  32. root[x]=root[y];root[y]=0;
  33. update(y);update(x);
  34. }
  35. void splay(int x){
  36. while (fa[x]){
  37. int y=fa[x],z=fa[y];
  38. if (root[y]||root[z]){rot(x);continue;}
  39. if (y==son[z][0]) rot(x==son[y][0]?y:x);
  40. else rot(x==son[y][1]?y:x);
  41. rot(x);
  42. }
  43. Root=x;
  44. }
  45. void find(int key){
  46. int x=Root;
  47. while (1){
  48. int i=s[lc]+1;
  49. if (i==key) break;
  50. if (key>i) key-=i,x=rc;
  51. else x=lc;
  52. }
  53. splay(x);
  54. }
  55. void hash(int x,int L,ll *ans){//以x开头,长为L的hash值
  56. find(x);find(x+L+1);
  57. int le=son[Root][0],th=son[le][1];
  58. for (int j=0;j<NP;j++) ans[j]=hs[th][j];
  59. }
  60. void insert(int p,int c){
  61. find(p+1);
  62. find(p+2);
  63. int le=son[Root][0];
  64. New(le,c);
  65. update(le);update(Root);
  66. ++n;
  67. }
  68. void change(int p,int c){
  69. find(p+1);
  70. k[Root]=c;
  71. update(Root);
  72. }
  73. int lcp(int x,int y){
  74. int l=0,r=n+1-max(x,y);
  75. ll hsx[NP],hsy[NP];
  76. while (l!=r){
  77. int mid=(l+r)>>1;
  78. hash(x,mid+1,hsx);
  79. hash(y,mid+1,hsy);
  80. bool check=1;
  81. for (int j=0;j<NP;j++)
  82. if (hsx[j]!=hsy[j]) check=0;
  83. if (check) l=mid+1;else r=mid;
  84. }
  85. return l;
  86. }
  87. void init(){
  88. for (int j=0;j<NP;j++) mi[0][j]=1;
  89. for (int i=1;i<=1e5;i++)
  90. for (int j=0;j<NP;j++)
  91. mi[i][j]=mi[i-1][j]*26%p[j];
  92. s[1]=2;s[2]=1;top=2;
  93. fa[son[1][1]=2]=1;
  94. root[Root=1]=1;
  95. }
  96. int main()
  97. {
  98. freopen("bzoj_1014.in","r",stdin);
  99. freopen("bzoj_1014.out","w",stdout);
  100. init();
  101. scanf("%s%d",str+1,&q);
  102. int len=strlen(str+1);
  103. for (int i=1;i<=len;i++) insert(i-1,str[i]-'a');
  104. while (q--){
  105. scanf("%s%d",ch,&x);
  106. if (ch[0]=='Q')
  107. scanf("%d",&y),printf("%d\n",lcp(x,y));
  108. else
  109. if (ch[0]=='R')
  110. scanf("%s",ch),change(x,ch[0]-'a');
  111. else
  112. if (ch[0]=='I')
  113. scanf("%s",ch),insert(x,ch[0]-'a');
  114. }
  115. return 0;
  116. }