可持久化trie(BZOJ5338: [TJOI2018]xor)
题面
Sol
显然是要维护一个区域的 \(trie\) 树,然后贪心
区间 \(trie\) 树???
可持久化 \(trie\) 树???
直接参考主席树表示出区间的方法建立 \(trie\) 树,然后做差就好了
巨简单
# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
IL int Input(){
RG int x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
}
const int maxn(1e5 + 5);
const int pw(1 << 30);
int n, q, first[maxn], cnt, val[maxn], dfn[maxn], idx, id[maxn];
int size[maxn], son[maxn], top[maxn], deep[maxn], fa[maxn];
struct Edge{
int to, next;
} edge[maxn << 1];
struct Trie{
int ch[2][maxn * 32], rt[maxn], tot, sz[maxn * 32];
IL void Modify(RG int &x, RG int v, RG int d){
ch[0][++tot] = ch[0][x], ch[1][tot] = ch[1][x];
sz[tot] = sz[x] + 1, x = tot;
if(!d) return;
Modify(ch[bool(d & v)][x], v, d >> 1);
}
IL int Query1(RG int a, RG int b, RG int v, RG int dep){
if(!a || !dep) return 0;
RG int f = bool(dep & v) ^ 1, s = sz[ch[f][a]] - sz[ch[f][b]];
if(s) return Query1(ch[f][a], ch[f][b], v, dep >> 1) + dep;
f ^= 1;
return Query1(ch[f][a], ch[f][b], v, dep >> 1);
}
IL int Query2(RG int a, RG int b, RG int c, RG int d, RG int v, RG int dep){
if(!(a + b) || !dep) return 0;
RG int f = bool(dep & v) ^ 1, s = sz[ch[f][a]] + sz[ch[f][b]] - sz[ch[f][c]] - sz[ch[f][d]];
if(s) return Query2(ch[f][a], ch[f][b], ch[f][c], ch[f][d], v, dep >> 1) + dep;
f ^= 1;
return Query2(ch[f][a], ch[f][b], ch[f][c], ch[f][d], v, dep >> 1);
}
} tree1, tree2;
IL void Add(RG int u, RG int v){
edge[cnt] = (Edge){v, first[u]}, first[u] = cnt++;
}
IL void Dfs1(RG int u, RG int ff){
size[u] = 1, tree1.rt[u] = tree1.rt[ff];
tree1.Modify(tree1.rt[u], val[u], pw);
for(RG int e = first[u]; e != -1; e = edge[e].next){
RG int v = edge[e].to;
if(v != ff){
deep[v] = deep[u] + 1, fa[v] = u;
Dfs1(v, u);
size[u] += size[v];
if(size[v] > size[son[u]]) son[u] = v;
}
}
}
IL void Dfs2(RG int u, RG int tp){
dfn[u] = ++idx, id[idx] = u, top[u] = tp;
if(son[u]) Dfs2(son[u], tp);
for(RG int e = first[u]; e != -1; e = edge[e].next)
if(!dfn[edge[e].to]) Dfs2(edge[e].to, edge[e].to);
}
IL int LCA(RG int u, RG int v){
while(top[u] ^ top[v])
deep[top[u]] > deep[top[v]] ? u = fa[top[u]] : v = fa[top[v]];
return deep[u] > deep[v] ? v : u;
}
int main(){
n = Input(), q = Input();
for(RG int i = 1; i <= n; ++i) val[i] = Input(), first[i] = -1;
for(RG int i = 1; i < n; ++i){
RG int u = Input(), v = Input();
Add(u, v), Add(v, u);
}
Dfs1(1, 0), Dfs2(1, 0);
for(RG int i = 1; i <= n; ++i){
tree2.rt[i] = tree2.rt[i - 1];
tree2.Modify(tree2.rt[i], val[id[i]], pw);
}
for(RG int i = 1; i <= q; ++i)
if(Input() == 1){
RG int u = Input(), v = Input();
printf("%d\n", tree2.Query1(tree2.rt[dfn[u] + size[u] - 1], tree2.rt[dfn[u] - 1], v, pw));
}
else{
RG int u = Input(), v = Input(), x = Input(), lca = LCA(u, v);
printf("%d\n", tree1.Query2(tree1.rt[u], tree1.rt[v], tree1.rt[lca], tree1.rt[fa[lca]], x, pw));
}
return 0;
}
可持久化trie(BZOJ5338: [TJOI2018]xor)的更多相关文章
- [BZOJ5338][TJOI2018]xor(可持久化Trie)
可持久化Trie模板题. 建两种可持久化Trie,每个点两棵,一棵对DFS求前缀和,一棵对祖先求前缀和. 或者树剖,不好写多少还多个log. #include<cstdio> #inclu ...
- BZOJ5338 [TJOI2018] Xor 【可持久化Trie树】【dfs序】
题目分析: 很无聊的一道题目.首先区间内单点对应异或值的询问容易想到trie树.由于题目在树上进行,case1将路径分成两段,然后dfs的时候顺便可持久化trie树做询问.case2维护dfs序,对d ...
- [BZOJ5338][TJOI2018]xor
bzoj luogu descirption 现在有一棵以 \(1\) 为根节点的由 \(n\) 个节点组成的树,树上每个节点上都有一个权值 \(v_i\) .现在有 \(Q\) 次操作,操作如下: ...
- BZOJ5338[TJOI2018]xor——主席树+dfs序
题目描述 现在有一颗以1为根节点的由n个节点组成的树,树上每个节点上都有一个权值vi. 现在有Q 次操作,操作如下: 1 x y 查询节点x的子树中与y异或结果的最大值 2 x y z ...
- BZOJ.5338.[TJOI2018]xor(可持久化Trie)
BZOJ LOJ 洛谷 惊了,18年了还有省选出模板题吗= = 做这题就是练模板的,我就知道我忘的差不多了 询问一就用以DFS序为前缀得到的可持久化Trie做,询问二很经典的树上差分. 注意求询问二的 ...
- [TJOI2018] Xor 异或 (可持久化Trie,树链剖分)
题目描述 现在有一颗以 1 为根节点的由 n 个节点组成的树,树上每个节点上都有一个权值 \(v_i\).现在有 Q 次操作,操作如下: 1 x y :查询节点 x 的子树中与 y 异或结果的最大值. ...
- 51nod1295 XOR key(可持久化trie)
1295 XOR key题目来源: HackerRank基准时间限制:1.5 秒 空间限制:262144 KB 分值: 160 难度:6级算法题 给出一个长度为N的正整数数组A,再给出Q个查询,每个查 ...
- 51nod 1295 XOR key (可持久化Trie树)
1295 XOR key 题目来源: HackerRank 基准时间限制:1.5 秒 空间限制:262144 KB 分值: 160 难度:6级算法题 给出一个长度为N的正整数数组A,再给出Q个查 ...
- 【xsy1147】 异或(xor) 可持久化trie
我的脑回路可能比较奇怪. 我们对这些询问离线,将所得序列${a}$的后缀和建$n$棵可持久化$trie$. 对于一组询问$(l,r,x)$,我们在主席树上询问第$l$棵树$-$第r$+1$棵树中与$s ...
随机推荐
- delphi 10.2---非常简单的数组用法求和
unit Unit9; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- java_I/O字符流
I/O流(Stream) INPUT:输入流,从文件里读OUPUT:输出流,写内容到文件 IO流分为:字符流和字节流 字符流:处理纯文本文件. 字节流:处理可以所有文件. 字符输出流测试: @Test ...
- 【转载】基于Redis实现分布式锁
背景在很多互联网产品应用中,有些场景需要加锁处理,比如:秒杀,全局递增ID,楼层生成等等.大部分的解决方案是基于DB实现的,Redis为单进程单线程模式,采用队列模式将并发访问变成串行访问,且多客户端 ...
- ltp-ddt eth过程中遇到的问题
eth_iperf_tcp ETH_S_PERF_IPERF_TCP_INTPACING_8K_1448B source 'common.sh'; iface=`get_eth_iface_name. ...
- prim /kruskal 最小生成树
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #inc ...
- 解决Visual C++ Redistributable for Visual Studio 2015的安装问题(摘录)
1. Visual C++ Redistributable for Visual Studio 2015系统要求:Windows 7情况下必须是Windows 7 with SP1.或者Windows ...
- jenkins backup and migration
service jenkins stop 压缩包:tar -czvf /tmp/xx.tar.gz --exclude=“workspace” --exclude=“.m2" --exclu ...
- Mac 10.12安装迅雷2.7.2
说明:主要是老版本难找,这个版本最好用. 下载: (链接: https://pan.baidu.com/s/1qXTldI8 密码: dmfe)
- 【树】Validate Binary Search Tree
需要注意的是,左子树的所有节点都要比根节点小,而非只是其左孩子比其小,右子树同样.这是很容易出错的一点是,很多人往往只考虑了每个根节点比其左孩子大比其右孩子小.如下面非二分查找树,如果只比较节点和其左 ...
- 天猫消息盒子的CSS实现
css: body,h2,h3,ul,p{margin:0;padding:0;font-size:12px;} li{list-style: none; } a{text-decoration: n ...