天天看點

【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

題意 給出一棵樹,樹上的點都有權值,每次給出一組詢問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 = +;
const int MAXM = ;
const int mod = +;
const int inf = ;


int son[MAXN*][],sum[MAXN*],root[MAXN*],sz;
int Insert(int a,int pre){
    int x=++sz,t=x;
    for(int i=;i>=;i--){
        son[x][]=son[pre][],son[x][]=son[pre][];
        sum[x]=sum[pre]+;
        int j=&(a>>i);
        son[x][j]=++sz;
        x=son[x][j],pre=son[pre][j];
    }
    sum[x]=sum[pre]+; 
    return t; 
}
int Query(int a,int le,int ri){
    int ans=;
    for(int i=;i>=;i--){
        int j=&(a>>i); j=!j;
        if(sum[son[ri][j]]-sum[son[le][j]]>) {
            ans|=(<<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][],dep[MAXN],arr[MAXN];
void dfs(int now,int pre,int de){
    dep[now]=de; p[now][]=pre;
    root[now]=Insert(arr[now],root[pre]);
    for(int i=;i<ve[now].size();i++){
        int v=ve[now][i];
        if(v==pre) continue;
        dfs(v,now,dep[now]+);
    }
}
void lca_init(){
    for(int j=;(<<j)<=n;j++) {
        for(int i=;i<=n;i++) 
            p[i][j]=p[p[i][j-]][j-];
    }
}
int LCA(int v,int u){
    if(dep[v]<dep[u]) swap(v,u);
    int d=dep[v]-dep[u];
    for(int i=;(d>>i)!=;i++)
        if((d>>i)&) v = p[v][i];
    if(v==u) return v;
    for(int i=;i>=;i--)
        if(p[v][i]!=p[u][i]) v=p[v][i],u=p[u][i];
    return p[v][];
}

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

        for(int i=;i<=n;i++) scanf("%d",&arr[i]); 
        for(int i=;i<n;i++){
             int a,b;
            scanf("%d%d",&a,&b);
            ve[a].push_back(b);
            ve[b].push_back(a);
        } 
        dfs(,,); lca_init();
        while(m--){
            int x,y,z;scanf("%d%d%d",&x,&y,&z);
            int lca=LCA(x,y);  lca=p[lca][];
        //  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 ;
}