hdu4757 可持续字典树
Tree
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 2058 Accepted Submission(s): 599
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.
#include<cstdio>
#include<cstring>
#include<algorithm>
const int N=100008;
int a[N],head[N],tot,index,cont,n,m;
int root[N],tree[N*35][2],son[N*35][2];
int fa[N],depth[N],up[N][18],pt[N];
struct node{
int next,to;
}e[N<<1];
void add(int u,int v){
e[tot].next=head[u];e[tot].to=v;head[u]=tot++;
e[tot].next=head[v];e[tot].to=u;head[v]=tot++;
}
void build(int last,int cur,int num,int pos){
if(pos<0) return;
int temp=!!(num&(1<<pos));
tree[cur][temp]=tree[last][temp]+1;
son[cur][temp^1]=son[last][temp^1];
tree[cur][temp^1]=tree[last][temp^1];
build(son[last][temp],son[cur][temp]=++cont,num,pos-1);//这里可以看到每一个数字都建立了31个节点,其实只要17个就够了根据题意
}
void dfs(int u){
pt[u]=++index;//记录一下每个节点在树中的位置
build(root[pt[fa[u]]],root[pt[u]]=++cont,a[u],31);
for(int i=head[u];~i;i=e[i].next){
int v=e[i].to;
if(fa[u]==v) continue;
fa[v]=u;
depth[v]=depth[u]+1;
dfs(v);
}
}
void doit(){
for(int i=1;i<=n;++i) up[i][0]=fa[i];
for(int j=1;j<=16;++j) for(int i=1;i<=n;++i) up[i][j]=up[up[i][j-1]][j-1];
}
int lca(int x,int y){
if(depth[x]<depth[y]) std::swap(x,y);
int dt=depth[x]-depth[y];
for(int i=0;i<=16;++i) if(dt&(1<<i)) x=up[x][i];
if(x==y) return x;
for(int i=16;i>=0;--i) if(up[x][i]!=up[y][i]) x=up[x][i],y=up[y][i];
return up[x][0];
}
int query(int last,int cur,int num,int sum,int pos){
if(pos<0) return sum;
int temp=!!(num&(1<<pos));
if(tree[cur][temp^1]-tree[last][temp^1]>0) return query(son[last][temp^1],son[cur][temp^1],num,sum|(1<<pos),pos-1);
else return query(son[last][temp],son[cur][temp],num,sum,pos-1);
}
int main(){
int x,y,z;
while(scanf("%d%d",&n,&m)!=EOF){
memset(head,-1,sizeof(head));
index=tot=cont=0;
for(int i=1;i<=n;++i) scanf("%d",&a[i]);
for(int i=1;i<n;++i) {scanf("%d%d",&x,&y);add(x,y);}
dfs(1);
doit();
while(m--){
scanf("%d%d%d",&x,&y,&z);
int ct=lca(x,y);
int mx=query(root[pt[fa[ct]]],root[pt[x]],z,0,31);
mx=std::max(query(root[pt[fa[ct]]],root[pt[y]],z,0,31),mx);
printf("%d\n",mx);
}
}
}
hdu4757 可持续字典树的更多相关文章
- 可持续字典树 Perfect Security
题目链接 题目大意:给你两个序列,第二个序列可以任意进行排列变换,然后由这两个序列一一异或得到答案序列,要求答案序列的字典序最小. 可持续字典树与第K大可持续线段树的区别主要在于每个节点上 ,它多了一 ...
- Hdu-4757 Tree(可持久化字典树+lca)
题目链接:点这 我的github地址:点这 Problem Description Zero and One are good friends who always have fun wi ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 9-11-Trie树/字典树/前缀树-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版
课本源码部分 第9章 查找 - Trie树/字典树/前缀树(键树) ——<数据结构>-严蔚敏.吴伟民版 源码使用说明 链接☛☛☛ <数据结构-C语言版>(严蔚 ...
- [LeetCode] 208. Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- 字典树+博弈 CF 455B A Lot of Games(接龙游戏)
题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...
- 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)
萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
- 山东第一届省赛1001 Phone Number(字典树)
Phone Number Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 We know that if a phone numb ...
随机推荐
- python django mysql配置
1 django默认支持sqlite,mysql, oracle,postgresql数据库. <1> sqlite django默认使用sqlite的数据库,默认自带sqlite ...
- 修复.NET的HttpClient
\ 看新闻很累?看技术新闻更累?试试下载InfoQ手机客户端,每天上下班路上听新闻,有趣还有料! \ \\ 早在2016年我们就报道过 ,.NET的HttpClient存在一些问题.随着.NET Co ...
- Mysql 查看被锁住的表
MYSQL 查看被锁住的表 -- 本文章仅用于学习,记录 当你在mysql 执行查询语句的时候,简单的一句查询语句却卡很久,一直转圈圈的时候,这时候你就需要怀疑数据库的哪些进程,哪些事物被锁住 ...
- 分享一批国内常用的tracker地址
本期先分享一批国内能用地址,下一期我会出一期取代迅雷的下载的工具教程. udp://p4p.arenabg.com:1337/announce udp://tracker.tiny-vps.com:6 ...
- Java——运算符那些事
&& 逻辑与 &&先运算&&左边的算式,如果为假,则直接停止,后面不管有多少运算式都不再运算,如果为真则继续判断后面的式子,只有所有的条件全部成立,才会 ...
- Spring Boot 集成 Flyway 实现数据库版本控制
在项目迭代开发中,难免会有更新数据库 Schema 的情况,比如添加新表.在表中增加字段或者删除字段等,那么当我对数据库进行一系列操作后,如何快速地在其他同事的电脑上同步?如何在测试/生产服务器上快速 ...
- word 小技巧 方框中 打 对勾
方框中 打 对勾 称为 复选框 控件,单击鼠标,在两种符号中切换. 设置步骤 1. 将隐藏的"开发工具"选项卡,显示出来 2. 在所需位置,插入复选框 3. 在属性中,设置复选框 ...
- Spring官网阅读(二)(依赖注入及方法注入)
上篇文章我们学习了官网中的1.2,1.3两小节,主要是涉及了容器,以及Spring实例化对象的一些知识.这篇文章我们继续学习Spring官网,主要是针对1.4小节,主要涉及到Spring的依赖注入.虽 ...
- request中跟路径有关的api的分析
最近重在研究struts的源码,其中涉及到了request中的几个api,看了文档后还是觉得不清楚,所以在自己原来的工程中 测试了一下各个api的具体效果.在这里跟大家分享一下. 这是我具体测试的代码 ...
- 王颖奇 20171010129《面向对象程序设计(java)》第十四周学习总结
实验十四 Swing图形界面组件 理论知识知识点: 1.Swing和MVC设计模式2.布局管理器3.文本输入4.选择组件5.菜单6.对话框 实验时间 2018-11-29 1.实验目的与要求 (1) ...