Hdu-4757 Tree(可持久化字典树+lca)

题目链接:点这

我的github地址:点这

Problem Description

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 a[i](0<=a[i]<2^{16}) 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

Source

2013 ACM/ICPC Asia Regional Nanjing Online

题意:给一棵带权树,每次询问树上一条链上(x 到 y)的所有权值xor z的最大值。

思路:可持久化字典树+lca

不能用lca的倍增算法,会t,

关于lca可以看这个:https://www.cnblogs.com/zhouzhendong/p/7256007.html

/*
    data:2018.04.26
    author:gswycf
    link:http://acm.hdu.edu.cn/showproblem.php?pid=4825
    accout:tonysave
*/
#define ll long long
#define IO ios::sync_with_stdio(false);
#define maxn 100005

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<vector>
using namespace std;

class Node{
    public:
        int cnt,ls,rs;
};
int n,m,cnt,aa,bb;
Node tr[32*maxn];
int weight[maxn];int root[maxn];int deep[maxn],f[maxn][31];
vector<int>  g[maxn];

inline void init()
{
    memset(tr,0,sizeof(tr));
    //memset(weight,0,sizeof(weight));
    memset(root,0,sizeof(root));
    deep[1]=0;
    cnt=0;
    for(int i=0;i<=n;i++)
        g[i].clear();
}
int in(int pre,int x,int deep)
{
    int num=++cnt;
    tr[num]=tr[pre];
    tr[num].cnt=tr[pre].cnt+1;
    if(deep<0)return num;
    if(!((x>>deep)&1))tr[num].ls=in(tr[pre].ls,x,deep-1);
    else tr[num].rs=in(tr[pre].rs,x,deep-1);
    return num;
}
int query(int l,int r,int x,int deep)
{
    if(deep<0)return 0;
    if(!((x>>deep)&1))
    {
        if(tr[tr[r].rs].cnt>tr[tr[l].rs].cnt)return (1<<deep)+query(tr[l].rs,tr[r].rs,x,deep-1);
        else return query(tr[l].ls,tr[r].ls,x,deep-1);
    }
    else
    {
        if(tr[tr[r].ls].cnt>tr[tr[l].ls].cnt)return (1<<deep)+query(tr[l].ls,tr[r].ls,x,deep-1);
        else return query(tr[l].rs,tr[r].rs,x,deep-1);
    }
}
void bfs(int node,int fa)
{
    root[node]=in(fa,weight[node],16);
    f[node][0]=fa;
    for(int i=0;i<g[node].size();i++)
    {
        if(g[node][i]!=fa)
        {
            deep[g[node][i]]=deep[node]+1;
            bfs(g[node][i],node);
        }
    }
}
inline void init2()
{
    for(int j=1;(1<<j)<=n;j++)
        for(int i=1;i<=n;i++)
            f[i][j]=f[f[i][j-1]][j-1];
}
int lca(int a,int b,int c)
{
    aa=a,bb=b;
    if(deep[a]>deep[b])swap(a,b);
    int d=deep[b]-deep[a];
    for(int i=0;i<30;i++)
        if((1<<i)&d)b=f[b][i];
    if(a==b)return a;
    for(int i=29;i>=0;i--)
    {
        if(f[a][i]!=f[b][i])
            a=f[a][i],b=f[b][i];
    }
    b=f[b][0];
    return b;

}

int main()
{
    int a,b,c;
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(int i=1;i<=n;i++)scanf("%d",&weight[i]);
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&a,&b);
            g[a].push_back(b);
            g[b].push_back(a);
        }
        bfs(1,0);init2();
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            int k=lca(a,b,c);
            printf("%d\n",max(query(root[k-1],root[a],c,16),query(root[k-1],root[b],c,16)));
        }
    }
}

原文地址:https://www.cnblogs.com/fantastic123/p/8976091.html

时间: 2024-07-31 00:12:45

Hdu-4757 Tree(可持久化字典树+lca)的相关文章

hdu 4757 Tree(可持久化字典树)

