【HDU - 4757】Tree 【LCA+持久化01 Trie】

Zero and One are good friends who always have fun with each other. This time, they decide to do something on a tree which is a kind of graph that there is only one path from node to node. First, Zero will give One an tree and every node in this tree has a value. Then, Zero will ask One a series of queries. Each query contains three parameters: x, y, z which mean that he want to know the maximum value produced by z xor each value on the path from node x to node y (include node x, node y). Unfortunately, One has no idea in this question. So he need you to solve it.
Input
There are several test cases and the cases end with EOF. For each case:

The first line contains two integers n(1<=n<=10^5) and m(1<=m<=10^5), which are the amount of tree’s nodes and queries, respectively.

The second line contains n integers a[1..n] and ai is the value on the ith node.

The next n–1 lines contains two integers u v, which means there is an connection between u and v.

The next m lines contains three integers x y z, which are the parameters of Zero’s query.
Output
For each query, output the answer.
Sample Input
3 2
1 2 2
1 2
2 3
1 3 1
2 3 2
Sample Output
3
0
题意 给出一棵树,树上的点都有权值,每次给出一组询问x y z,求从x到y路径上的点权值和z异或得到的最大值

分析: 我们建一个 子节点继承父节点的01Trie ,然后对于每一组查询x y z ,我们先求出lca(x,y) ,然后再求出 fa(lca),之后分别求出两段区间 ( fa(lca) , x ) ( fa(lca) , y ) 的最大值,然后再取最大值。
代码

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 1e5+11;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;


int son[MAXN*35][2],sum[MAXN*35],root[MAXN*35],sz;
int Insert(int a,int pre){
    int x=++sz,t=x;
    for(int i=31;i>=0;i--){
        son[x][1]=son[pre][1],son[x][0]=son[pre][0];
        sum[x]=sum[pre]+1;
        int j=1&(a>>i);
        son[x][j]=++sz;
        x=son[x][j],pre=son[pre][j];
    }
    sum[x]=sum[pre]+1; 
    return t; 
}
int Query(int a,int le,int ri){
    int ans=0;
    for(int i=31;i>=0;i--){
        int j=1&(a>>i); j=!j;
        if(sum[son[ri][j]]-sum[son[le][j]]>0) {
            ans|=(1<<i);
            le=son[le][j],ri=son[ri][j];
        }else 
            le=son[le][!j],ri=son[ri][!j];
    }
    return ans;
}

int n,m;
vector<int>ve[MAXN];
int p[MAXN][25],dep[MAXN],arr[MAXN];
void dfs(int now,int pre,int de){
    dep[now]=de; p[now][0]=pre;
    root[now]=Insert(arr[now],root[pre]);
    for(int i=0;i<ve[now].size();i++){
        int v=ve[now][i];
        if(v==pre) continue;
        dfs(v,now,dep[now]+1);
    }
}
void lca_init(){
    for(int j=1;(1<<j)<=n;j++) {
        for(int i=1;i<=n;i++) 
            p[i][j]=p[p[i][j-1]][j-1];
    }
}
int LCA(int v,int u){
    if(dep[v]<dep[u]) swap(v,u);
    int d=dep[v]-dep[u];
    for(int i=0;(d>>i)!=0;i++)
        if((d>>i)&1) v = p[v][i];
    if(v==u) return v;
    for(int i=20;i>=0;i--)
        if(p[v][i]!=p[u][i]) v=p[v][i],u=p[u][i];
    return p[v][0];
}

int main(){
    CLOSE();
//  fread();
//  fwrite();
    while(scanf("%d%d",&n,&m)!=EOF){
        sz=0;
        for(int i=1;i<=n;i++) ve[i].clear();

        for(int i=1;i<=n;i++) scanf("%d",&arr[i]); 
        for(int i=1;i<n;i++){
             int a,b;
            scanf("%d%d",&a,&b);
            ve[a].push_back(b);
            ve[b].push_back(a);
        } 
        dfs(1,0,0); lca_init();
        while(m--){
            int x,y,z;scanf("%d%d%d",&x,&y,&z);
            int lca=LCA(x,y);  lca=p[lca][0];
        //  printf("lca  %d\n",lca);
            int ans=Query(z,root[lca],root[x]);
        //  printf("ans %d\n",ans);
            ans=max(ans,Query(z,root[lca],root[y]));
            printf("%d\n",ans);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值