记录编号 454251 评测结果 AAAAAAAAAA
题目名称 [JSOI 2008]火星人prefix 最终得分 100
用户昵称 GravatarAnonymity 是否通过 通过
代码语言 C++ 运行时间 2.980 s
提交时间 2017-09-28 18:37:44 内存使用 1.17 MiB
显示代码纯文本
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #define maxn 100005
  6. using namespace std;
  7. inline int read()
  8. { char c=getchar();int x=0,y=1;
  9. while(c<'0'||c>'9'){if(c=='-') y=-1;c=getchar();}
  10. while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
  11. return x*y;
  12. }
  13. inline char readchar(){char c;for(c=getchar();c<'a'||c>'z';c=getchar());return c;}
  14. inline char readop(){char c;for(c=getchar();c!='Q'&&c!='R'&&c!='I';c=getchar());return c;}
  15. template<typename T>
  16. inline T m_min(T x,T y){return x<y?x:y;}
  17. const int P=123;
  18. typedef unsigned int ull;
  19. char T[maxn];
  20. int len,m;
  21. ull H[maxn],xp[maxn];
  22. struct Treap
  23. { Treap *ch[2];int s,r;ull Hash,v;
  24. Treap(ull x=0):v(x),Hash(x),r(rand()),s(1){ch[0]=ch[1]=NULL;}
  25. inline int sz(){return this?this->s:0;}
  26. inline ull hv(){return this?this->Hash:0;}
  27. inline void mt()
  28. { if(!this) return;
  29. this->s=this->ch[0]->sz()+this->ch[1]->sz()+1;
  30. this->Hash=this->ch[0]->hv()+this->v*xp[this->ch[0]->sz()]+this->ch[1]->hv()*xp[this->ch[0]->sz()+1];
  31. }
  32. inline void paint(ull x){if(this){this->v=this->Hash=x;}}
  33. }*root;
  34. typedef pair<Treap*,Treap*> dt;
  35. inline Treap* merge(Treap* x,Treap* y)
  36. { if(!x) return y;if(!y) return x;
  37. if(x->r < y->r){x->ch[1]=merge(x->ch[1],y);x->mt();return x;}
  38. else{y->ch[0]=merge(x,y->ch[0]);y->mt();return y;}
  39. }
  40. inline dt split(Treap* x,int k)
  41. { if(!x) return dt(NULL,NULL);if(!k) return dt(NULL,x);
  42. dt y;
  43. if(x->ch[0]->sz()>=k) y=split(x->ch[0],k),x->ch[0]=y.second,x->mt(),y.second=x;
  44. else y=split(x->ch[1],k-x->ch[0]->sz()-1),x->ch[1]=y.first,x->mt(),y.first=x;
  45. return y;
  46. }
  47. inline Treap* build(char* s,int y)
  48. { Treap* *st=new Treap*[len];Treap *las,*x,*ans;int tail=0;
  49. for(int i=1;i<=y;++i)
  50. { x=new Treap(s[i]-'a');las=NULL;
  51. while(tail&&st[tail-1]->r > x->r)
  52. st[tail-1]->mt(),las=st[tail-1],st[--tail]=NULL;
  53. if(tail) st[tail-1]->ch[1]=x;
  54. x->ch[0]=las;st[tail++]=x;
  55. }
  56. while(tail) st[--tail]->mt();
  57. ans=st[0];
  58. delete []st;
  59. return ans;
  60. }
  61. inline ull getmes(int x,int y)
  62. { dt t1=split(root,y),t2=split(t1.first,x-1);
  63. ull tmp=t2.second->hv();root=merge(merge(t2.first,t2.second),t1.second);
  64. return tmp;
  65. }
  66. inline int query(int x,int y)
  67. { if(x==y) return len-x+1;
  68. int rig=m_min(len-x+1,len-y+1),lef=0,mid,ans=0;
  69. while(lef<=rig)
  70. { mid=lef+rig>>1;
  71. if(getmes(x,x+mid-1)==getmes(y,y+mid-1)) ans=mid,lef=mid+1;
  72. else rig=mid-1;
  73. }
  74. return ans;
  75. }
  76. inline void change(int x,ull y)
  77. { dt t1=split(root,x-1),t2=split(t1.second,1);
  78. t2.first->paint(y);
  79. root=merge(t1.first,merge(t2.first,t2.second));
  80. }
  81. inline void insert(int x,ull y)
  82. { Treap* tmp=new Treap(y);
  83. dt t1=split(root,x);root=merge(merge(t1.first,tmp),t1.second);
  84. }
  85. int main()
  86. { freopen("bzoj_1014.in","r",stdin);
  87. freopen("bzoj_1014.out","w",stdout);
  88. scanf("%s",T+1);m=read();len=strlen(T+1);xp[0]=1;
  89. for(int i=1;i<=100000;++i) xp[i]=xp[i-1]*P;
  90. root=build(T,len);int x,y;char z;
  91. for(int i=1;i<=m;++i)
  92. { z=readop();
  93. if(z=='Q'){x=read();y=read();printf("%d\n",query(x,y));}
  94. else if(z=='R'){x=read();z=readchar();change(x,z-'a');}
  95. else{x=read();z=readchar();insert(x,z-'a');++len;}
  96. }
  97. return 0;
  98. }