Query on A Tree

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

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
 

题目链接:HDU 6191

本来以为是很水的一道题目(确实很水),结果被坑在update的构建顺序上WA了很久,因为是可持久化,需要上一个版本的信息,而恰好一开始是先DFS序,再for每一个元素用它的L[i]时间戳来构建,其实这是有问题的, 因为上一个元素的L[i]不一定跟当前的L[i]连续,因此要按照L[i]的顺序来构建,而不是i的顺序;构建好之后查询一下L[u]~R[u]之间的与x的异或最大值即可

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <climits>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 100010;
struct edge
{
int to, nxt;
edge() {}
edge(int _to, int _nxt): to(_to), nxt(_nxt) {}
} E[N];
struct Trie
{
int nxt[2];
int cnt;
void init()
{
nxt[0] = nxt[1] = 0;
cnt = 0;
}
} L[N * 31];
int tot, root[N];
int head[N], etot;
int ll[N], rr[N], idx;
int arr[N]; void init()
{
L[0].init();
tot = 0;
CLR(head, -1);
etot = 0;
idx = 0;
}
inline void add(int s, int t)
{
E[etot] = edge(t, head[s]);
head[s] = etot++;
}
void update(int &cur, int ori, int step, LL n, int v)
{
cur = ++tot;
L[cur] = L[ori];
L[cur].cnt += v;
if (step < 0)
return ;
int t = (n >> step) & 1;
update(L[cur].nxt[t], L[ori].nxt[t], step - 1, n, v);
}
int Find(int S, int E, int step, LL n)
{
if (step < 0)
return 0;
int t = (n >> step) & 1;
if (L[L[E].nxt[t ^ 1]].cnt - L[L[S].nxt[t ^ 1]].cnt > 0)
return (1LL << step) + Find(L[S].nxt[t ^ 1], L[E].nxt[t ^ 1], step - 1, n);
else
return Find(L[S].nxt[t], L[E].nxt[t], step - 1, n);
}
void dfs_build(int u)
{
ll[u] = ++idx;
update(root[ll[u]], root[ll[u] - 1], 29, arr[u], 1);
for (int i = head[u]; ~i; i = E[i].nxt)
{
int v = E[i].to;
dfs_build(v);
}
rr[u] = idx;
}
int main(void)
{
int n, q, i;
while (~scanf("%d%d", &n, &q))
{
init();
for (i = 1; i <= n; ++i)
scanf("%d", &arr[i]);
for (i = 2; i <= n; ++i)
{
int f;
scanf("%d", &f);
add(f, i);
}
dfs_build(1);
while (q--)
{
int u, x;
scanf("%d%d", &u, &x);
int l = ll[u], r = rr[u];
printf("%d\n", Find(root[l - 1], root[r], 29, x));
}
}
return 0;
}

HDU 6191 Query on A Tree(可持久化Trie+DFS序)的更多相关文章

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

    题目链接 题意:有一棵树,树根为1,树上的每个结点都有一个数字x.给出Q组询问,每组询问有两个值u,x,代表询问以结点u为根的子树中的某一个数与x的最大异或值. 解法一:dfs序+可持久化字典树.看到 ...

  2. HDU 6191 Query on A Tree(可持久化Trie)

    题意 \(n\) 个点的有根树,根为 \(1\) .每个点有点权,有 \(q\) 个询问,每次询问以 \(u\) 为根的子树的点的点权中异或 \(x\) 所得的最大值是多少. 思路 求出整棵树的 \( ...

  3. HDU 6191 Query on A Tree ( 2017广西邀请赛 && 可持久化Trie )

    题目链接 题意 : 给你一棵树.树上的每个点都有点权.之后有若干次问询.每次问询给出一个节点编号以及一个整数 X .问你以给出节点为根的子树中哪个节点和 X 异或最大.输出这个值 分析 : 看到这种树 ...

  4. [hdu 6191] Query on A Tree

    Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Othe ...

  5. HDU 6191 Query on A Tree(字典树+离线)

    Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Othe ...

  6. BZOJ_1803_Spoj1487 Query on a tree III_主席树+dfs序

    BZOJ_1803_Spoj1487 Query on a tree III_主席树 Description You are given a node-labeled rooted tree with ...

  7. 【BZOJ1803】Spoj1487 Query on a tree III 主席树+DFS序

    [BZOJ1803]Spoj1487 Query on a tree III Description You are given a node-labeled rooted tree with n n ...

  8. SPOJ Query on a tree III (树剖(dfs序)+主席树 || Splay等平衡树)(询问点)

    You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the node whose ...

  9. SP1487 PT07J - Query on a tree III 主席树+dfs序

    Code: #include<iostream> #include<cstdio> #include<algorithm> #include<string&g ...

随机推荐

  1. PAT (Basic Level) Practice 1009 说反话

    给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式: 测试输入包含一个测试用例,在一行内给出总长度不超过 80 的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小 ...

  2. R语言学习笔记(一):mode, class, typeof的区别

    要了解这三个函数的区别,先了解numeric, double与integer. 在r中浮点数有两个名字叫numeric与double. double是指它的类型(type)名字,numeric是指它的 ...

  3. PHP.39-扩展-锁机制解决并发-MySQL锁、PHP文件锁

    锁机制适用于高并发场景:高并发订单.秒杀…… apache压力测试 Mysql锁详解 语法 加锁:LOCK TABLE 表名1 READ|WRITE, 表名2 READ|WRITE ......... ...

  4. 4299: Codechef FRBSUM

    4299: Codechef FRBSUM https://www.lydsy.com/JudgeOnline/problem.php?id=4299 分析: 主席树. https://blog.se ...

  5. A problem occurred evaluating project ':'. > ASCII

    项目编译出错: 错误信息如下: FAILURE: Build failed with an exception. * Where: Build file 'F:\git\i***\build.grad ...

  6. oracle 游标例子

    CREATE OR REPLACE PROCEDURE PRC_WAP_ACTIVEUSERS(RETCODE OUT VARCHAR2) /***************************** ...

  7. jmeter常用的内置变量

    1. vars   API:http://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html vars.get(& ...

  8. unity3d 摄像机跟随角色时被物体遮挡解决方案

    参考文章:http://www.xuanyusong.com/archives/1991 在看此文章时请先看上面的参考文章 看完以上文章后,你也许会想人家都已经给出所有代码了,你还写个毛啊 别急,现在 ...

  9. 【APUE】Chapter13 Daemon Processes

    这章节内容比较紧凑,主要有5部分: 1. 守护进程的特点 2. 守护进程的构造步骤及原理. 3. 守护进程示例:系统日志守护进程服务syslogd的相关函数. 4. Singe-Instance 守护 ...

  10. Windows自带的磁盘填充命令

    一张不用了的SD卡要给别人,之前一直是手机使用的,担心有一些资料被恢复,想要将它内容清空.以前就知道数字公司有一个磁盘填充的工具,后来网上搜一搜发现Windows有一个自带的命令用于磁盘填充. 首先进 ...