HDU 4757 Tree
Tree
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)
The first line contains two integers $n$ ($1\le n \le 10^{5}$) and $m$ ($1 \le m \le 10^{5}$), which are the amount of tree’s nodes and queries, respectively.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ and $a_i$ ($0 \le a_i <2^{16}$) is the value on the $i$th node.
The next $n–1$ lines contains two integers $u, v$, which means there is an edge between $u$ and $v$.
The next $m$ lines contains three integers $x ,y, z$, which are the parameters of Zero’s query.
Solution
Implementation
#include <bits/stdc++.h>
using namespace std; const int M{<<}, N{<<}; int a[N], nxt[M][], cnt[M], tail, root[N];
vector<int> g[N]; //a NULL NODE
//The empty tree is a well-defined tree! int Insert(int id, int v, int dep){ //this node is going to be modified
//always copy before modification
int cur=tail++;
cnt[cur]=cnt[id]+;
if(dep>=)
if(v&<<dep){
nxt[cur][]=nxt[id][];
nxt[cur][]=Insert(nxt[id][], v, dep-);
}
else{
nxt[cur][]=Insert(nxt[id][], v, dep-);
nxt[cur][]=nxt[id][];
}
else nxt[cur][]=nxt[cur][]=; //error-prone return cur;
} int fa[N][], dep[N]; void dfs(int u, int f){
fa[u][]=f, dep[u]=dep[f]+;
for(int i=; i<; i++)
fa[u][i]=fa[fa[u][i-]][i-]; root[u]=Insert(root[f], a[u], );
for(auto x:g[u])
if(x!=f)
dfs(x, u);
} int get_lca(int u, int v){
if(dep[u]<dep[v]) swap(u, v);
int diff=dep[u]-dep[v];
for(int i=; i<; i++)
if(diff&<<i) u=fa[u][i];
if(u==v) return u;
for(int i=; i>=; i--) //error-prone
if(fa[u][i]!=fa[v][i])
u=fa[u][i], v=fa[v][i];
return fa[u][];
} //keep it simple
int query(int u, int v, int x){
int res=, lca=get_lca(u, v);
int _u=root[u], _v=root[v], _w=root[lca]; for(int i=; i>=; i--){
bool f=!(x&<<i);
//check if this bit valid
if(cnt[nxt[_u][f]]+cnt[nxt[_v][f]]>cnt[nxt[_w][f]]<<) res|=<<i;
else f=!f;
_u=nxt[_u][f], _v=nxt[_v][f], _w=nxt[_w][f];
}
return max(res, x^a[lca]);
} int main(){
for(int n, m; cin>>n>>m; ){
for(int i=; i<=n; i++){
scanf("%d", a+i);
g[i].clear();
}
for(int u, v, i=; i<n; i++){
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
//construct an empty tree
root[]=tail=, memset(nxt[tail], , sizeof(nxt[tail])), cnt[tail]=, tail++; dfs(, );
for(int u, v, x, lca; m--; ){
scanf("%d%d%d", &u, &v, &x);
printf("%d\n", query(u, v, x));
}
}
}
HDU 4757 Tree的更多相关文章
- HDU 4757 Tree(可持久化Trie+Tarjan离线LCA)
Tree Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others) Total Su ...
- 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 题意:给出一棵树,节点有权值.每次询问x到y的路径上与z抑或的最大值. 思路:可持久化trie. ...
- HDU 4757 Tree 可持久化字典树 trie
http://acm.hdu.edu.cn/showproblem.php?pid=4757 给出一棵树,每个节点有权值,每次查询节点 (u,v) 以及 val,问 u 到 v 路径上的某个节点与 v ...
- HDU 4757 Tree(可持续化字典树,lca)
题意:询问树上结点x到结点y路上上的权值异或z的最大值. 任意结点权值 ≤ 2^16,可以想到用字典树. 但是因为是询问某条路径上的字典树,将字典树可持续化,字典树上的结点保存在这条路径上的二进制数. ...
- HDU 4757 Tree(可持久化字典树)(2013 ACM/ICPC Asia Regional Nanjing Online)
Problem Description Zero and One are good friends who always have fun with each other. This time, ...
- HDU.4757.Tree(可持久化Trie)
题目链接 \(Description\) 给定一棵树,点有点权.\(Q\)次询问\(x,y,z\),求\(x\)到\(y\)的简单路径中,与\(z\)异或能得到的最大的数是多少. \(Solution ...
- hdu 5909 Tree Cutting [树形DP fwt]
hdu 5909 Tree Cutting 题意:一颗无根树,每个点有权值,连通子树的权值为异或和,求异或和为[0,m)的方案数 \(f[i][j]\)表示子树i中经过i的连通子树异或和为j的方案数 ...
- HDU 5044 Tree(树链剖分)
HDU 5044 Tree field=problem&key=2014+ACM%2FICPC+Asia+Regional+Shanghai+Online&source=1&s ...
随机推荐
- Linux学期总结
学习笔记链接 第一次 http://www.cnblogs.com/Spr1ngxx/p/4823573.html 第二次 http://www.cnblogs.com/Spr1ngxx/p/4842 ...
- Qt5 新特性
Qt 5 已经临近发布,其最大的特点就是模块化.将原来庞大的模块更细分为不同的部分,同时,一个大版本的升级,当然少不了添加.删除各个功能类.文本简单介绍 Qt5 的特性,其具体内容来自 Qt5 官方 ...
- 支付宝Cookie高危漏洞引发的思考
背景:当时我在做公司的网站支付接入,在调试支付宝WAP支付时,发现一些匪夷所思的事情: 1.我想要切换账号时退到需要输入登录信息时,原账号并没有退出,我按一下后退键又回来了: 2.我关闭浏览器也没有退 ...
- 高性能JavaScript 重排与重绘
先回顾下前文高性能JavaScript DOM编程,主要提了两点优化,一是尽量减少DOM的访问,而把运算放在ECMAScript这一端,二是尽量缓存局部变量,比如length等等,最后介绍了两个新的A ...
- C#微信开发小白成长教程二(新手接入指南,附视频)
距离第一讲又已经过去了一个多星期了,本打算一周更新一讲的,奈何实在太忙.最近也在群里发现有一部分人已经可以熟练调用微信的部分接口但却不是很清楚微信公众平台接收消息的一个处理机制.本讲就来介绍下怎么接入 ...
- sql 重复数据只保留一条
用SQL语句,删除掉重复项只保留一条在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * ...
- android之视频播放
视频播放和音频播放一样,都是使用MediaPlayer来播放的,区别就是MediaPlayer播放视频时是直接在Activity中实现的,而音频播放则需要写到服务中去.使用MediaPlayer只支持 ...
- Android activity跳转方式
方法一:通过SetContentView切换Layout来实现界面的切换,这种方法相当于重绘Activity. protected void onCreate(Bundle savedInstance ...
- Beta版本冲刺———第六天
会议照片: 项目燃尽图: 1.项目进展: 该项目的Beta版本冲刺到今天就大体结束,但是小组依然困在"如何保存每次游戏的分数,并将其排序列在排行榜中"的问题上,小组四个人都在一起解 ...
- URL参数GB2312和UTF-8编码 自动识别
网上找的,以备后用. 直接上代码: public static string QueryStringDecode(string key) { HttpRequest Request = System. ...