数据结构
01trie
const int M = 30;
const int N = 2e5 + 5;
int n, a[N];
struct Trie {
int t[N * M][2], ed[N * M], dp[N * M], tot;
inline void clear(void) {
for (int i = 0; i <= tot; i++) t[i][0] = t[i][1] = ed[i] = dp[i] = 0;
tot = 0;
}
Trie(void) { clear(); }
inline void insert(int val) {
int pos = 0;
for (int i = M; i >= 0; i--) {
int b = val >> i & 1;
if (!t[pos][b]) t[pos][b] = ++tot;
pos = t[pos][b];
}
ed[pos]++;
}
inline void remove(int val) {
int pos = 0;
for (int i = M; i >= 0; i--) {
int b = val >> i & 1;
if (!t[pos][b]) break;
pos = t[pos][b];
}
if (ed[pos]) ed[pos]--;
}
inline int query_min(int val) {
int pos = 0, ret = 0;
for (int i = M; i >= 0; i--) {
int b = val >> i & 1;
if (t[pos][b]) pos = t[pos][b];
else if (t[pos][1 - b]) ret = ret | (1 << i), pos = t[pos][1 - b];
else break;
} return ret;
}
inline int query_max(int val) {
int pos = 0, ret = 0;
for (int i = M; i >= 0; i--) {
int b = val >> i & 1; b = 1 - b;
if (t[pos][b]) pos = t[pos][b];
else if (t[pos][1 - b]) ret = ret | (1 << i), pos = t[pos][1 - b];
else break;
} return ret;
}
inline void dfs(int pos) {
int l = t[pos][0], r = t[pos][1];
if (l) dfs(l), chkmax(dp[pos], dp[l]);
if (r) dfs(r), chkmax(dp[pos], dp[r]);
if (l && r) dp[pos]++;
if (ed[pos]) dp[pos] = 1;
}
} T;
标签:val,int,ed,pos,ret,板子,--,大全
From: https://www.cnblogs.com/EternalEpic/p/18430577