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.
 
题目大意:给你一颗n个点的树,每个点有一个权值。然后有m个询问,问从x到y的简单路径中,权值 xor z最大是多少。
思路:可持久化的0-1字典树,每个点在父节点的历史版本上新建一棵字典树,字典树上的每个结点给一个值记录从这个结点到树的根节点,有多少个权值会经过这个字典树上的结点。询问的时候在字典树上奏即可。由于a[i]<2^16,那么字典树的深度最大为16,空间复杂度为O(16 * n)。
PS:tarjan果然要比RMQ快啊。
PS2:数据保障z<2^16,虽然题目没讲……
 
代码(1265MS):
 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std; const int MAX = ;
const int MAXN = * MAX;
const int MAXE = * MAXN; int head[MAXN], weight[MAXN], fa[MAX];
bool vis[MAX];
int to[MAXE], next[MAXE], id[MAXE];
int n, m, ecnt; inline void init() {
memset(head, , sizeof(head));
memset(vis, , sizeof(vis));
for(int i = ; i <= n; ++i) fa[i] = i;
ecnt = ;
} inline void add_edge(int u, int v, int i) {
to[ecnt] = v; id[ecnt] = i; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; id[ecnt] = i; next[ecnt] = head[v]; head[v] = ecnt++;
} struct QUERY {
int x, y, lca, z;
void read(int i) {
scanf("%d%d%d", &x, &y, &z);
add_edge(x + n, y + n, i);
}
} Query[MAX]; int get_set(int x) {
return fa[x] == x ? x : fa[x] = get_set(fa[x]);
} void dfs_LCA(int u, int f) {
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(v == f) continue;
dfs_LCA(v, u);
fa[v] = u;
}
vis[u] = true;
for(int p = head[u + n]; p; p = next[p]) {
int v = to[p] - n;
if(vis[v]) Query[id[p]].lca = get_set(v);
}
} struct Node {
int go[], cnt;
} tree[ * MAX];
int root[MAX], Tcnt; void init_tree() {
Tcnt = ; root[] = ;
} int insert(int x, int val) {
tree[Tcnt] = tree[x];
int ret = x = Tcnt++;
for(int i = ; i >= ; --i) {
int idx = (val >> i) & , t = Tcnt++;
tree[t] = tree[tree[x].go[idx]];
++tree[t].cnt;
tree[x].go[idx] = t;
x = t;
}
return ret;
} void dfs_build(int u, int f) {
root[u] = insert(root[f], weight[u]);
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(v == f) continue;
dfs_build(v, u);
}
} int query(int x, int y, int lca, int val) {
int ret = , ret_lca = weight[lca] ^ val;
x = root[x], y = root[y], lca = root[lca];
for(int i = ; i >= ; --i) {
int idx = !((val >> i) & );
int cnt = tree[tree[x].go[idx]].cnt + tree[tree[y].go[idx]].cnt - * tree[tree[lca].go[idx]].cnt;
if(cnt > ) ret |= ( << i);
else idx = !idx;
x = tree[x].go[idx], y = tree[y].go[idx], lca = tree[lca].go[idx];
}
return max(ret, ret_lca);
} int main() {
while(scanf("%d%d", &n, &m) != EOF) {
for(int i = ; i <= n; ++i) scanf("%d", &weight[i]);
init();
for(int i = ; i < n; ++i) {
int u, v;
scanf("%d%d", &u, &v);
add_edge(u, v, );
}
for(int i = ; i <= m; ++i) Query[i].read(i);
dfs_LCA(, );
init_tree();
dfs_build(, );
for(int i = ; i <= m; ++i)
printf("%d\n", query(Query[i].x, Query[i].y, Query[i].lca, Query[i].z));
}
}

