题意

题目链接

Sol

一眼splay + 二分hash,不过区间splay怎么写来着呀

试着写了两个小时发现死活不对

看了一下yyb的代码发现自己根本就不会splay。。。。

  1. // luogu-judger-enable-o2
  2. #include<bits/stdc++.h>
  3. #define ull unsigned long long
  4. using namespace std;
  5. const int MAXN = 1e6 + 10;
  6. const ull base = 27;
  7. inline int read() {
  8. char c = getchar(); int x = 0, f = 1;
  9. while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
  10. while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  11. return x * f;
  12. }
  13. int N, M;
  14. char s[MAXN];
  15. int root, tot, ch[MAXN][2], fa[MAXN], siz[MAXN];
  16. ull ha[MAXN], val[MAXN], po[MAXN];
  17. bool rev[MAXN];
  18. int ident(int x) {
  19. return ch[fa[x]][1] == x;
  20. }
  21. void connect(int x, int f, int id) {
  22. fa[x] = f; ch[f][id] = x;
  23. }
  24. void update(int x) {
  25. siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + 1;
  26. ha[x] = ha[ch[x][0]] + val[x] * po[siz[ch[x][0]]] + po[siz[ch[x][0]] + 1] * ha[ch[x][1]];
  27. }
  28. void rotate(int x) {
  29. int y = fa[x], R = fa[y], Yson = ident(x), Rson = ident(y);
  30. int B = ch[x][Yson ^ 1];
  31. connect(x, R, Rson); connect(B, y, Yson); connect(y, x, Yson ^ 1);
  32. update(y); update(x);
  33. }
  34. void splay(int x, int to) {
  35. //to = fa[to];
  36. while(fa[x] != to) {
  37. if(fa[fa[x]] == to) rotate(x);
  38. else if(ident(x) == ident(fa[x])) rotate(fa[x]), rotate(x);
  39. else rotate(x), rotate(x);
  40. }
  41. if(!to) root = x; update(x);
  42. }
  43. int find(int k) {
  44. int x = root;
  45. while(1) {
  46. if(siz[ch[x][0]] + 1 == k) return x;
  47. if(siz[ch[x][0]] + 1 < k) k -= siz[ch[x][0]] + 1, x = ch[x][1];//tag
  48. else x = ch[x][0];
  49. }
  50. }
  51. ull gethash(int l, int len) {
  52. int x = find(l), y = find(l + len + 1);
  53. splay(x, 0); splay(y, x);
  54. return ha[ch[y][0]];
  55. }
  56. int LCP(int y, int x) {
  57. int l = 0, r = tot - max(x, y) - 1, ans = 0;
  58. while(l <= r) {
  59. int mid = l + r >> 1;
  60. if(gethash(x, mid) == gethash(y, mid)) l = mid + 1, ans = mid;
  61. else r = mid - 1;
  62. }
  63. return ans;
  64. }
  65. void insert(int x, int v) {
  66. int l = find(x + 1), r = find(x + 2);
  67. splay(l, 0); splay(r, l);
  68. val[++tot] = v; fa[tot] = r; ch[r][0] = tot; splay(tot, 0);
  69. }
  70. void change(int x, int v) {
  71. int l = find(x), r = find(x + 2);
  72. splay(l, 0); splay(r, l);
  73. val[ch[r][0]] = v; update(ch[r][0]);
  74. update(r); update(l);
  75. }
  76. int main() {
  77. // freopen("a.in", "r", stdin);freopen("a.out", "w", stdout);
  78. po[0] = 1;
  79. for(int i = 1; i <= (int)1e5; i++) po[i] = base * po[i - 1];
  80. scanf("%s", s + 1); N = strlen(s + 1);
  81. ch[1][1] = 2; fa[2] = 1; tot = 2; root = 1; update(2); update(1);
  82. for(int i = 1; i <= N; i++)
  83. insert(i - 1, s[i]);
  84. int M = read();
  85. while(M--) {
  86. char opt[3]; scanf("%s", opt);
  87. if(opt[0] == 'Q') {int x = read(), y = read(); printf("%d\n", LCP(x, y));}
  88. else if(opt[0] == 'R') {
  89. int x = read(); scanf("%s", opt + 1);
  90. change(x, opt[1]);
  91. } else {
  92. int x = read(); scanf("%s", opt + 1);
  93. insert(x, opt[1]);
  94. }
  95. }
  96. return 0;
  97. }
  98. /*
  99. */

