splay维护哈希值即可。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstdlib>
  5. #include<cstring>
  6. #include<algorithm>
  7. using namespace std;
  8. #define ll long long
  9. #define ull unsigned long long
  10. #define N 100010
  11. #define P 509
  12. #define lson tree[k].ch[0]
  13. #define rson tree[k].ch[1]
  14. char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<''||c>'')) c=getchar();return c;}
  15. int gcd(int n,int m){return m==?n:gcd(m,n%m);}
  16. int read()
  17. {
  18. int x=,f=;char c=getchar();
  19. while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
  20. while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
  21. return x*f;
  22. }
  23. int m,cnt,root;
  24. ull p[N];
  25. char s[N];
  26. struct data{int ch[],fa,size,x;ull hash;
  27. }tree[N];
  28. void up(int k)
  29. {
  30. tree[k].size=tree[lson].size+tree[rson].size+;
  31. tree[k].hash=tree[lson].hash*p[tree[rson].size+]+tree[k].x*p[tree[rson].size]+tree[rson].hash;
  32. }
  33. void build(int &k,int l,int r)
  34. {
  35. if (l>r) return;
  36. int mid=l+r>>;
  37. k=++cnt,tree[k].x=s[mid]-'a';
  38. build(lson,l,mid-),build(rson,mid+,r);
  39. tree[lson].fa=tree[rson].fa=k;
  40. up(k);
  41. }
  42. int whichson(int k){return tree[tree[k].fa].ch[]==k;}
  43. void move(int k)
  44. {
  45. int fa=tree[k].fa,gf=tree[fa].fa,p=whichson(k);
  46. if (fa) tree[gf].ch[whichson(fa)]=k;tree[k].fa=gf;
  47. tree[tree[k].ch[!p]].fa=fa,tree[fa].ch[p]=tree[k].ch[!p];
  48. tree[k].ch[!p]=fa,tree[fa].fa=k;
  49. up(fa),up(k);
  50. }
  51. void splay(int k,int rt)
  52. {
  53. while (tree[k].fa!=rt)
  54. {
  55. int fa=tree[k].fa;
  56. if (tree[fa].fa!=rt)
  57. if (whichson(k)^whichson(fa)) move(k);
  58. else move(fa);
  59. move(k);
  60. }
  61. if (!rt) root=k;
  62. }
  63. int find(int k,int x)
  64. {
  65. if (tree[lson].size+==x) return k;
  66. if (tree[lson].size>=x) return find(lson,x);
  67. else return find(rson,x-tree[lson].size-);
  68. }
  69. int split(int x,int y)
  70. {
  71. int p=find(root,x),q=find(root,y+);
  72. splay(p,);
  73. splay(q,p);
  74. return q;
  75. }
  76. ull gethash(int x,int y)
  77. {
  78. int p=split(x,y);
  79. return tree[tree[p].ch[]].hash;
  80. }
  81. void ins(int k,int x)
  82. {
  83. int p=split(k+,k);
  84. k=++cnt;tree[k].fa=p,tree[p].ch[]=k,tree[k].x=x;
  85. up(k),up(p),up(root);
  86. }
  87. void modify(int k,int x)
  88. {
  89. int p=split(k,k);
  90. tree[k=tree[p].ch[]].x=x;
  91. up(k),up(p),up(root);
  92. }
  93. int main()
  94. {
  95. #ifndef ONLINE_JUDGE
  96. freopen("bzoj1014.in","r",stdin);
  97. freopen("bzoj1014.out","w",stdout);
  98. const char LL[]="%I64d\n";
  99. #else
  100. const char LL[]="%lld\n";
  101. #endif
  102. scanf("%s",s+);int n=strlen(s+);
  103. p[]=;for (int i=;i<=;i++) p[i]=p[i-]*P;
  104. build(root,,n+);
  105. m=read();
  106. while (m--)
  107. {
  108. char c=getc();
  109. if (c=='Q')
  110. {
  111. int x=read(),y=read();
  112. int l=,r=n-max(x,y)+,ans=;
  113. while (l<=r)
  114. {
  115. int mid=l+r>>;
  116. if (gethash(x,x+mid-)==gethash(y,y+mid-)) ans=mid,l=mid+;
  117. else r=mid-;
  118. }
  119. printf("%d\n",ans);
  120. }
  121. if (c=='I')
  122. {
  123. n++;int x=read();char c=getc();
  124. ins(x,c-'a');
  125. }
  126. if (c=='R')
  127. {
  128. int x=read();char c=getc();
  129. modify(x,c-'a');
  130. }
  131. }
  132. return ;
  133. }

