Hdu-4757 Tree(可持久化字典树+lca)
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.
题意:给一棵带权树,每次询问树上一条链上(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));
memset(deep,0,sizeof(deep));
memset(f,0,sizeof(f));
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(root[fa],weight[node],16);
f[node][0]=fa;deep[node]=deep[fa]+1;
for(int i=0;i<g[node].size();i++)
{
if(g[node][i]!=fa)
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)
{
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)));
}
}
}
/*
3 4
1 2 3
1 2
1 3
2 2 2
2 3 1
1 3 1
3 2 1
*/
Hdu-4757 Tree(可持久化字典树+lca)的更多相关文章
- HDU 4757 Tree 可持久化字典树
Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4757 Des ...
- HDU 4757 Tree 可持久化字典树 trie
http://acm.hdu.edu.cn/showproblem.php?pid=4757 给出一棵树,每个节点有权值,每次查询节点 (u,v) 以及 val,问 u 到 v 路径上的某个节点与 v ...
- HDU - 6191 Query on A Tree (可持久化字典树/字典树合并)
题目链接 题意:有一棵树,树根为1,树上的每个结点都有一个数字x.给出Q组询问,每组询问有两个值u,x,代表询问以结点u为根的子树中的某一个数与x的最大异或值. 解法一:dfs序+可持久化字典树.看到 ...
- HDU 4557 Tree(可持久化字典树 + LCA)
http://acm.hdu.edu.cn/showproblem.php?pid=4757 题意: 给出一棵树,每个结点有一个权值,现在有多个询问,每次询问包含x,y,z三个数,求出在x到y的路径上 ...
- hdu 6191--Query on A Tree(持久化字典树)
题目链接 Problem Description Monkey A lives on a tree, he always plays on this tree. One day, monkey A l ...
- BZOJ - 2588 Spoj 10628. Count on a tree (可持久化线段树+LCA/树链剖分)
题目链接 第一种方法,dfs序上建可持久化线段树,然后询问的时候把两点之间的所有树链扒出来做差. #include<bits/stdc++.h> using namespace std; ...
- HDU.4757.Tree(可持久化Trie)
题目链接 \(Description\) 给定一棵树,点有点权.\(Q\)次询问\(x,y,z\),求\(x\)到\(y\)的简单路径中,与\(z\)异或能得到的最大的数是多少. \(Solution ...
- 【HDU 6191】Query on A Tree 【可持久化字典树】
题目 给出一棵有n个结点的树,树根是1,每个结点给出一个value.然后给出q个询问,每个询问给出两个整数u和x,你要在以u结点为根的子树中找出一个结点v,使得val[v] xor x最大, 并输出这 ...
- BZOJ 2588: Spoj 10628. Count on a tree-可持久化线段树+LCA(点权)(树上的操作) 无语(为什么我的LCA的板子不对)
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 9280 Solved: 2421 ...
随机推荐
- angularjs &登录跳转
如果要使用$location,$stateParams,那么必须有相应形参controller: function ($rootScope, $http, $scope, $state,$locati ...
- Linux v4l2编程(摄像头信息采集)
基于Linux3.4.2,自己做一点儿视频信息采集及网络传输的小实验,边做边学,一些基础知识同步整理..... 1. 定义 V4L2(Video For Linux Two) 是内核提供给应用程序访问 ...
- 漫谈C语言结构体
相信大家对于结构体都不陌生.在此,分享出本人对C语言结构体的学习心得.如果你发现这个总结中有你以前所未掌握的,那本文也算是有点价值了.当然,水平有限,若发现不足之处恳请指出.代码文件test.c我放在 ...
- poi提取docx中的文字和图片
package com.fry.poiDemo.dao; import java.io.File; import java.io.FileInputStream; import java.io.Fil ...
- python 使用yaml模块
python:yaml模块一.yaml文件介绍YAML是一种简洁的非标记语言.其以数据为中心,使用空白,缩进,分行组织数据,从而使得表示更加简洁.1. yaml文件规则基本规则: 大小写敏感 ...
- ubuntu php多版本共存切换
做开发时,由于本机开发的php版本跟线上发布的php版本不一致,很容易在上线后,发现因版本的影响导致一些bug,但又不想重新去换本机的php版本,那么多版本共存就很方便了!有必要时,切换到指定版本测试 ...
- laravel新增路由文件
除去原有路由文件,有时为方便路由管理,我们可以新增独立路由文件,如:针对管理后台的路由文件. 1.在routes文件夹下创建新路由文件admin.php 2.在app\Providers\RouteS ...
- 嵌入式C语言4.3 C语言内存空间的使用-指针与运算符
1. ++.--.+.- int a=100; a+1; 对比: int *p=xxx; [0x12] p+1; [0x12+1*sizeof(*p)] 指针的加法(减法)运算, ...
- log4j日志格式化
Apache log4j 提供了各种布局对象,每一个对象都可以根据各种布局格式记录数据.另外,也可以创建一个布局对象格式化测井数据中的特定应用的方法. 所有的布局对象 - Appender对象收到 L ...
- 20140919 进程间通信 系统栈 用户栈 多级反馈队列 windows 内存管理
1.进程间通信 共享内存(剪切板) 匿名管道只能实现父子进程间的通信(以文件系统为基础): 匿名管道是什么,有什么用,怎么用 1.创建父进程,也就是在解决方案中建立一个parent的工程 2.在par ...