可持久化01Trie树+LCA【p4592】[TJOI2018]异或
Description
现在有一颗以\(1\)为根节点的由\(n\)个节点组成的树,树上每个节点上都有一个权值\(v_i\)。现在有\(Q\)次操作,操作如下:
- 1\(\;x\;y\):查询节点\(x\)的子树中与\(y\)异或结果的最大值
- 2\(\;x\;y\;z\):查询路径\(x\)到\(y\)上点与\(z\)异或结果最大值
Input
第一行是两个数字\(n,Q\);
第二行是\(n\)个数字用空格隔开,第\(i\)个数字\(v_i\)表示点\(i\)上的权值
接下来\(n-1\)行,每行两个数,\(x,y\),表示节点\(x\)与\(y\)之间有边
接下来\(Q\)行,每一行为一个查询,格式如上所述.
Output
对于每一个查询,输出一行,表示满足条件的最大值。
表示不太会可持久化\(01Trie\)
参考着题解码了出来,还是有点不懂.
但是又感觉懂得差不多。
这个题差不多可以自己码出来,很好的一个题。
可持久化\(01Trie\)思想还行,类似于主席树思想.
用到了\(lastroot\).
对于现在的自己,不太想深究具体构造,感觉网上讲解的这个不是很好。
打算活过\(NOIP\)之后写一篇讲解。
代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#define R register
using namespace std;
const int maxn= 1e5+8;
inline void in(int &x)
{
int f=1;x=0;char s=getchar();
while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}
while(isdigit(s)){x=x*10+s-'0';s=getchar();}
x*=f;
}
struct Trie
{
int root[maxn],ch[maxn*35][2],tot,cnt[maxn*35];
Trie(){root[0]=tot=1;}
inline void insert(int lastroot,int &nowroot,int x)
{
nowroot=++tot;
int u=nowroot;
for(R int i=30;~i;i--)
{
R int bit=(x>>i)&1;
ch[u][!bit]=ch[lastroot][!bit];
ch[u][bit]=++tot;
u=ch[u][bit];
lastroot=ch[lastroot][bit];
cnt[u]=cnt[lastroot]+1;
}
}
inline int query(int l,int r,int x)
{
int res=0;
for(R int i=30;~i;i--)
{
R int bit=(x>>i)&1;
if(cnt[ch[r][!bit]]-cnt[ch[l][!bit]])
{
r=ch[r][!bit];
l=ch[l][!bit];
res+=(1<<i);
}
else
{
r=ch[r][bit];
l=ch[l][bit];
}
}
return res;
}
}tr,se;
int dfn[maxn],fdfn[maxn],val[maxn],idx,depth[maxn];
int head[maxn],tot,l[maxn],r[maxn],size[maxn];
struct cod{int u,v;}edge[maxn<<1];
inline void add(R int x,R int y)
{
edge[++tot].u=head[x];
edge[tot].v=y;
head[x]=tot;
}
int f[maxn][21];
void dfs(R int u,R int fa)
{
tr.insert(tr.root[fa],tr.root[u],val[u]);
f[u][0]=fa;depth[u]=depth[fa]+1;
dfn[u]=++idx,fdfn[idx]=u;size[u]=1;
for(R int i=1;(1<<i)<=depth[u];i++)
f[u][i]=f[f[u][i-1]][i-1];
for(R int i=head[u];i;i=edge[i].u)
{
if(edge[i].v==fa)continue;
dfs(edge[i].v,u);
size[u]+=size[edge[i].v];
}
}
inline int lca(R int x,R int y)
{
if(depth[x]>depth[y])swap(x,y);
for(R int i=17;~i;i--)
if(depth[x]+(1<<i)<=depth[y])
y=f[y][i];
if(x==y)return y;
for(R int i=17;~i;i--)
{
if(f[x][i]==f[y][i])continue;
x=f[x][i],y=f[y][i];
}
return f[x][0];
}
int n,q;
int main()
{
in(n),in(q);
for(R int i=1;i<=n;i++)in(val[i]);
for(R int i=1,x,y;i<n;i++)
{
in(x),in(y);
add(x,y),add(y,x);
}
dfs(1,0);
for(R int i=1;i<=n;i++)
se.insert(se.root[i-1],se.root[i],val[fdfn[i]]);
for(R int opt,x,y,z;q;q--)
{
in(opt);
if(opt==1)
{
in(x),in(y);
printf("%d\n",se.query(se.root[dfn[x]-1],se.root[dfn[x]+size[x]-1],y));
}
else
{
in(x),in(y),in(z);
int la=lca(x,y);
printf("%d\n",max(tr.query(tr.root[f[la][0]],tr.root[x],z),tr.query(tr.root[f[la][0]],tr.root[y],z)));
}
}
}
可持久化01Trie树+LCA【p4592】[TJOI2018]异或的更多相关文章
- 可持久化01Trie树【p4735(bzoj3261)】最大异或和
Description 给定一个非负整数序列\(\{a\}\),初始长度为\(N\). 有\(M\)个操作,有以下两种操作类型: A x:添加操作,表示在序列末尾添加一个数\(x\),序列的长度\(N ...
- 洛谷 P4592 [TJOI2018]异或 解题报告
P4592 [TJOI2018]异或 题目描述 现在有一颗以\(1\)为根节点的由\(n\)个节点组成的树,树上每个节点上都有一个权值\(v_i\).现在有\(Q\)次操作,操作如下: 1 x y:查 ...
- 可持久化0-1Trie树
我跟可持久化数据结构杠上了 \(QwQ\) .三天模拟赛考了两次可持久化数据结构(主席树.可持久化0-1Trie树),woc. 目录: 个人理解 时空复杂度分析 例题及简析 一.个人理解 可持久化0- ...
- bzoj 4137 [FJOI2015]火星商店问题——线段树分治+可持久化01trie树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4137 关于可持久化01trie树:https://www.cnblogs.com/LadyL ...
- BZOJ 2588: Spoj 10628. Count on a tree-可持久化线段树+LCA(点权)(树上的操作) 无语(为什么我的LCA的板子不对)
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 9280 Solved: 2421 ...
- Hdu-4757 Tree(可持久化字典树+lca)
题目链接:点这 我的github地址:点这 Problem Description Zero and One are good friends who always have fun wi ...
- 洛谷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 ...
- BZOJ 3261: 最大异或和位置-贪心+可持久化01Trie树
3261: 最大异或和 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 3519 Solved: 1493[Submit][Status][Discu ...
随机推荐
- 9.python爬虫--pyspider
pyspider简介 PySpider:一个国人编写的强大的网络爬虫系统并带有强大的WebUI.采用Python语言编写,分布式架构,支持多种数据库后端,强大的WebUI支持脚本编辑器,任务监视器,项 ...
- RSA host key has changed 错误
RSA host key for mysharebook.cn has changed and you have requested strict checking.Host key verifica ...
- bzoj2516 电梯
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2516 [题解] 状压dp. $f_{sta,i}$表示状态为sta,当前在第i层的最小花费时 ...
- UIImageView属性---iOS-Apple苹果官方文档翻译
本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 //转载请注明出处--本文永久链接:http://www.cnblogs.com/C ...
- MSSQL数据库 "无法删除数据库 "***",因为该数据库当前正在使用" 解决方案
--1 创建数据库 create database AAA --2 使用数据库 use AAA --3 在AAA数据库下创建table create table BBB ( bId ,) primar ...
- js按值及引用传递中遇到的小问题
有人闲的蛋疼,非要在函数中使用如下方式传值,尼玛一下把我搞糊涂了.于是决定发挥打破沙锅问到底的精神搞清楚它. var a = 1,b = [], c = {}; function f(a, b, c) ...
- AndroidStudio获得发布版安全码SHA1
耗了一下午才搞定 在cmd中: 1.打开keytool的目录:即JDK的安装目录 2.输入口令: (E:\tenyears\tenyears\app是keystore文件的目录)
- sublime在搜索的时候排除js文件
代码审计的时候sublime是一个神器.所以.... Ctrl + Shift + F /home/i3ekr/Desktop/coding/phpcms,*.php 这样就可以直接搜索所有的php文 ...
- bisai.py
比赛专用py #!/usr/etc/env python #encoding:utf-8 #by i3ekr #token import re,os,requests res = "(fla ...
- 【C语言】Coursera课程《计算机程式设计》台湾大学刘邦锋——Week6 String课堂笔记
Coursera课程 <计算机程式设计>台湾大学 刘邦锋 Week6 String 6-1 Character and ASCII 字符变量的声明 char c; C语言使用一个位元组来储 ...