HDU 4757 Tree(可持久化字典树)(2013 ACM/ICPC Asia Regional Nanjing Online)的更多相关文章

  1. HDU 4757 Tree 可持久化字典树

    Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4757 Des ...

  2. HDU 4757 Tree 可持久化字典树 trie

    http://acm.hdu.edu.cn/showproblem.php?pid=4757 给出一棵树,每个节点有权值,每次查询节点 (u,v) 以及 val,问 u 到 v 路径上的某个节点与 v ...

  3. HDU 4758——Walk Through Squares——2013 ACM/ICPC Asia Regional Nanjing Online

    与其说这是一次重温AC自动机+dp,倒不如说这是个坑,而且把队友给深坑了. 这个题目都没A得出来,我只觉得我以前的AC自动机的题目都白刷了——深坑啊. 题目的意思是给你两个串,每个串只含有R或者D,要 ...

  4. HDU 4749 Parade Show 2013 ACM/ICPC Asia Regional Nanjing Online

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4749 题目大意:给一个原序列N,再给出一个序列M,问从N中一共可以找出多少个长度为m的序列,序列中的数 ...

  5. [2013 ACM/ICPC Asia Regional Nanjing Online C][hdu 4750]Count The Pairs(kruskal + 二分)

    http://acm.hdu.edu.cn/showproblem.php?pid=4750 题意: 定义f(u,v)为u到v每条路径上的最大边的最小值..现在有一些询问..问f(u,v)>=t ...

  6. HDU 4751 Divide Groups 2013 ACM/ICPC Asia Regional Nanjing Online

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 题目大意:判断一堆人能否分成两组,组内人都互相认识. 解题思路:如果两个人不是相互认识,该两人之 ...

  7. hdu 4751 Divide Groups bfs (2013 ACM/ICPC Asia Regional Nanjing Online 1004)

    SDUST的训练赛 当时死磕这个水题3个小时,也无心去搞其他的 按照题意,转换成无向图,预处理去掉单向的边,然后判断剩下的图能否构成两个无向完全图(ps一个完全图也行或是一个完全图+一个孤点) 代码是 ...

  8. HDU4753 Fishhead’s Little Game——2013 ACM/ICPC Asia Regional Nanjing Online

    今天比赛又是做得好水的.被狂虐啊. 比赛两个多小时一直没出题,遒遒最先交的若干发都wa了.T_T 我独自在一遍苦思了1006这个题,还好最后把这个题目A掉了,不然又是深坑队友. 题目的意思我就不多说了 ...

  9. hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4710 Balls Rearrangement Time Limit: 6000/3000 MS (Java/Ot ...

随机推荐

  1. Question 20171117 Java中的编码问题?

    撰文缘由 前几天做一个邮件发送功能,一些常用信息配置在properties文件中,通过prop.getProperty(key)来获取配置的信息,结果配置文件中是用中文写的,邮件发送成功后,邮箱中的激 ...

  2. Unity 游戏框架搭建 (八) 减少加班利器-QLog

    为毛要实现这个工具? 在我小时候,每当游戏到了测试阶段,交给QA测试,QA测试了一会儿拿着设备过来说游戏闪退了....当我拿到设备后测了好久Bug也没有复现,排查了好久也没有头绪,就算接了Bugly拿 ...

  3. 辨析:Object与Instance都是对象,概念上没有区别。

    Object与Instance有重要的区别:Object是客观世界中存在的实体:Instance是将Object虚拟到计算机世界的实例,它的生存方式是可运行的代码,它的生存环境是计算机中的内存资源,生 ...

  4. iOS之网络请求及各类错误代码含义总结(包含AFN错误码大全)

    转自http://blog.csdn.net/wangyanchang21/article/details/50932191 在很多时候都会遇到错误, 还会带有一些 Error Code , 比如在各 ...

  5. [USACO06NOV]玉米田Corn Fields(动态规划,状态压缩)

    题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...

  6. Mysql8.0 3306端口无法远程连接

    在阿里云上搭建MySql8.0数据库服务,在阿里云上可以成功连接登陆使用,但用自己的电脑远程连接时却无法成功连接 经过资料查找,找出原因如下: 1.首先通过查看MySQL的的用户信息 可以看到host ...

  7. .NET中获取当前的IP地址

    /// <summary> /// 获取本地IP地址信息 /// </summary> public static string GetAddressIP() { ///获取本 ...

  8. GET POST 请求的详细区别

    前言: 作为最常见的请求方式 在面试很有可能会被问到 所以在这里做一个简单总结 GET get方法向页面请求发送参数 地址和参数之间用?进行分割 例如 localhost:80/download.ht ...

  9. flask之route中的参数

    flask的路由中有一些参数 使用案例 from flask import Flask, render_template, url_for, session, request, redirect ap ...

  10. YII2 多MongoDB配置和使用

    1:在config/web.php 文件下配置多个连接即可: 注意在componets 下 'mongodb' => [ 'class' => '\yii\mongodb\Connecti ...