Tree 【HDU - 4757】【可持久化字典树】

题目链接


  给你一棵N个点的树,每个点都有权值,然后给N-1条树上的边,之后有Q次询问,问u到v的这条链上的点,和给出权值z异或的最大值。

  那么,就是可持久化字典树,然后再加上一个LCA即可求解该问题了,多组输入。利用树上差分,求链信息。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
//#include 
//#include 
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define eps 1e-8
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(x, y) make_pair(x, y)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e5 + 7;
int N, Q, head[maxN], cnt, root[maxN], tot, a[maxN];
struct Eddge
{
    int nex, to;
    Eddge(int a=-1, int b=0):nex(a), to(b) {}
} edge[maxN << 1];
inline void addEddge(int u, int v)
{
    edge[cnt] = Eddge(head[u], v);
    head[u] = cnt++;
}
inline void _add(int u, int v) { addEddge(u, v); addEddge(v, u); }
struct Trie
{
    int nex[2], s;
    Trie() { nex[0] = nex[1] = 0; s = 0; }
    inline void clear() { nex[0] = nex[1] = 0; s = 0; }
} t[maxN * 20];
void Insert(int u, int old, int val)
{
    root[u] = ++tot;
    int rt = root[u];
    for(int i=15, id; i>=0; i--)
    {
        id = (val >> i) & 1;
        t[rt] = t[old];
        t[rt].s ++;
        t[++tot].clear();
        t[rt].nex[id] = tot;
        old = t[old].nex[id];
        rt = tot;
    }
    t[rt] = t[old];
    t[rt].s ++;
}
inline int query(int Lrt, int Rrt, int val)
{
    int ans = 0;
    for(int i=15, id; i>=0; i--)
    {
        id = (val >> i) & 1;
        if(t[t[Rrt].nex[id ^ 1]].s > t[t[Lrt].nex[id ^ 1]].s)
        {
            ans |= 1 << i;
            Rrt = t[Rrt].nex[id ^ 1];
            Lrt = t[Lrt].nex[id ^ 1];
        }
        else
        {
            Rrt = t[Rrt].nex[id];
            Lrt = t[Lrt].nex[id];
        }
    }
    return ans;
}
int fa[maxN][20], LOG_2[maxN], deep[maxN];
void dfs(int u, int p)
{
    fa[u][0] = p; deep[u] = deep[p] + 1;
    for(int i=0; (1 << (i + 1)) < N; i++) fa[u][i + 1] = fa[fa[u][i]][i];
    Insert(u, root[p], a[u]);
    for(int i=head[u], v; ~i; i=edge[i].nex)
    {
        v = edge[i].to;
        if(v == p) continue;
        dfs(v, u);
    }
}
inline int _LCA(int u, int v)
{
    if(deep[u] < deep[v]) swap(u, v);
    int det = deep[u] - deep[v];
    for(int i=LOG_2[det]; i>=0; i--)
    {
        if((det >> i) & 1) u = fa[u][i];
    }
    if(u == v) return u;
    for(int i=LOG_2[deep[u]]; i>=0; i--)
    {
        if(fa[u][i] ^ fa[v][i])
        {
            u = fa[u][i];
            v = fa[v][i];
        }
    }
    return fa[u][0];
}
inline void init()
{
    cnt = tot = 0;
    for(int i=1; i<=N; i++) head[i] = -1;
    for(int i = 1, j = 2, k = 0; i <= N; i++)
    {
        if(i == j) { j <<= 1; k++; }
        LOG_2[i] = k;
    }
}
int main()
{
    while(scanf("%d%d", &N, &Q) != EOF)
    {
        init();
        for(int i=1; i<=N; i++) scanf("%d", &a[i]);
        for(int i=1, u, v; i

 

你可能感兴趣的:(可持久化字典树)