HDU - 6191 Query on A Tree 可持久化Tire

本文介绍了一道算法题目,通过使用字典树来解决树形结构中节点权值与给定数值的最大异或值问题。文章详细阐述了解题思路,并提供了具体的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Monkey A lives on a tree, he always plays on this tree.

One day, monkey A learned about one of the bit-operations, xor. He was keen of this interesting operation and wanted to practise it at once.

Monkey A gave a value to each node on the tree. And he was curious about a problem.

The problem is how large the xor result of number x and one node value of label y can be, when giving you a non-negative integer x and a node label u indicates that node y is in the subtree whose root is u(y can be equal to u).

Can you help him?
Input
There are no more than 6 test cases.

For each test case there are two positive integers n and q, indicate that the tree has n nodes and you need to answer q queries.

Then two lines follow.

The first line contains n non-negative integers V1,V2,⋯,VnV1,V2,⋯,Vn, indicating the value of node i.

The second line contains n-1 non-negative integers F1,F2,⋯Fn−1F1,F2,⋯Fn−1, FiFi means the father of node i+1i+1.

And then q lines follow.

In the i-th line, there are two integers u and x, indicating that the node you pick should be in the subtree of u, and x has been described in the problem.

2≤n,q≤1052≤n,q≤105

0≤Vi≤1090≤Vi≤109

1≤Fi≤n1≤Fi≤n, the root of the tree is node 1.

1≤u≤n,0≤x≤1091≤u≤n,0≤x≤109
Output
For each query, just print an integer in a line indicating the largest result.
Sample Input
2 2
1 2
1
1 3
2 1
Sample Output
2
3

题意就是说给你一棵树,树上每个节点有一个权值。然后有q次查询,每次查询给你节点标号u和一个数x。问你以u的子树里面的所有节点点权和数x的最大异或值是多少。

思路:求最大异或值显然是用字典树QWQemmm这是个题型2333
这题我一开始的感觉是个离线,先全部存下来,然后每次查询到这个节点暴力去构造出属于这个节点的字典树然后去里面把所有关于这个节点的查询全部做出来。。。但是这个暴力构造的过程我一开始没想到。。大佬重现赛的时候想了个dfs序加莫队。大佬还是大佬。题解给了dsu on tree启发式合并。其实思路都是一样的,每次暴力增删去构造当前子树的字典树。那么我那个时候看题的时候。。。对每个节点构造一棵属于他自己的字典树,那不是字典树森林么。。。线段树森林有主席树,那字典树显然也可以=-=。。然后就把可持久化字典树YY出来了。。。结果还真有这玩意23333万物皆可可持久化。字典树的可持久化写法其实跟主席树差不多。因为两个都是二叉树,只不过主席树是满二叉树,字典树不是满二叉树。但是做法一样,每次去暴力构造一条链。2333还是蛮简单的=-=虽然不知道为什么第一次写的时候T了。。。很绝望
说真的。。好像没有什么题是重写第二遍还错的,要是还错那就再写一遍2333
代码:

#include<bits/stdc++.h>
using namespace std;
//thanks to pyf ...
//thanks to qhl ...

const int N = 1e5 + 7;

struct Tire
{
    int next[2];
    int val;
} t[N * 32 * 2];
struct Edge
{
    int u, v, next;
} edge[N * 2];
int head[N];
int cnt = 0, tot = 0, Index = 0;
int key[N], L[N], R[N];
int root[N];
void init()
{
    memset(head, -1, sizeof(head));
    cnt = 0, tot = 0, Index = 0;
}
void init_Node(int id)
{
    t[id].next[0] = t[id].next[1] = t[id].val = 0;
}
void add_edge(int u, int v)
{
    edge[tot] = {u, v, head[u]};
    head[u] = tot ++;
}
void update(int x, int y, int k)
{
    int b[31];
    for (int i = 0; i <= 30; i++)
    {
        b[i] = k & 1;
        k >>= 1;
    }
    root[x] = ++cnt;
    init_Node(cnt);
    int rx = root[x], ry = root[y];
    for (int i = 30; i >= 0; i--)
    {
        t[rx].next[b[i]] = ++cnt;
        init_Node(t[rx].next[b[i]]);
        t[rx].next[(b[i] ^ 1)] = t[ry].next[(b[i] ^ 1)];
        t[rx].val = t[ry].val + 1;
        rx = t[rx].next[b[i]] , ry = t[ry].next[b[i]];
    }
    t[rx].val = t[ry].val + 1;
    // rx = root[x], ry = root[y];
    // for (int i = 30; i >= 0; i--)
    // {
    //  if (t[t[rx].next[0]].val > t[t[ry].next[0]].val)
    //  {
    //      cout << 0;
    //      rx = t[rx].next[0], ry = t[ry].next[0];
    //  }
    //  else
    //  {
    //      cout << 1;
    //      rx = t[rx].next[1], ry = t[ry].next[1];
    //  }
    // }
    // cout << endl;
}
int query(int l, int r, int val)
{
    int b[31];
    for (int i = 0; i <= 30; i++)
    {
        b[i] = val & 1;
        b[i]  ^= 1, val >>= 1;
    }
    int ans = 0;
    for (int i = 30; i >= 0; i--)
    {
        if (t[t[r].next[b[i]]].val > t[t[l].next[b[i]]].val)
        {
            ans += (b[i]) * (1 << i);
            l = t[l].next[b[i]], r = t[r].next[b[i]];
        }
        else
        {
            ans += (b[i] ^ 1) * (1 << i);
            l = t[l].next[b[i] ^ 1], r = t[r].next[b[i] ^ 1];
        }
    }
    return ans;
}
void dfs(int u)
{
    L[u] = ++ Index;
    for (int i = head[u]; ~i; i = edge[i].next)
    {
        int v = edge[i].v;
        dfs(v);
    }
    R[u] = Index;
}
int pos[N];
int main()
{
    int n, q;
    while (scanf("%d%d", &n, &q) == 2)
    {
        init();
        for (int i = 1; i <= n; i++)
            scanf("%d", key + i);
        for (int i = 2; i <= n; i++)
        {
            int f;
            scanf("%d", &f);
            add_edge(f, i);
        }
        dfs(1);
        for (int i = 1; i <= n; i++)
            pos[L[i]] = i;
        for (int i = 1; i <= n; i++)
            update(i, i - 1, key[pos[i]]);
        for (int i = 0; i < q; i++)
        {
            int u, x;
            scanf("%d%d", &u, &x);
            printf("%d\n", x ^ query(root[L[u] - 1], root[R[u]], x));
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值