HDU - 6191 Query on A Tree (可持久化字典树 | 字典树合并)

版权声明:Why is everything so heavy? https://blog.csdn.net/lzc504603913/article/details/82316275

Query on A Tree

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 1692    Accepted Submission(s): 558


 

Problem Description

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,⋯,Vn, indicating the value of node i.

The second line contains n-1 non-negative integers F1,F2,⋯Fn−1, Fi means the father of node i+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≤105

0≤Vi≤109

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

1≤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

 

Source

2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)

 

Recommend

liuyiding

 

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

解题思路:主要用来学习字典树合并,还是很简单的。合并的过程天然是启发式的。这题用可持久化也能AC,主要是要先把dfs序求出来,然后就像主席树那样操作就好了。字典树合并的话,要注意,由于是启发式的,因此合并的时候,子树的结构会被破坏,因此在合并前,要先把相关的答案计算出来。

可持久化解法:2400ms+

#include<iostream>
#include<string.h>
#include<queue>
#include<bitset>
#include<vector>
using namespace std;
typedef long long ll;
const int MAXN=100005;
const int INF=1e18;

vector<int> G[MAXN];

int root=1;//总的根的个数
int ch[MAXN*33][2];//子节点
int num[MAXN*33];//当前编号的节点有多少个
int tot=0;//总的点数
int rt[MAXN];//第i棵树的根的编号
int a[MAXN];

void insert(int x,int r,int lr){
    bitset<32> s(x);

    int cur=r;
    int lcur=lr;
    for(int i=31;i>=0;i--){
        if(!ch[cur][s[i]]){
            ch[cur][s[i]]=++tot;
            ch[cur][!s[i]]=ch[lcur][!s[i]];
            num[ch[cur][s[i]]]=num[ch[lcur][s[i]]];
        }
        cur=ch[cur][s[i]];
        lcur=ch[lcur][s[i]];
        num[cur]++;
    }
}

int lft[MAXN],riht[MAXN];
int temp=0;
void insertdfs(int u,int fa){
    rt[u]=++tot;
    lft[u]=u;
    riht[u]=u;
    insert(a[u],rt[u],rt[temp]);
    temp=u;
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        if(v!=fa){
            insertdfs(v,u);
            riht[u]=riht[v];
        }
    }

}


int query(int x,int rt,int lrt){
    bitset<32> s(x);
    bitset<32> ans;

    int ucur=rt;
    int vcur=lrt;
    for(int i=31;i>=0;i--){
        if(num[ch[vcur][!s[i]]]-num[ch[ucur][!s[i]]]>0){
            //printf("%d %d %d\n",i,num[ch[ucur][!s[i]]],num[ch[vcur][!s[i]]]);
            ans[i]=1;
            ucur=ch[ucur][!s[i]];
            vcur=ch[vcur][!s[i]];
        }
        else{
            ucur=ch[ucur][s[i]];
            vcur=ch[vcur][s[i]];
        }
    }
    return ans.to_ullong();
}



int main(){
    int N,Q;
    while(~scanf("%d%d",&N,&Q)){
        tot=0;
        temp=0;
        memset(ch,0,sizeof(ch));
        memset(num,0,sizeof(num));
        memset(rt,0,sizeof(rt));

        for(int i=1;i<=N;i++){
            scanf("%d",&a[i]);
            G[i].clear();
        }

        int v;
        for(int i=2;i<=N;i++){
            scanf("%d",&v);
            G[v].push_back(i);
        }

        insertdfs(1,0);

        int u,x;
        while(Q--){
            scanf("%d%d",&u,&x);
            //cout<<lft[u]<<" "<<riht[u]<<endl;
            printf("%d\n",max(x^a[u],query(x,rt[lft[u]],rt[riht[u]])));
        }



    }



    return 0;
}















字典树启发式合并解法 1700ms+

#include<iostream>
#include<string.h>
#include<queue>
#include<bitset>
#include<vector>
using namespace std;
typedef long long ll;
const int MAXN=100005;
const int INF=1e18;

vector<int> G[MAXN];

int ch[MAXN*33][2];//子节点
int num[MAXN*33];//当前编号的节点有多少个
int tot=0;//总的点数
int rt[MAXN];//第i棵树的根的编号
int a[MAXN];

vector<pair<int,int > > qy[MAXN];

void insert(int x,int r){
    bitset<32> s(x);
    int cur=r;
    for(int i=31;i>=0;i--){
        if(!ch[cur][s[i]]){
            ch[cur][s[i]]=++tot;
        }
        cur=ch[cur][s[i]];
        num[cur]++;
    }
}

//超简单的
int merge(int rt,int lrt){
    if(!rt)return lrt;
    if(!lrt)return rt;
    ch[rt][0]=merge(ch[rt][0],ch[lrt][0]);
    ch[rt][1]=merge(ch[rt][1],ch[lrt][1]);
    return rt;
}


int query(int x,int rt){
    bitset<32> s(x);
    bitset<32> ans;
    int ucur=rt;
    for(int i=31;i>=0;i--){
        if(num[ch[ucur][!s[i]]]>0){
            ans[i]=1;
            ucur=ch[ucur][!s[i]];
        }
        else{
            ucur=ch[ucur][s[i]];
        }
    }
    return ans.to_ullong();
}

int ans[MAXN];
int temp=0;
void insertdfs(int u,int fa){
    rt[u]=++tot;
    insert(a[u],rt[u]);
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        if(v!=fa){
            insertdfs(v,u);
            rt[u]=merge(rt[u],rt[v]);
        }
    }
    //先计算了,以免后面合并时被破坏结构
    for(auto &q: qy[u]){
        ans[q.first] = query(q.second,rt[u]);
    }
}


int main(){
    int N,Q;
    while(~scanf("%d%d",&N,&Q)){
        tot=0;
        temp=0;
        memset(ch,0,sizeof(ch));
        memset(num,0,sizeof(num));
        memset(rt,0,sizeof(rt));

        for(int i=1;i<=N;i++){
            scanf("%d",&a[i]);
            G[i].clear();
            qy[i].clear();
        }
        int v;
        for(int i=2;i<=N;i++){
            scanf("%d",&v);
            G[v].push_back(i);
        }
        int u,x;
        for(int i=0;i<Q;i++){
            scanf("%d%d",&u,&x);
            qy[u].push_back(make_pair(i,x));
        }
        insertdfs(1,0);
        for(int i = 0; i < Q; i++)
            printf("%d\n", ans[i]);
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/lzc504603913/article/details/82316275
今日推荐