题目链接:hdu 4757 Tree 题目大意:给定一棵树,每一个节点有一个值.如今有Q次询问,每次询问u到v路径上节点值与w亦或值的最大值. 解题思路:刚開始以为是树链剖分,事实上树链剖分仅仅是用来求LCA(能够不用树链剖分). 可持久化字典树.在每次插入的同一时候,不改动原先的节点.而是对全部改动的节点复制一个新的节点,而且在新的节点 上做操作,这样做的目的是可以获取某次改动前的状态.同过可持久化的操作,保留了改动前后的公共数据. 对给定树上的全部节点权值建立01字典树,然后每一个节点都保存

【BZOJ2588】【Spoj 10628.】 Count on a tree 可持久化线段树+lca

链接: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/45048639"); } 题解: 对于每个树上节点存一个版本的可持久化线段树,为它到根节点上所有权值的权值线段树(需要离散化). 然后对于每次询问,这条链(a,b)的线段树就是:线段树a+线段树b?线段树lca?线段树falca 然后

hdu 6191 Query on A Tree(dfs序+可持久化字典树)

题目链接:hdu 6191 Query on A Tree 题意: 给你一棵树,每个节点有一个值,现在有q个询问,每个询问 询问一个u x,问以u为根的子树中,找一个节点,使得这个节点的值与x异或的值最大,输出那个最大的值. 题解: dfs序和一棵可持久化字典树就搞定了. 1 #include<bits/stdc++.h> 2 #define mst(a,b) memset(a,b,sizeof(a)) 3 #define F(i,a,b) for(int i=a;i<=b;++i) 4

【HDU 6191】Query on A Tree 【可持久化字典树】

题目 给出一棵有n个结点的树,树根是1,每个结点给出一个value.然后给出q个询问,每个询问给出两个整数u和x,你要在以u结点为根的子树中找出一个结点v,使得val[v] xor x最大, 并输出这个最大值 分析 显而易见的可持久化字典树,只不过这次每次查询不是查询一个区间,而是查询一棵子树.那么也很简单,我们只要预处理出dfs序然后找出每个结点以它为根的子树在dfs序中的区间.然后以这个区间建可持久化字典树就可以了. 1 #include <cstdio> 2 #include <c

hdu 1247 Hat’s Words 字典树

// hdu 1247 Hat's Words 字典树 // // 题目大意: // // 在一些字符串中,找到这样字符串:由两个其他的字符串构成 // // 解题思路: // // 字典树,先将这些字符串插入到字典树中,然后枚举断点,如果 // 字符串的前后两段都找到了,输出该串即可~ // // 感悟: // // 这道题目的话,就是字典树上的暴力嘛,细节方面还是要多多注意 // val值还是不能少哟~因为查找到了该串,不一定是一个单词,可能 // 是中间的一个节点,即某个字符串的前缀~~~

【BZOJ2741】【FOTILE模拟赛】L 可持久化字典树+分块

广告: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/44496739"); } 题解: 首先我们处理出来sum[0,n]作为异或前缀和,然后答案就不再是[l,r]中间某段区间的异或和,而转化成求了[l?1,r]中任意两点异或和的最大值. 然后我们分块处理出fi,j表示 [第i块的开头,j

[ACM] hdu 1251 统计难题 (字典树)

统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每一个提问都是一个字符串. 注意:本题仅仅有一组測试数据,处理到文件结束. Out

HDU 2094 产生冠军 (字典树+拓扑)

产生冠军 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8591    Accepted Submission(s): 4047 Problem Description 有一群人,打乒乓球比赛,两两捉对撕杀,每两个人之间最多打一场比赛. 球赛的规则如下: 如果A打败了B,B又打败了C,而A与C之间没有进行过比赛,那么就认定,A一定能打

codechef Xor Queries (可持久化字典树)

题目链接:codechef Xor Queries 题意: 题解: 一棵可持久化字典树就行了. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=(a);i<=(b);++i) 3 using namespace std; 4 5 const int N=5e5+7; 6 struct Node{int son[2],cnt;}ch[N*40]; 7 int root[N],cnt,ed,n; 8 9 void ins(int