其实很早之前就想学习可持久化trie,不过由于换队友等情况,还是优先去学数论和计算几何,今天突然心血来潮学了一发可持久化trie,感觉还是蛮简单的,不过由于自己很长时间没写过可持久化了,都快忘了是个什么套路了。

具体来说,就是每次插入一个数,我们先判断这一位是0还是1,然后把以前节点里面和这一位相反的那个子节点接到我们现在要插入的新节点上面去,然后呢,你就可以直接递归的构造之后的子树。并且我们每到新的一层,都会记录这个节点所代表的的位数在当前这个位置已经出现了多少次,那么我们查询的时候,就能判断一个区间里面是否有这一位了,理解之后感觉随便写写就能出来,记个代码。

  1. //author Eterna
  2. #define Hello the_cruel_world!
  3. #pragma GCC optimize(2)
  4. #include<iostream>
  5. #include<algorithm>
  6. #include<cstdio>
  7. #include<string>
  8. #include<cstring>
  9. #include<vector>
  10. #include<map>
  11. #include<set>
  12. #include<queue>
  13. #include<stack>
  14. #include<utility>
  15. #include<cmath>
  16. #include<climits>
  17. #include<deque>
  18. #include<functional>
  19. #include<complex>
  20. #include<numeric>
  21. #include<unordered_map>
  22. #define max(x,y) ((x)>(y)?(x):(y))
  23. #define min(x,y) ((x)<(y)?(x):(y))
  24. #define Pi acos(-1.0)
  25. #define ABS(x) ((x) >= 0 ? (x) : (-(x)))
  26. #define pb(x) push_back(x)
  27. #define lowbit(x) (x & -x)
  28. #define FRIN freopen("C:\\Users\\Administrator.MACHENI-KA32LTP\\Desktop\\in.txt", "r", stdin)
  29. #define FROUT freopen("C:\\Users\\Administrator.MACHENI-KA32LTP\\Desktop\\out.txt", "w", stdout)
  30. #define FAST ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  31. #define outd(x) printf("%d\n", x)
  32. #define outld(x) printf("%lld\n", x)
  33. #define il inline
  34. #define ls(x) arr[x].child[0]
  35. #define rs(x) arr[x].child[1]
  36. using namespace std;
  37. typedef long long ll;
  38. typedef unsigned long long ull;
  39. typedef pair<int, int> pii;
  40. const int maxn = 6e5;
  41. const int INF = 0x7fffffff;
  42. const int mod = 1e9 + ;
  43. const double eps = 1e-;
  44. inline int read_int() {
  45. char c;
  46. int ret = , sgn = ;
  47. do { c = getchar(); } while ((c < '' || c > '') && c != '-');
  48. if (c == '-') sgn = -; else ret = c - '';
  49. while ((c = getchar()) >= '' && c <= '') ret = ret * + (c - '');
  50. return sgn * ret;
  51. }
  52. inline ll read_ll() {
  53. char c;
  54. ll ret = , sgn = ;
  55. do { c = getchar(); } while ((c < '' || c > '') && c != '-');
  56. if (c == '-') sgn = -; else ret = c - '';
  57. while ((c = getchar()) >= '' && c <= '') ret = ret * + (c - '');
  58. return sgn * ret;
  59. }
  60. struct node {
  61. int child[], cnt;
  62. }arr[ * maxn + ];
  63. int tot, coe[], root[maxn + ];
  64. void Insert(int& now, int pre, int v, int cnt = ) {
  65. if (!now)now = ++tot;
  66. arr[now].cnt = arr[pre].cnt + ;
  67. if (cnt < )return;
  68. int p = (v >> cnt) & ;
  69. arr[now].child[p ^ ] = arr[pre].child[p ^ ];
  70. Insert(arr[now].child[p], arr[pre].child[p], v, cnt - );
  71. }
  72. int Query(int l, int r, int v, int cnt = ) {
  73. if (cnt < )return ;
  74. int p = (v >> cnt) & ;
  75. int sum = arr[arr[r].child[p ^ ]].cnt - arr[arr[l].child[p ^ ]].cnt;
  76. if (sum > )return coe[cnt] + Query(arr[l].child[p ^ ], arr[r].child[p ^ ], v, cnt - );
  77. else return Query(arr[l].child[p], arr[r].child[p], v, cnt - );
  78. }
  79. int n, m, a[maxn + ];
  80. char op;
  81. int main()
  82. {
  83. coe[] = ;
  84. for (int i = ; i < ; ++i)coe[i] = coe[i - ] * ;
  85. n = read_int() + , m = read_int();
  86. Insert(root[], root[], );
  87. for (int i = ; i < n; ++i) {
  88. a[i] = read_int();
  89. a[i] ^= a[i - ];
  90. Insert(root[i + ], root[i], a[i]);
  91. }
  92. while (m--) {
  93. scanf(" %c", &op);
  94. if (op == 'A') {
  95. a[n] = a[n - ];
  96. a[n++] ^= read_int();
  97. Insert(root[n], root[n - ], a[n - ]);
  98. }
  99. else {
  100. int l = read_int(), r = read_int(), x = read_int();
  101. printf("%d\n", Query(root[l - ], root[r], a[n - ] ^ x));
  102. }
  103. }
  104. //system("pause");
  105. return ;
  106. }

