可持久化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 ...
随机推荐
- Spring 并发事务的探究
前言 在目前的软件架构中,不仅存在单独的数据库操作(一条SQL以内,还存在逻辑性的一组操作.而互联网软件系统最少不了的就是对共享资源的操作.比如热闹的集市,抢购的人群对同见商品的抢购由一位售货员来处理 ...
- 部署WSUS服务(一)
引言:随着网络的发展,我们的生活也越来越离不开网络,但面临的安全威胁也越来越多.像去年爆发的针对Windows系统的勒索病毒(Wanna Cry)和年初爆发的Intel芯片漏洞告诉我们网络威胁时时刻刻 ...
- 数组或者stack
数组 clear1(long long int array[], size_t int size) { ; i < size; i += ) array[i] = ; } li x5, // i ...
- Great Expectations
Dear friend, This game is created based on Dicken's Great Expectations. To colorful the contents, I ...
- Python开发:Windows下Python+Eclipse+Pydev开发环境配置
一.配置前的准备: 1.安装jdk: 下载地址: https://www.oracle.com/technetwork/java/javase/downloads/index.html 2.安装Ecl ...
- OpenERP 疑问之一
def _get_send_amount(self,cr,uid,ids,name,args,context=None): res={} MRP={} lines = self.browse(cr,u ...
- 查看Postgresql的连接状况
今天遇到一个问题,就是pg一直报错,说有太多的客户端连接到数据库上面.但现在不知道是什么程序连接.pg默认的max_connection是100,我并没有修改过,以为平时公司内部用,应该够了,但现在貌 ...
- wusir 线程间操作无效: 从不是创建控件“”的线程访问它 解决办法
利用FileSystemWatcher设计一个文件监控系统时,如果一个文件被修改或者新建,则文件修改事件会被多次触发而产生多条信息.为了将一个文件被修改一次而产生的多条信息归结为一条,在设计中新开了一 ...
- 搭建类似生产环境的RAC
install the necessary packages yum install -y 创建组.用户和目录 创建用户组 groupadd -g 2000 oinstallgroupadd -g 2 ...
- 《Algorithms算法》笔记:元素排序(1)——简单排序
<Algorithms算法>元素排序(1)——简单排序 Algorithms算法元素排序1简单排序 排序问题 1 回调函数 2Java中回调函数的路线图 3 全序 4 Comparable ...