1014

思路:

  平衡树+二分答案+hash;

  好了懂了吧。

代码:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. #define maxn 2000005
  9.  
  10. int ch[maxn][],key[maxn],f[maxn],root,size[maxn];
  11. int n,m,tot;
  12.  
  13. long long ha[maxn],mi[maxn];
  14.  
  15. char ai[maxn];
  16.  
  17. inline void in(int &now)
  18. {
  19. char Cget=getchar();now=;
  20. while(Cget>''||Cget<'') Cget=getchar();
  21. while(Cget>=''&&Cget<='')
  22. {
  23. now=now*+Cget-'';
  24. Cget=getchar();
  25. }
  26. }
  27.  
  28. inline bool getson(int now)
  29. {
  30. return ch[f[now]][]==now;
  31. }
  32.  
  33. inline void updata(int now)
  34. {
  35. size[now]=,ha[now]=;
  36. if(ch[now][]) size[now]+=size[ch[now][]],ha[now]+=ha[ch[now][]];
  37. ha[now]+=key[now]*mi[size[now]-];
  38. if(ch[now][]) ha[now]+=ha[ch[now][]]*mi[size[now]],size[now]+=size[ch[now][]];
  39. }
  40.  
  41. inline void rotate(int now,int &to)
  42. {
  43. int fa=f[now],ffa=f[fa];bool pos=getson(now);
  44. ch[fa][pos]=ch[now][pos^];
  45. if(ch[fa][pos]) f[ch[fa][pos]]=fa;
  46. if(to==fa) to=now;
  47. else ch[ffa][getson(fa)]=now;
  48. ch[now][pos^]=fa,f[fa]=now,f[now]=ffa;
  49. updata(fa),updata(now);
  50. }
  51.  
  52. void splay(int now,int &to)
  53. {
  54. while(now!=to)
  55. {
  56. int fa=f[now],ffa=f[fa];
  57. if(fa!=to)
  58. {
  59. if(ch[fa][]==now^ch[ffa][]==fa) rotate(now,to);
  60. else rotate(now,to);
  61. }rotate(now,to);
  62. }
  63. }
  64.  
  65. void find(int p,bool pos)
  66. {
  67. int now=root;
  68. while()
  69. {
  70. if(size[ch[now][]]>=p) now=ch[now][];
  71. else
  72. {
  73. p-=size[ch[now][]];
  74. if(p==)
  75. {
  76. if(pos) splay(now,root);
  77. else splay(now,ch[root][]);
  78. return ;
  79. }
  80. else p--,now=ch[now][];
  81. }
  82. }
  83. }
  84.  
  85. void insert(int p,int ci)
  86. {
  87. find(p,true),find(p+,false);
  88. ch[ch[root][]][]=++tot;
  89. key[tot]=ci,size[tot]=,f[tot]=ch[root][],ha[tot]=ci;
  90. updata(ch[root][]),updata(root);
  91. }
  92.  
  93. int tree_build(int l,int r,int fa)
  94. {
  95. int now=l+r>>;key[now]=ai[now],f[now]=fa;
  96. if(now>l) ch[now][]=tree_build(l,now-,now);
  97. if(now<r) ch[now][]=tree_build(now+,r,now);
  98. updata(now);return now;
  99. }
  100.  
  101. long long hash_ci(int p,int len)
  102. {
  103. find(p,true),find(p+len+,false);
  104. return ha[ch[ch[root][]][]];
  105. }
  106.  
  107. int main()
  108. {
  109. mi[]=;
  110. for(int i=;i<=;i++) mi[i]=mi[i-]*27LL;
  111. scanf("%s",ai+),tot=strlen(ai+)+;
  112. for(int i=;i<=tot-;i++) ai[i]=ai[i]-'a'+;
  113. root=tree_build(,tot,);
  114. in(m);char op[];int u,v;
  115. for(;m--;)
  116. {
  117. scanf("%s",op);
  118. if(op[]=='Q')
  119. {
  120. in(u),in(v);
  121. if(u>v) swap(u,v);
  122. int l=,r=tot-v-,ans=;
  123. while(l<=r)
  124. {
  125. int mid=l+r>>;
  126. if(hash_ci(u,mid)==hash_ci(v,mid)) ans=mid,l=mid+;
  127. else r=mid-;
  128. }
  129. printf("%d\n",ans);
  130. }
  131. else if(op[]=='R')
  132. {
  133. in(u),scanf("%s",op);
  134. find(u+,true),key[root]=op[]-'a'+,updata(root);
  135. }
  136. else
  137. {
  138. in(u),scanf("%s",op);
  139. insert(u+,(int)(op[]-'a'+));
  140. }
  141. }
  142. return ;
  143. }

