可持久化trie学习笔记
其实很早之前就想学习可持久化trie,不过由于换队友等情况,还是优先去学数论和计算几何,今天突然心血来潮学了一发可持久化trie,感觉还是蛮简单的,不过由于自己很长时间没写过可持久化了,都快忘了是个什么套路了。
具体来说,就是每次插入一个数,我们先判断这一位是0还是1,然后把以前节点里面和这一位相反的那个子节点接到我们现在要插入的新节点上面去,然后呢,你就可以直接递归的构造之后的子树。并且我们每到新的一层,都会记录这个节点所代表的的位数在当前这个位置已经出现了多少次,那么我们查询的时候,就能判断一个区间里面是否有这一位了,理解之后感觉随便写写就能出来,记个代码。
- //author Eterna
- #define Hello the_cruel_world!
- #pragma GCC optimize(2)
- #include<iostream>
- #include<algorithm>
- #include<cstdio>
- #include<string>
- #include<cstring>
- #include<vector>
- #include<map>
- #include<set>
- #include<queue>
- #include<stack>
- #include<utility>
- #include<cmath>
- #include<climits>
- #include<deque>
- #include<functional>
- #include<complex>
- #include<numeric>
- #include<unordered_map>
- #define max(x,y) ((x)>(y)?(x):(y))
- #define min(x,y) ((x)<(y)?(x):(y))
- #define Pi acos(-1.0)
- #define ABS(x) ((x) >= 0 ? (x) : (-(x)))
- #define pb(x) push_back(x)
- #define lowbit(x) (x & -x)
- #define FRIN freopen("C:\\Users\\Administrator.MACHENI-KA32LTP\\Desktop\\in.txt", "r", stdin)
- #define FROUT freopen("C:\\Users\\Administrator.MACHENI-KA32LTP\\Desktop\\out.txt", "w", stdout)
- #define FAST ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
- #define outd(x) printf("%d\n", x)
- #define outld(x) printf("%lld\n", x)
- #define il inline
- #define ls(x) arr[x].child[0]
- #define rs(x) arr[x].child[1]
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef pair<int, int> pii;
- const int maxn = 6e5;
- const int INF = 0x7fffffff;
- const int mod = 1e9 + ;
- const double eps = 1e-;
- inline int read_int() {
- char c;
- int ret = , sgn = ;
- do { c = getchar(); } while ((c < '' || c > '') && c != '-');
- if (c == '-') sgn = -; else ret = c - '';
- while ((c = getchar()) >= '' && c <= '') ret = ret * + (c - '');
- return sgn * ret;
- }
- inline ll read_ll() {
- char c;
- ll ret = , sgn = ;
- do { c = getchar(); } while ((c < '' || c > '') && c != '-');
- if (c == '-') sgn = -; else ret = c - '';
- while ((c = getchar()) >= '' && c <= '') ret = ret * + (c - '');
- return sgn * ret;
- }
- struct node {
- int child[], cnt;
- }arr[ * maxn + ];
- int tot, coe[], root[maxn + ];
- void Insert(int& now, int pre, int v, int cnt = ) {
- if (!now)now = ++tot;
- arr[now].cnt = arr[pre].cnt + ;
- if (cnt < )return;
- int p = (v >> cnt) & ;
- arr[now].child[p ^ ] = arr[pre].child[p ^ ];
- Insert(arr[now].child[p], arr[pre].child[p], v, cnt - );
- }
- int Query(int l, int r, int v, int cnt = ) {
- if (cnt < )return ;
- int p = (v >> cnt) & ;
- int sum = arr[arr[r].child[p ^ ]].cnt - arr[arr[l].child[p ^ ]].cnt;
- if (sum > )return coe[cnt] + Query(arr[l].child[p ^ ], arr[r].child[p ^ ], v, cnt - );
- else return Query(arr[l].child[p], arr[r].child[p], v, cnt - );
- }
- int n, m, a[maxn + ];
- char op;
- int main()
- {
- coe[] = ;
- for (int i = ; i < ; ++i)coe[i] = coe[i - ] * ;
- n = read_int() + , m = read_int();
- Insert(root[], root[], );
- for (int i = ; i < n; ++i) {
- a[i] = read_int();
- a[i] ^= a[i - ];
- Insert(root[i + ], root[i], a[i]);
- }
- while (m--) {
- scanf(" %c", &op);
- if (op == 'A') {
- a[n] = a[n - ];
- a[n++] ^= read_int();
- Insert(root[n], root[n - ], a[n - ]);
- }
- else {
- int l = read_int(), r = read_int(), x = read_int();
- printf("%d\n", Query(root[l - ], root[r], a[n - ] ^ x));
- }
- }
- //system("pause");
- return ;
- }
可持久化trie学习笔记的更多相关文章
- 可持久化trie 学习总结
QAQ 以前一直觉得可持久化trie很难,今天强行写了一发觉得还是蛮简单的嘛 自己的模板是自己手写的,写了几道题目并没有出过错误 THUSC的第二题的解法五貌似就是可持久化trie,时间复杂度O(60 ...
- 可持久化fhq-treap学习笔记
目录 可持久化fhq-treap----- 支持查询历史版本的非旋treap 先看看为啥他可以可持久化 过程 别的 注意&&出错&&吐槽 模板->luoguP38 ...
- Trie学习笔记
Trie(字典树) 基本数据结构 实际是:对于每个字符串组的每一个不同前缀建立节点 基本代码 void Insert(char *s,int p){ int now=0; int l=strlen(s ...
- 字典树(Trie)学习笔记
目录 什么是字典树 如何存储字典树 如何查找字符串有没有出现 第一个图的那种线段树 应用 例题 1.统计难题 2.P2580 于是他错误的点名开始了 什么是字典树 上图来自luogu题解 这是一种字典 ...
- [学习笔记]可持久化数据结构——数组、并查集、平衡树、Trie树
可持久化:支持查询历史版本和在历史版本上修改 可持久化数组 主席树做即可. [模板]可持久化数组(可持久化线段树/平衡树) 可持久化并查集 可持久化并查集 主席树做即可. 要按秩合并.(路径压缩每次建 ...
- [原创]java WEB学习笔记77:Hibernate学习之路---Hibernate 版本 helloword 与 解析,.环境搭建,hibernate.cfg.xml文件及参数说明,持久化类,对象-关系映射文件.hbm.xml,Hibernate API (Configuration 类,SessionFactory 接口,Session 接口,Transaction(事务))
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- AC自动机学习笔记-2(Trie图&&last优化)
我是连月更都做不到的蒟蒻博主QwQ 考虑到我太菜了,考完noip就要退役了,所以我决定还是把博客的倒数第二篇博客给写了,也算是填了一个坑吧.(最后一篇?当然是悲怆のnoip退役记啦QAQ) 所以我们今 ...
- Android:日常学习笔记(9)———探究持久化技术
Android:日常学习笔记(9)———探究持久化技术 引入持久化技术 什么是持久化技术 持久化技术就是指将那些内存中的瞬时数据保存到存储设备中,保证即使在手机或电脑关机的情况下,这些数据仍然不会丢失 ...
- tensorflow学习笔记——模型持久化的原理,将CKPT转为pb文件,使用pb模型预测
由题目就可以看出,本节内容分为三部分,第一部分就是如何将训练好的模型持久化,并学习模型持久化的原理,第二部分就是如何将CKPT转化为pb文件,第三部分就是如何使用pb模型进行预测. 一,模型持久化 为 ...
随机推荐
- Android : Camera之CHI API
一.CAM CHI API功能介绍: CHI API建立在Google HAL3的灵活性基础之上,目的是将Camera2/HAL3接口分离出来用于使用相机功能,它是一个灵活的图像处理驱动程序(摄像头硬 ...
- Python函数的一点用法
#python的基本语法网上已经有很多详细的解释了,写在这里方便自己记忆一些 BIF是python内置的函数,任何一门语言都能用来创造函数,python也不例外 1.创建一个函数 def func() ...
- ecplise包的层次结构选择
ecplise包的层次结构选择 平坦方式: 分层方式:
- Problem 6: Sum square difference
The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385 The square of ...
- SSE 向量乘矩阵
struct Vector4 { float x, y, z, w; }; struct Matrix { ][]; }; void SSE_VectorMultiplyMatrix(const Ve ...
- 关于Unity3D使用时Scene视图清楚,Game视图不清楚的问题
1.自己不知道什么时候,将LowResolutioinAspectRatios给勾上了, 2.同样的Scale值大于1的时候也会造成模糊,但这个好像比1好发现一点
- 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 ...
- .Net Core+Angular6 学习 第三部分(从api获取data)
. 现在开始需要集成angular6到VS项目中 1.1 打开Startup.cs文件, 在ConfigureServices方法中配置angular files的目录. 1.2 在Configure ...
- 汉诺塔III
题目描述: 约19世纪末,在欧州的商店中出售一种智力玩具,在一块铜板上有三根杆,最左边的杆上自上而下.由小到大顺序串着由64个圆盘构成的塔.目的是将最左边杆上的盘全部移到右边的杆上,条件是一次只能移动 ...
- 基于Java Instrument的Agent实现
使用 Instrumentation,使得开发者可以构建一个独立于应用程序的代理程序(Agent),用来监测和协助运行在 JVM 上的程序,甚至能够替换和修改某些类的定义.有了这样的功能,开发者就可以 ...