BZOJ1014: [JSOI2008]火星人prefix(splay 二分 hash)的更多相关文章

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

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

  2. 【BZOJ-1014】火星人prefix Splay + 二分 + Hash

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

  3. BZOJ1014[JSOI2008]火星人prefix(splay维护hash)

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

  4. [BZOJ1014] [JSOI2008] 火星人prefix (splay & 二分答案)

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

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

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

  6. bzoj1014: [JSOI2008]火星人prefix splay+hash+二分

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

  7. BZOJ 1014 [JSOI2008]火星人prefix (splay+二分答案+字符串hash)

    题目大意:维护一个字符串,支持插入字符和替换字符的操作,以及查询该字符串两个后缀的最长公共前缀长度 乍一看以为是后缀数组,然而并没有可持久化后缀数组(雾) 看题解才知道这是一道splay题,首先要对s ...

  8. bzoj1014: [JSOI2008]火星人prefix splay+hash

    我写的代码好像自古以来就是bzoj不友好型的 本地跑的比std快,但是交上去巧妙被卡 答案...应该是对的,拍了好久了 #include <bits/stdc++.h> #define M ...

  9. [bzoj1014](JSOI2008)火星人 prefix (Splay维护哈希)

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

随机推荐

  1. pickle 模块学习 常用方法

    内容提要: 1: pickle的主要作用 pickle主要用于python 于python 之间进行文件传出,网络传输 他同json 一样也是有4个函数 pickle.dumps(iterable)  ...

  2. Go语言管道

    Channel概念 Channel 是Go中的一个核心类型,你可以把它看成一个管道.Channel是引用类型,操作符是箭头 <- . Channel 是 CSP 模式的具体实现,用于多个 gor ...

  3. 洛谷 P2014 选课(树形背包)

    洛谷 P2014 选课(树形背包) 思路 题面:洛谷 P2014 如题这种有依赖性的任务可以用一棵树表示,因为一个儿子要访问到就必须先访问到父亲.然后,本来本题所有树是森林(没有共同祖先),但是题中的 ...

  4. 毫秒查询9位数qq号码是否存在-BitMap算法应用

    实现详情请查看博客园 https://www.cnblogs.com/caoke/p/10793885.html 随机注册10万个放入BitMap,然后查询qq号码是否已存在,算法复杂度O(1). / ...

  5. Windows7上用VS编译本地使用的live555

    本文链接:https://www.jianshu.com/p/6ea100865744 环境 系统:Windows7 SP1 64位 编辑器:Visual Studio Community 2017 ...

  6. c# SocketAsyncEventArgs类的使用 IOCP服务器

    要编写高性能的Socket服务器,为每个接收的Socket分配独立的处理线程的做法是不可取的,当连接数量很庞大时,服务器根本无法应付.要响应庞大的连接数量,需要使用IOCP(完成端口)来撤换并处理响应 ...

  7. IIS7如何实现访问HTTP跳转到HTTPS访问 转的

    加几句,1.安装url重写模块,不需要重启IIS,安装完了就能用.个人感觉比 IIS REWRITE组件更好用,iis rewrite是安装第三方的那种,不缴费只可以把所有规则写在一起,不能区别站点, ...

  8. Mac下Go2Shell配合ITerm2无法定位到当前文件夹目录的解决方法

    下载最新版,这个问题在最新版已经完美解决. http://zipzapmac.com/go2shell

  9. 判断checkbox是否被选中

    jquery判断checked的三种方法: .attr('checked):   //看版本1.6+返回:”checked”或”undefined” ;1.5-返回:true或false .prop( ...

  10. 用maven来创建scala和java项目代码环境(图文详解)(Intellij IDEA(Ultimate版本)、Intellij IDEA(Community版本)和Scala IDEA for Eclipse皆适用)(博主推荐)

    不多说,直接上干货! 为什么要写这篇博客? 首先,对于spark项目,强烈建议搭建,用Intellij IDEA(Ultimate版本),如果你还有另所爱好尝试Scala IDEA for Eclip ...