可持久化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 ;
}
上一篇:sybase SA密码重置


下一篇:elastic-search单机部署以及中文分词IKAnalyzer安装