P4592 [TJOI2018]异或
吐槽
睡起来写道模板清醒一下
貌似有两个log的树剖写法,还有一个log的Trie树的差分做法(类似于count on a tree),然后一个log的要把两个询问分开写,一个dfs序一个差分,然后我就写了好写的树剖写法
以及为啥出题人这么喜欢把序列问题放到树上啊。。。
可持久化Tried树模板难度,懒得写思路
代码
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = 100100;
struct Node{
int son[2],sz;
}Trie[MAXN*40];
const int MAXlen=30;
int u[MAXN<<1],v[MAXN<<1],w[MAXN],nxt[MAXN<<1],root[MAXN],dep[MAXN],fir[MAXN],id[MAXN],top[MAXN],sz[MAXN],heason[MAXN],fa[MAXN],cnt,dfs_clock,w_p[MAXN],n,m,Nodecnt;
void addedge(int ui,int vi){
++cnt;
u[cnt]=ui;
v[cnt]=vi;
nxt[cnt]=fir[ui];
fir[ui]=cnt;
}
void dfs1(int u,int f){
sz[u]=1;
fa[u]=f;
dep[u]=dep[f]+1;
for(int i=fir[u];i;i=nxt[i]){
if(v[i]==f)
continue;
dfs1(v[i],u);
sz[u]+=sz[v[i]];
if(heason[u]==0||sz[heason[u]]<sz[v[i]])
heason[u]=v[i];
}
}
void dfs2(int u,int topf){
id[u]=++dfs_clock;
w[id[u]]=w_p[u];
top[u]=topf;
if(!heason[u])
return;
dfs2(heason[u],topf);
for(int i=fir[u];i;i=nxt[i]){
if(v[i]==fa[u]||v[i]==heason[u])
continue;
dfs2(v[i],v[i]);
}
}
void insert(int d,int val,int &o){
Trie[++Nodecnt]=Trie[o];
o=Nodecnt;
Trie[o].sz++;
if(d>=0)
insert(d-1,val,Trie[o].son[(val>>d)&1]);
}
int query(int lroot,int rroot,int val,int d){
if(d<0)
return 0;
int flag=(val>>d)&1;
if(Trie[rroot].son[!flag]>Trie[lroot].son[!flag])
return (1<<d)+query(Trie[lroot].son[!flag],Trie[rroot].son[!flag],val,d-1);
else
return query(Trie[lroot].son[flag],Trie[rroot].son[flag],val,d-1);
}
void build(void){
for(int i=1;i<=dfs_clock;i++){
root[i]=root[i-1];
insert(MAXlen,w[i],root[i]);
}
}
int query(int x,int y,int c){
int ans=0;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])
swap(x,y);
ans=max(ans,query(root[id[top[x]]-1],root[id[x]],c,MAXlen));
x=fa[top[x]];
}
if(dep[x]>dep[y])
swap(x,y);
ans=max(ans,query(root[id[x]-1],root[id[y]],c,MAXlen));
return ans;
}
int query(int x,int c){
return query(root[id[x]-1],root[id[x]+sz[x]-1],c,MAXlen);
}
int main(){
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&w_p[i]);
for(int i=1;i<n;i++){
int a,b;
scanf("%d %d",&a,&b);
addedge(a,b);
addedge(b,a);
}
dfs1(1,0);
dfs2(1,1);
build();
for(int i=1;i<=m;i++){
int opt,x,y,z;
scanf("%d %d %d",&opt,&x,&y);
if(opt==1){
printf("%d\n",query(x,y));
}
else{
scanf("%d",&z);
printf("%d\n",query(x,y,z));
}
}
return 0;
}
P4592 [TJOI2018]异或的更多相关文章
- 洛谷 P4592 [TJOI2018]异或 解题报告
P4592 [TJOI2018]异或 题目描述 现在有一颗以\(1\)为根节点的由\(n\)个节点组成的树,树上每个节点上都有一个权值\(v_i\).现在有\(Q\)次操作,操作如下: 1 x y:查 ...
- P4592 [TJOI2018]异或 (可持久化Trie)
[题目链接] https://www.luogu.org/problemnew/show/P4592 题目描述 现在有一颗以\(1\)为根节点的由\(n\)个节点组成的树,树上每个节点上都有一个权值\ ...
- [洛谷P4592][TJOI2018]异或
题目大意:有一棵$n$个点的树,第$i$个点权值为$w_i$,有两种操作: $1\;x\;y:$询问节点$x$的子树中与$y$异或结果的最大值 $2\;x\;y\;z:$询问路径$x$到$y$上点与$ ...
- 洛谷P4592 [TJOI2018]异或(可持久化01Trie)
题意 题目链接 可持久化01Trie板子题 对于两个操作分别开就行了 #include<bits/stdc++.h> using namespace std; const int MAXN ...
- 洛谷P4592 [TJOI2018]异或 【可持久化trie树】
题目链接 BZOJ4592 题解 可持久化trie树裸题 写完就A了 #include<algorithm> #include<iostream> #include<cs ...
- 【BZOJ5338】[TJOI2018]异或(主席树)
[BZOJ5338][TJOI2018]异或(主席树) 题面 洛谷 题解 很明显的是\(Trie\)树上暴力判断答案 因为要支持区间,用主席树的结构存\(Trie\)树就好了 #include< ...
- 洛谷 P4592: bzoj 5338: [TJOI2018]异或
题目传送门:洛谷P4592. 题意简述: 题面说的很清楚了. 题解: 发现没有修改很快乐.再看异或最大值操作,很容易想到可持久化 01trie. 这里要把 01trie 搬到树上,有点难受. 树剖太捞 ...
- 可持久化01Trie树+LCA【p4592】[TJOI2018]异或
Description 现在有一颗以\(1\)为根节点的由\(n\)个节点组成的树,树上每个节点上都有一个权值\(v_i\).现在有\(Q\)次操作,操作如下: 1\(\;x\;y\):查询节点\(x ...
- [TJOI2018]异或
Description: 现在有一颗以1为根节点的由n个节点组成的树,树上每个节点上都有一个权值v 现在有Q次操作,操作如下: 1.1 x y :查询节点x的子树中与y异或结果的最大值 2.2 x ...
随机推荐
- ASP.Net 获取Form表单值
新建一HtmlPage1.html,如下post发送() <body> <form enctype="multipart/form-data" action=&q ...
- python pandas使用数据透视表
1) 官网啰嗦这一堆, pandas.pivot_table函数中包含四个主要的变量,以及一些可选择使用的参数.四个主要的变量分别是数据源data,行索引index,列columns,和数值value ...
- 关于JS的几个基础题目
1.截取字符串abcdefg的efg alert("abcdefg".substring(4)); 2.判断一个字符串中出现次数最多的字符,统计这个次数 var str = 'as ...
- python自定义方法处理日志文件
从命令行界面拷贝的内容包含过个">>>",函数的作用是用正则把每两个">>>"之间的字符取出来,然后把包含“Tracebac ...
- winscp 怎么用私钥文件登录的,以.ppk结尾的密钥文件
Winscp默认用帐号密码登录,用私钥文件登录需要在高级选项里面的SSH--验证里面选择文件. Winscp使用的是putty作为SSH登录工具,而puttygen所生成的是以.ppk结尾的密钥文件. ...
- Google自动广告,将广告代码放置在 HTML 中的什么位置?
Google自动广告,将广告代码放置在 HTML 中的什么位置? 为自动广告生成广告代码后,您需要将此代码放置在要展示广告的每个网页中.您应将广告代码放置在网页的 <head> 标记(或正 ...
- hibernate二级缓存ehcache hibernate配置详解
<!-----------------hibernate二级缓存ehcache------------------------->hibernate配置 <prop key=&quo ...
- linux学习笔记---grep
先来讲讲grep(搜索过滤) 1.命令格式: grep [option] pattern file 2.命令参数: -a 不要忽略二进制的数据 -A<显示行数> 除了显示 ...
- 安装启动kafka
vim kafka/config/server.properties #确保唯一 broker.id=0 #允许删除主题 delete.topic.enable=true # 指定数据文件所在目录 l ...
- iOS项目之报错笔记
问题一: linker command failed with exit code 1 (use -vto see invocation) 原因:导入了.m的头文件,导致同时有两个一样的.m文件在编译 ...