Query on A Tree

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

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
 
Source
 

显然,又是data structure。。。

网上有用DFS序来做的,但是本蒟蒻并不太清楚他们dalao的做法,所以只是大了一发可持久化trie合并。。

对于这一题,主要涉及trie的合并,要将u的子节点的信息合并到u的身上去。

那么,假设要将v的信息并到u上,则:

 int merge(int u,int v) {
     if (!u) return v;
     if (!v) return u;
     ch[u][]=merge(ch[u][],ch[v][]);
     ch[u][]=merge(ch[u][],ch[v][]);
     return u;
 }

那么这题差不多就可以A了。

code:

 %:pragma gcc optimize()
 #include<cstdio>
 #include<cstring>
 #include<algorithm>
 #include<vector>
 #define jug(i,x) (((1<<i)&x)>0)
 #define M(a,x) memset(a,x,sizeof a)
 using namespace std;
 ,Nod=;
 int n,tot,Q,a[N],lnk[N],nxt[N],son[N];
 int ro[N],ans[N];
 struct que {int v,i;};
 vector <que> qr[N];
 struct persistent_trie {
     ];
     ;}
     int newnode() {
         M(ch[cnt],);
         return cnt++;
     }
     int merge(int x,int y) {
         if (!x) return y;
         if (!y) return x;
         ch[x][]=merge(ch[x][],ch[y][]);
         ch[x][]=merge(ch[x][],ch[y][]);
         return x;
     }
     void insert(int x,int v) {
         int u=ro[x];
         ; i>=; i--) {
             bool c=jug(i,v);
             if (!ch[u][c]) ch[u][c]=newnode();
             u=ch[u][c];
         }
     }
     int query(int x,int v) {
         ;
         ; i>=; i--) {
             bool c=jug(i,v);
             -c]) ret|=(<<i),u=ch[u][-c];
             else u=ch[u][c];
         }
         return ret;
     }
 }pt;
 inline int read() {
     ; char ch=getchar();
     ') ch=getchar();
     +ch-',ch=getchar();
     return x;
 }
 void add(int x,int y) {
     nxt[++tot]=lnk[x],son[tot]=y,lnk[x]=tot;
 }
 void DFS(int x) {
     ro[x]=pt.newnode();
     pt.insert(x,a[x]);
     for (int j=lnk[x]; j; j=nxt[j])
         DFS(son[j]),ro[x]=pt.merge(ro[x],ro[son[j]]);
     ,si=qr[x].size(); i<si; i++)
         ans[qr[x][i].i]=pt.query(x,qr[x][i].v);
 }
 int main() {
     while (scanf("%d%d",&n,&Q)!=EOF) {
         tot=,M(lnk,),M(nxt,),M(ans,),pt.init();
         ; i<=n; i++) a[i]=read();
         ; i<n; i++) {
             );
         }
         ; i<=n; i++) qr[i].clear();
         ; i<=Q; i++) {
             int x=read(); que now;
             now.i=i,now.v=read(),qr[x].push_back(now);
         }
         DFS();
         ; i<=Q; i++) printf("%d\n",ans[i]);
     }
     ;
 }

[hdu 6191] Query on A Tree的更多相关文章

  1. HDU 6191 Query on A Tree(可持久化Trie+DFS序)

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

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

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

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

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

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

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

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

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

  6. S - Query on a tree HDU - 3804 线段树+dfs序

    S - Query on a tree HDU - 3804   离散化+权值线段树 题目大意:给你一棵树,让你求这棵树上询问的点到根节点直接最大小于等于val的长度. 这个题目和之前写的那个给你一棵 ...

  7. hdu 4836 The Query on the Tree(线段树or树状数组)

    The Query on the Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  8. hdu 4912 Paths on the tree(树链拆分+贪婪)

    题目链接:hdu 4912 Paths on the tree 题目大意:给定一棵树,和若干个通道.要求尽量选出多的通道,而且两两通道不想交. 解题思路:用树链剖分求LCA,然后依据通道两端节点的LC ...

  9. Query on a tree——树链剖分整理

    树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...

随机推荐

  1. js操作css变量

    原文:http://css-live.ru/articles/dostup-k-css-peremennym-i-ix-izmenenie-spomoshhyu-javascript.html :ro ...

  2. Shell中的IFS

    一.IFS 介绍 Shell 脚本中有个变量叫 IFS(Internal Field Seprator) ,内部域分隔符.完整定义是The shell uses the value stored in ...

  3. magrittr管道操作符使用解释(一)

    使用管道操作符提高代码简洁性 在编写R语言代码时,有时候需要对一个变量进行一系列的运算,例如对于一个同时包含数值列和字符串列的数据框,如果要计算所有数值列之间的相关系数,一般要分两步,第一步首先筛选数 ...

  4. Oracle Single-Row Functions(单行函数)——NULL-Related Functions

    参考资料:http://docs.oracle.com/database/122/SQLRF/Functions.htm#SQLRF006 Single-row functions return a ...

  5. 屏幕尺寸,分辨率,像素,PPI之间到底什么关系?

    转载自:http://www.jianshu.com/p/c3387bcc4f6e 感谢博主的无私分享. 今天我给大家来讲讲这几个咱们经常打交道的词到底啥意思,以及他们之间到底有什么关系.这篇文章是我 ...

  6. Sqlserver中分页,2012后支持offset + fetch,2012之前用rownum嵌套查询

    今天发现原先用的sql offset fetch好用,换了一个DB就歇菜 歇菜截图 比较了一下,是数据库版本的问题 一个是13,一个是10 版本低的不支持用offset + fetch 进行分页,ms ...

  7. Systemd初始化进程/RHEL 6系统中System V init命令与RHEL 7系统中systemctl命令的对比

    Linux操作系统的开机过程是这样的,即从BIOS开始,然后进入Boot Loader,再加载系统内核,然后内核进行初始化,最后启动初始化进程.初始化进程作为Linux系统的第一个进程,它需要完成Li ...

  8. Django模板操作

    进行加减运算 def index(request): a = request.GET['a'] b = request.GET['b'] c = int(a) + int(b) return Http ...

  9. yum节省安装时间

    yum install java-1.8.0-openjdk 安装jdk yum install tomcat 安装tomcat wget http://repo.mysql.com/mysql-co ...

  10. [原][粒子特效][spark]插值器interpolator

    深入浅出spark粒子特效连接:https://www.cnblogs.com/lyggqm/p/9956344.html 插值器是体现粒子生命周期变化的功能 group使用到插值器的方式: 可以看到 ...