可持久化trie学习笔记的更多相关文章

  1. 可持久化trie 学习总结

    QAQ 以前一直觉得可持久化trie很难,今天强行写了一发觉得还是蛮简单的嘛 自己的模板是自己手写的,写了几道题目并没有出过错误 THUSC的第二题的解法五貌似就是可持久化trie,时间复杂度O(60 ...

  2. 可持久化fhq-treap学习笔记

    目录 可持久化fhq-treap----- 支持查询历史版本的非旋treap 先看看为啥他可以可持久化 过程 别的 注意&&出错&&吐槽 模板->luoguP38 ...

  3. Trie学习笔记

    Trie(字典树) 基本数据结构 实际是:对于每个字符串组的每一个不同前缀建立节点 基本代码 void Insert(char *s,int p){ int now=0; int l=strlen(s ...

  4. 字典树(Trie)学习笔记

    目录 什么是字典树 如何存储字典树 如何查找字符串有没有出现 第一个图的那种线段树 应用 例题 1.统计难题 2.P2580 于是他错误的点名开始了 什么是字典树 上图来自luogu题解 这是一种字典 ...

  5. [学习笔记]可持久化数据结构——数组、并查集、平衡树、Trie树

    可持久化:支持查询历史版本和在历史版本上修改 可持久化数组 主席树做即可. [模板]可持久化数组(可持久化线段树/平衡树) 可持久化并查集 可持久化并查集 主席树做即可. 要按秩合并.(路径压缩每次建 ...

  6. [原创]java WEB学习笔记77:Hibernate学习之路---Hibernate 版本 helloword 与 解析,.环境搭建,hibernate.cfg.xml文件及参数说明,持久化类,对象-关系映射文件.hbm.xml,Hibernate API (Configuration 类,SessionFactory 接口,Session 接口,Transaction(事务))

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  7. AC自动机学习笔记-2(Trie图&&last优化)

    我是连月更都做不到的蒟蒻博主QwQ 考虑到我太菜了,考完noip就要退役了,所以我决定还是把博客的倒数第二篇博客给写了,也算是填了一个坑吧.(最后一篇?当然是悲怆のnoip退役记啦QAQ) 所以我们今 ...

  8. Android:日常学习笔记(9)———探究持久化技术

    Android:日常学习笔记(9)———探究持久化技术 引入持久化技术 什么是持久化技术 持久化技术就是指将那些内存中的瞬时数据保存到存储设备中,保证即使在手机或电脑关机的情况下,这些数据仍然不会丢失 ...

  9. tensorflow学习笔记——模型持久化的原理,将CKPT转为pb文件,使用pb模型预测

    由题目就可以看出,本节内容分为三部分,第一部分就是如何将训练好的模型持久化,并学习模型持久化的原理,第二部分就是如何将CKPT转化为pb文件,第三部分就是如何使用pb模型进行预测. 一,模型持久化 为 ...

随机推荐

  1. Android : Camera之CHI API

    一.CAM CHI API功能介绍: CHI API建立在Google HAL3的灵活性基础之上,目的是将Camera2/HAL3接口分离出来用于使用相机功能,它是一个灵活的图像处理驱动程序(摄像头硬 ...

  2. Python函数的一点用法

    #python的基本语法网上已经有很多详细的解释了,写在这里方便自己记忆一些 BIF是python内置的函数,任何一门语言都能用来创造函数,python也不例外 1.创建一个函数 def func() ...

  3. ecplise包的层次结构选择

    ecplise包的层次结构选择 平坦方式: 分层方式:

  4. Problem 6: Sum square difference

    The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385 The square of ...

  5. SSE 向量乘矩阵

    struct Vector4 { float x, y, z, w; }; struct Matrix { ][]; }; void SSE_VectorMultiplyMatrix(const Ve ...

  6. 关于Unity3D使用时Scene视图清楚,Game视图不清楚的问题

    1.自己不知道什么时候,将LowResolutioinAspectRatios给勾上了, 2.同样的Scale值大于1的时候也会造成模糊,但这个好像比1好发现一点

  7. L364 Should Your Resume Be One Page or Two?

    Should Your Resume Be One Page or Two? Conventional wisdom suggests that you should keep it short: A ...

  8. .Net Core+Angular6 学习 第三部分(从api获取data)

    . 现在开始需要集成angular6到VS项目中 1.1 打开Startup.cs文件, 在ConfigureServices方法中配置angular files的目录. 1.2 在Configure ...

  9. 汉诺塔III

    题目描述: 约19世纪末,在欧州的商店中出售一种智力玩具,在一块铜板上有三根杆,最左边的杆上自上而下.由小到大顺序串着由64个圆盘构成的塔.目的是将最左边杆上的盘全部移到右边的杆上,条件是一次只能移动 ...

  10. 基于Java Instrument的Agent实现

    使用 Instrumentation,使得开发者可以构建一个独立于应用程序的代理程序(Agent),用来监测和协助运行在 JVM 上的程序,甚至能够替换和修改某些类的定义.有了这样的功能,开发者就可以 ...