BZOJ1014 JSOI2008火星人(splay+哈希)的更多相关文章

  1. BZOJ1014:[JSOI2008]火星人(Splay,hash)

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

  2. [BZOJ1014][JSOI2008]火星人prefix

    [BZOJ1014][JSOI2008]火星人prefix 试题描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字 ...

  3. BZOJ1014 JSOI2008 火星人prefix 【非旋转Treap】*

    BZOJ1014 JSOI2008 火星人prefix Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符 ...

  4. 2018.06.28 BZOJ1014 [JSOI2008]火星人prefix(非旋treap+hash)

    [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MB Submit: 8951 Solved: 2860 Description 火星 ...

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

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

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

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

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

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

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

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

  9. bzoj1014: [JSOI2008]火星人prefix(splay+hash+二分)

    题目大意:一个字符串三个操作:①求两个后缀的LCP②插入一个字符③修改一个字符. 前几天刚学了hash+二分求lcp,就看到这题. 原来splay还能这么用?!原来splay模板这么好写?我以前写的s ...

随机推荐

  1. Android学习之六种事件响应方法汇总

    java源码如下: 1.MainActivity.java源码 package com.example.responsetest; import android.app.Activity; impor ...

  2. Android应用更新-自动检测版本及自动升级

    原文 http://www.cnblogs.com/keyindex/articles/1819504.html 注:实质,把自己新版的APK放在可以下载的地方,先自己设置个通信,检查版本,我是直接放 ...

  3. day88

    ContentType 场景需求:比方说我们现有两种商品,但是他们对应着一个价格策略表,为了防止数据库的浪费,我们在价格策略中加入一个表名字段,一个表明对应的id字段,这样的设计既优化了数据库,还不怕 ...

  4. flask多app和栈的应用

    一.简介     flask的蓝图可以实现url的分发,当有多个app时也可以利用app进行url分发,这里介绍下使用方式和内部原理以及栈的应用. 二.多app使用 使用示例 from werkzeu ...

  5. Spring Boot 进行Bean Validate和Method Validate

    SpringBoot在内部通过集成hibernate-validation 已经实现了JSR-349验证规范接口,在SpringBoot项目中只要直接使用就行了. 一般用在Controller中用于验 ...

  6. 分布式监控系统Zabbix--完整安装记录(7)-使用percona监控MySQL

    前面已经介绍了分布式监控系统Zabbix-3.0.3-完整安装记录(2)-添加mysql监控,但是没有提供可以直接使用的Key,太过简陋,监控效果不佳.要想更加仔细的监控Mysql,业内同学们都会选择 ...

  7. zabbix邮件报警功能的验证

    zabbix里面设置了很多监控项,有很多重要的监控预警,必须保证zabbix邮件报警功能正常,以确保那些告警信息能及时发送到运维人员的邮箱里. 所以需要每天8:30发一封确认zabbix邮件报警功能正 ...

  8. HTML5-indexedDB使用常见错误总结

    indexedDB使用过程中常常会出现以下错误: Failed to execute ‘createObjectStore’ on ‘IDBDatabase’: The database is not ...

  9. linux内核分析第六周学习笔记

    LINUX内核分析第六周学习总结 标签(空格分隔): 20135328陈都 陈都 原创作品转载请注明出处 <Linux内核分析>MOOC课程 http://mooc.study.163.c ...

  10. BF算法和KMP算法 python实现

    BF算法 def Index(s1,s2,pos = 0): """ BF算法 """ i = pos j = 0 while(i < ...