AC日记——[JSOI2008]火星人prefix bzoj 1014的更多相关文章

  1. 1014: [JSOI2008]火星人prefix - BZOJ

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  2. BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6243  Solved: 2007[Submit] ...

  3. BZOJ 1014: [JSOI2008]火星人prefix Splay+二分

    1014: [JSOI2008]火星人prefix 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1014 Description 火星人 ...

  4. bzoj 1014: [JSOI2008]火星人prefix hash && splay

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3154  Solved: 948[Submit][ ...

  5. 求帮看!!!!BZOJ 1014 [JSOI2008]火星人prefix

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4164  Solved: 1277[Submit] ...

  6. BZOJ 1014: [JSOI2008]火星人prefix( splay + hash )

    用splay维护序列, 二分+hash来判断LCQ.. #include<bits/stdc++.h> using namespace std; typedef unsigned long ...

  7. BZOJ 1014 [JSOI2008]火星人prefix (Splay + Hash + 二分)

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 8112  Solved: 2569[Submit] ...

  8. 1014: [JSOI2008]火星人prefix

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MB Description 火星人最近研究了一种操作:求一个字串两个后缀 ...

  9. bzoj千题计划106:bzoj1014 [JSOI2008]火星人prefix

    http://www.lydsy.com/JudgeOnline/problem.php?id=1014 两个后缀的最长公共前缀:二分+hash 带修改带插入:splay维护 #include< ...

随机推荐

  1. C# 利用WMI对象获取物理内存和可用内存大小

    下面的代码演示的是使用WMI对象可获取取物理内存和可用内存大小,在使用WMI对象前,先要添加对System.Management的引用,然后就可以调用WMI对象,代码如下: //获取总物理内存大小 M ...

  2. js阻止冒泡事件和默认事件的方法

    阻止默认事件 function stopDeFault(e){ if(e&&e.preventDefault){//非IE e.preventDefault(); }else{//IE ...

  3. [洛谷P4329][COCI2006-2007#1] Bond

    题目大意:有$n$个人有$n$个任务,每个人执行每个任务有不同的成功率,每个人只能执行一个任务,求所有任务都执行的总的成功率. 题解:可以跑最大费用最大流,把成功率取个$log$,最后$exp$回去就 ...

  4. JSONP以及Spring对象MappingJacksonValue的使用方式

    什么是JSONP?,以及Spring对象MappingJacksonValue的使用方式 原文: https://blog.csdn.net/weixin_38111957/article/detai ...

  5. 小程序根据input输入,动态设置按钮的样式

    [需求]实现当手机号已填写和协议已勾选时,“立即登录”按钮变亮,按钮可点击:若有一个不满足,按钮置灰,不可点击:实现获取短信验证码,倒计时提示操作:对不满足要求内容进行toast弹窗提示. <v ...

  6. windows主机控制

    一.开关机控制 using System.Runtime.InteropServices; //注销.关机.重启 class shutdown { [StructLayout(LayoutKind.S ...

  7. 【Foreign】无聊的计算姬 [Lucas][BSGS]

    无聊的计算姬 Time Limit: 10 Sec  Memory Limit: 256 MB Description Input Output Sample Input 6 2 2 3 4 3 2 ...

  8. 【洛谷 P4886】 快递员 (点分治)

    这题因为一些小细节还是\(debug\)了很久...不过我第一次用脚本对拍,不亏. 先随便找一个点作为根,算出答案,即所有点对到这个点的距离和的最大值,并记录所有距离最大的点对.如果这个点在任意一个距 ...

  9. Java任务调度框架----kunka

    初衷 工作中用到了很多框架,但是给我印象最深的还是我们PO(Product Owner)在若干年前写的一套任务调度框架,在JDK1.4之前,concurrent包还没有引入, 手写的这套Token调度 ...

  10. python函数篇:名称空间、作用域和函数的嵌套

    一.名称空间:(有3类) (1)内置名称空间(全局作用域) (2)全局名称空间(全局作用域) (3)局部名称空间(局部作用域) 关于名称空间的查询: x=1 def func(): print('fr ...