记录编号 300973 评测结果 AAAAAAAAAA
题目名称 [NOI 2003]文本编辑器 最终得分 100
用户昵称 GravatarFoolMike 是否通过 通过
代码语言 C++ 运行时间 1.043 s
提交时间 2016-08-29 16:52:06 内存使用 60.37 MiB
显示代码纯文本
  1. #include<cstdio>
  2. #include<cstring>
  3. const int N=3000010;
  4. int t,l,q[N];bool m[N];
  5. char ch[N],S[N],a[N];
  6. struct Splay{
  7. char ch[N];
  8. int l[N],r[N],s[N],root,top,_x;
  9. Splay(){
  10. memset(l,0,sizeof l);
  11. memset(r,0,sizeof r);
  12. memset(ch,0,sizeof ch);
  13. memset(s,0,sizeof s);
  14. _x=root=1;top=r[1]=s[1]=2;s[2]=1;
  15. }
  16. inline void update(int x){
  17. s[x]=s[l[x]]+s[r[x]]+1;
  18. }
  19. inline void l_rotate(int &x){
  20. int y=r[x];
  21. r[x]=l[y];l[y]=x;
  22. update(y);update(x);
  23. x=y;
  24. }
  25. inline void r_rotate(int &x){
  26. int y=l[x];
  27. l[x]=r[y];r[y]=x;
  28. update(y);update(x);
  29. x=y;
  30. }
  31. void splay(int &x,int pos){
  32. int i=s[l[x]]+1;
  33. if (i==pos) return;
  34. if (i<pos) splay(r[x],pos-i),l_rotate(x);
  35. else splay(l[x],pos),r_rotate(x);
  36. }
  37. int build(int L,int R){
  38. if (L>R) return 0;
  39. int mid=(L+R)/2,now=++top;
  40. ch[now]=S[mid];s[now]=R-L+1;
  41. l[now]=build(L,mid-1);
  42. r[now]=build(mid+1,R);
  43. return now;
  44. }
  45. inline void insert(int len){
  46. splay(root,_x);
  47. splay(root,_x+1);
  48. for (int i=1;i<=len;i++)
  49. for (S[i]=0;S[i]<32||S[i]>126;S[i]=getchar());
  50. r[l[root]]=build(1,len);
  51. update(l[root]);
  52. update(root);
  53. }
  54. inline void del(int len){
  55. splay(root,_x);
  56. splay(root,_x+len+1);
  57. r[l[root]]=0;
  58. update(l[root]);
  59. update(root);
  60. }
  61. inline void move(int pos){_x=pos+1;}
  62. inline void next(){_x++;}
  63. inline void prev(){_x--;}
  64. inline void get(int len){
  65. splay(root,_x);
  66. splay(root,_x+len+1);
  67. print(r[l[root]]);
  68. puts("");
  69. }
  70. void print(int x){
  71. if (l[x]) print(l[x]);
  72. putchar(ch[x]);
  73. if (r[x]) print(r[x]);
  74. }
  75. }T;
  76. int main()
  77. {
  78. freopen("editor2003.in","r",stdin);
  79. freopen("editor2003.out","w",stdout);
  80. scanf("%d",&t);
  81. while (t--){
  82. scanf("%s",ch);
  83. if (ch[0]!='P'&&ch[0]!='N') scanf("%d",&l);
  84. gets(a);
  85. if (ch[0]=='I') T.insert(l);
  86. if (ch[0]=='D') T.del(l);
  87. if (ch[0]=='G') T.get(l);
  88. if (ch[0]=='M') T.move(l);
  89. if (ch[0]=='P') T.prev();
  90. if (ch[0]=='N') T.next();
  91. }
  92. return 0;
  93. }