BZOJ5338:[TJOI2018]异或——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5338
现在有一颗以1为根节点的由n个节点组成的树,树上每个节点上都有一个权值vi。现在有Q 次操作,操作如下:1 x y 查询节点x的子树中与y异或结果的最大值2 x y z 查询路径x到y上点与z异或结果最大值
别的不想多说了,出原题不怕被骂吗……
#include<cmath>
#include<queue>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e5+;
inline int read(){
int X=,w=;char ch=;
while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
while(isdigit(ch))X=(X<<)+(X<<)+(ch^),ch=getchar();
return w?-X:X;
}
struct trie{
int son[],sum;
}tr[*N];
struct node{
int to,nxt;
}e[N*];
int tot,a[N],rt[N*],pool,cnt,head[N],idx[N];
int dep[N],anc[N][],n,q,pos[N],id,size[N];
inline void add(int u,int v){
e[++cnt].to=v;e[cnt].nxt=head[u];head[u]=cnt;
}
void insert(int y,int &x,int k,int now){
tr[x=++pool]=tr[y];
tr[x].sum++;
if(now<)return;
bool p=k&(<<now);
insert(tr[y].son[p],tr[x].son[p],k,now-);
return;
}
int query(int nl,int nr,int k,int now){
if(now<)return ;
bool p=k&(<<now);
int delta=tr[tr[nr].son[p^]].sum-tr[tr[nl].son[p^]].sum;
if(delta>)return (<<now)+query(tr[nl].son[p^],tr[nr].son[p^],k,now-);
else return query(tr[nl].son[p],tr[nr].son[p],k,now-);
}
void dfs(int u,int f){
anc[u][]=f;pos[u]=++id;idx[id]=u;size[u]=;
for(int k=;k<=;k++)
anc[u][k]=anc[anc[u][k-]][k-];
dep[u]=dep[f]+;
insert(rt[f],rt[u],a[u],);
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to;
if(v==f)continue;
dfs(v,u);size[u]+=size[v];
}
return;
}
inline int LCA(int i,int j){
if(dep[i]<dep[j])swap(i,j);
for(int k=;k>=;k--){
if(dep[anc[i][k]]>=dep[j])i=anc[i][k];
}
if(i==j)return i;
for(int k=;k>=;k--){
if(anc[i][k]!=anc[j][k])i=anc[i][k],j=anc[j][k];
}
return anc[i][];
}
int main(){
n=read(),q=read();
for(int i=;i<=n;i++)a[i]=read();
for(int i=;i<n;i++){
int u=read(),v=read();
add(u,v);add(v,u);
}
dfs(,);
for(int i=;i<=n;i++)insert(rt[i+n],rt[i+n+],a[idx[i]],);
for(int i=;i<=q;i++){
int op=read();
if(op==){
int x=read(),y=read();
int l=pos[x],r=l+size[x]-;
printf("%d\n",query(rt[l+n],rt[r+n+],y,));
}else{
int x=read(),y=read(),z=read();
int lca=LCA(x,y),f=anc[lca][];
printf("%d\n",max(query(rt[f],rt[x],z,),query(rt[f],rt[y],z,)));
}
}
return ;
}
+++++++++++++++++++++++++++++++++++++++++++
+本文作者:luyouqi233。 +
+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+
+++++++++++++++++++++++++++++++++++++++++++
BZOJ5338:[TJOI2018]异或——题解的更多相关文章
- 【BZOJ5338】[TJOI2018]异或(主席树)
[BZOJ5338][TJOI2018]异或(主席树) 题面 洛谷 题解 很明显的是\(Trie\)树上暴力判断答案 因为要支持区间,用主席树的结构存\(Trie\)树就好了 #include< ...
- 洛谷 P4592 [TJOI2018]异或 解题报告
P4592 [TJOI2018]异或 题目描述 现在有一颗以\(1\)为根节点的由\(n\)个节点组成的树,树上每个节点上都有一个权值\(v_i\).现在有\(Q\)次操作,操作如下: 1 x y:查 ...
- 洛谷 P4592: bzoj 5338: [TJOI2018]异或
题目传送门:洛谷P4592. 题意简述: 题面说的很清楚了. 题解: 发现没有修改很快乐.再看异或最大值操作,很容易想到可持久化 01trie. 这里要把 01trie 搬到树上,有点难受. 树剖太捞 ...
- [洛谷P4592][TJOI2018]异或
题目大意:有一棵$n$个点的树,第$i$个点权值为$w_i$,有两种操作: $1\;x\;y:$询问节点$x$的子树中与$y$异或结果的最大值 $2\;x\;y\;z:$询问路径$x$到$y$上点与$ ...
- P4592 [TJOI2018]异或 (可持久化Trie)
[题目链接] https://www.luogu.org/problemnew/show/P4592 题目描述 现在有一颗以\(1\)为根节点的由\(n\)个节点组成的树,树上每个节点上都有一个权值\ ...
- BZOJ5338[TJOI2018]xor——主席树+dfs序
题目描述 现在有一颗以1为根节点的由n个节点组成的树,树上每个节点上都有一个权值vi. 现在有Q 次操作,操作如下: 1 x y 查询节点x的子树中与y异或结果的最大值 2 x y z ...
- BZOJ5338 [TJOI2018] Xor 【可持久化Trie树】【dfs序】
题目分析: 很无聊的一道题目.首先区间内单点对应异或值的询问容易想到trie树.由于题目在树上进行,case1将路径分成两段,然后dfs的时候顺便可持久化trie树做询问.case2维护dfs序,对d ...
- [TJOI2018]异或
Description: 现在有一颗以1为根节点的由n个节点组成的树,树上每个节点上都有一个权值v 现在有Q次操作,操作如下: 1.1 x y :查询节点x的子树中与y异或结果的最大值 2.2 x ...
- [BZOJ5338][TJOI2018]xor(可持久化Trie)
可持久化Trie模板题. 建两种可持久化Trie,每个点两棵,一棵对DFS求前缀和,一棵对祖先求前缀和. 或者树剖,不好写多少还多个log. #include<cstdio> #inclu ...
随机推荐
- Andorid自定义attr的各种坑
本文来自网易云社区 作者:孙有军 在开发Andorid应用程序中,经常会自定义View来实现各种各样炫酷的效果,在实现这吊炸天效果的同时,我们往往会定义很多attr属性,这样就可以在XML中配置我们想 ...
- python删除文本中的所有空字符
import re import os input_path = 'G:/test/aa.json' output_path ='G:/test/bb.json' with open(input_pa ...
- mysql c 获取error_code
#include <stdio.h> #include <mysql.h> int main(int argc, char **argv) { MYSQL *con = mys ...
- 编译Chromium出现warning C4819的解决办法
编译Chromium时出现 warning C4819: The file contains a character that cannot be represented in the current ...
- hdu6027Easy Summation(快速幂取模)
Easy Summation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- 关于java中的“error: bad operand types for binary operator ”
今天做这个题目的时候(142. O(1) Check Power of 2),遇到一个错误“ bad operand types for binary operator '&' ”. 先贴一下 ...
- 数据库Mysql的学习(六)-子查询和多表操作
)*0.05 WHERE card_id ='20121xxxxxx'; //子查询就是一个嵌套先计算子查询 SELECT * FROM borrow WHERE book_id =(SELECT b ...
- org.apache.spark.launcher.Main源码分析
public static void main(String[] argsArray) throws Exception { //org.apache.spark.launcher.Main chec ...
- ionic ios样式偏移解决方案。
在css属性内增加: .item-ios [item-end] { //解决ios系统上尾部图标出现重影而增加的格式. margin: 0px -15.3px 0px 0px; margin-bott ...
- 3.配置HDFS HA
安装zookeeper下载zookeeper编辑zookeeper配置文件创建myid文件启动zookeeper配置HDFS HA配置手动HA配置自动HA启动HDFS HA namenode负责管理整 ...