2017ACM/ICPC广西邀请赛 K- Query on A Tree trie树合并
Query on A Tree
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)
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?
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
1 2
1
1 3
2 1
题解:
每个数存在各自trie树里边,n个点这是棵树,再从底向上tri树合并起来
查询就是查询一颗合并后的trie树,利用从高位到低位,贪心取
#include <bits/stdc++.h>
inline int read(){int x=,f=;char ch=getchar();while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}return x*f;} using namespace std; #define LL long long
const int N = 2e5; vector<int > G[N];
int n, q, x, u, a[N];
int ch[N*][], root[N],sz; void inserts(int u,int x) {
root[u] = ++sz;
int tmp = sz;
int y = sz;
for(int i = ; i >= ; --i) {
int tmps = (x>>i)&;
if(!ch[y][tmps]) ch[y][tmps] = ++sz;
y = ch[y][tmps];
}
}
int merges(int u,int to) {
if(u == ) return to;
if(to == ) return u;
int t = ++sz;
ch[t][] = merges(ch[u][],ch[to][]);
ch[t][] = merges(ch[u][],ch[to][]);
return t;
}
void dfs(int u) {
inserts(u,a[u]);
for(auto to:G[u]) {
dfs(to);
root[u] = merges(root[u],root[to]);
}
}
LL query(int u,int x) {
int y = root[u];
LL ret = ;
for(int i = ; i >= ; --i) {
int tmps = (x>>i)&;
if(ch[y][tmps^]) ret += (<<i),y = ch[y][tmps^];
else y = ch[y][tmps];
}
return ret;
}
void init() {
for(int i = ; i <= n; ++i) root[i] = ,G[i].clear();
sz = ;
memset(ch,,sizeof(ch));
}
int main( int argc , char * argv[] ){
while(scanf("%d%d",&n,&q)!=EOF) {
for(int i = ; i <= n; ++i) scanf("%d",&a[i]);
init();
for(int i = ; i <= n; ++i) {
scanf("%d",&x);
G[x].push_back(i);
}
dfs();
for(int i = ; i <= q; ++i) {
scanf("%d%d",&u,&x);
printf("%lld\n",query(u,x));
}
}
return ;
}
2017ACM/ICPC广西邀请赛 K- Query on A Tree trie树合并的更多相关文章
- HDU 6191 2017ACM/ICPC广西邀请赛 J Query on A Tree 可持久化01字典树+dfs序
题意 给一颗\(n\)个节点的带点权的树,以\(1\)为根节点,\(q\)次询问,每次询问给出2个数\(u\),\(x\),求\(u\)的子树中的点上的值与\(x\)异或的值最大为多少 分析 先dfs ...
- 2017ACM/ICPC广西邀请赛-重现赛 1010.Query on A Tree
Problem Description Monkey A lives on a tree, he always plays on this tree. One day, monkey A learne ...
- 2017ACM/ICPC广西邀请赛-重现赛
HDU 6188 Duizi and Shunzi 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6188 思路: 签到题,以前写的. 实现代码: #inc ...
- 2017ACM/ICPC广西邀请赛-重现赛1005 CS course
2017-08-31 16:19:30 writer:pprp 这道题快要卡死我了,队友已经告诉我思路了,但是做题速度很缓慢,很费力,想必是因为之前 的训练都是面向题解编程的缘故吧,以后不能这样了,另 ...
- 2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)
上一场CF打到心态爆炸,这几天也没啥想干的 A Math Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- 2017ACM/ICPC广西邀请赛 1005 CS Course
CS Course Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 2017ACM/ICPC广西邀请赛-重现赛 1004.Covering
Problem Description Bob's school has a big playground, boys and girls always play games here after s ...
- 2017ACM/ICPC广西邀请赛-重现赛 1001 A Math Problem
2017-08-31 16:48:00 writer:pprp 这个题比较容易,我用的是快速幂 写了一次就过了 题目如下: A Math Problem Time Limit: 2000/1000 M ...
- 2017ACM/ICPC广西邀请赛 Color it
Color it Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Tota ...
随机推荐
- Spring Data Redis 的坑
用 Spring data redis 的redisTemplate存储数据的时候发现,它的键值前多出现了字符串:\xac\xed\x00\x05t\x00\x03 如本来key=name,会变成&q ...
- 七牛云杜江华:让云 + AI 成为企业服务的标配
12 月 5-6 日,2018 创业邦 100 未来领袖峰会暨创业邦年会(以下简称「创业邦 100 未来领袖峰会」)在北京国家会议中心举行.12 月 5 日下午,七牛云执行副总裁杜江华在企业服务论坛上 ...
- 【Luogu】P3847调整队形(DP)
题目链接 DP果真是考思维啊 增加一个数的操作等价于删掉那个不和谐的数的操作. 所以1.2操作可以忽略. 剩下3.4操作,则可以设计f[i][j]是将区间[i,j]变成回文序列需要的操作数. if(a ...
- gcc/g++ 编译时出现:“对’xxxx’未定义的引用,collect2: error: ld returned 1 exit status” 的错误
出现的问题: 在使用 make 编译实现一个程序时,出现了下面的错误.查看程序源文件所在的目录时发现程序已经完成了编译,并生成了 list_repo.o 的文件,说明是在程序链接生成可执行文件时发生了 ...
- STL中heap用法
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; ]={,, ...
- 【CF659F】Polycarp and Hay(并查集,bfs)
题意: 构造一个矩阵,使得: 矩阵所有格子中数字都小于等于原矩阵,并且至少有一个元素和原矩阵相等, 构造的矩阵除了0以外的数字必须联通并且相等,矩阵中元素之和为K. n,m<=1e3,1< ...
- 【shell】shell编程(一)-入门
如今,不会Linux的程序员都不意思说自己是程序员,而不会shell编程就不能说自己会Linux.说起来似乎shell编程很屌啊,然而不用担心,其实shell编程真的很简单.背景 什么是shell编程 ...
- windows bat语法
@echo off 表示在这条语句之后,所有执行命令的语句不会显示 setLocal 参考1 rem 注释 :或者:: 参考 set /p 等待用户输入 SET /A express ...
- Codeforces 271D - Good Substrings [字典树]
传送门 D. Good Substrings time limit per test 2 seconds memory limit per test 512 megabytes input stand ...
- gulp 环境搭建
wind+r弹出cmd命令窗口 要先安装node及npm,检查是否安装成功,node -v,npm -v可查看nod及npm的版本号. 安装gulp之前我们需要安装nodejs的环境,检测能够正常使用 ...