BZOJ_5338_ [TJOI2018]xor_可持久化trie
BZOJ_5338_ [TJOI2018]xor_可持久化trie
Description
Input
Output
Sample Input
1 2 3
1 5 1
2 3 1
2 4 2
Sample Output
【样例解释】
将点1,2染黑就能获得最大收益。
HINT
2017.9.12新加数据一组 By GXZlegend
博客里没有几道可持久化trie的题,还是更一篇吧。
同时要求子树和两点路径上的信息,
只能用两个序维护一下,然后可持久化一下随便搞搞。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
#define N 200050
int head[N],to[N],nxt[N],val[N],cnt,n,m,be[N],ed[N],tot,root[N],t[N*33],sanae,ch[N*33][2],son[N],siz[N],fa[N],dep[N],top[N];
int t2[N*33],ch2[N*33][2],dfn[N],marisa,reimu,root2[N];
inline void add(int u,int v) {
to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt;
}
void update(int x,int c,int &y,int q) {
y=++sanae; int p=y; t[p]=t[q]+c;
int i;
for(i=30;i>=0;i--) {
int k=(x>>i)&1;
ch[p][k]=++sanae; ch[p][!k]=ch[q][!k];
p=ch[p][k]; q=ch[q][k]; t[p]=t[q]+c;
}
}
void upd(int x,int c,int &y,int q) {
y=++reimu; int p=y; t2[p]=t2[q]+c;
// printf("upd---%d %d %d %d\n",p,q,t2[p],t2[q]);
int i;
for(i=30;i>=0;i--) {
int k=(x>>i)&1;
ch2[p][k]=++reimu; ch2[p][!k]=ch2[q][!k];
p=ch2[p][k]; q=ch2[q][k]; t2[p]=t2[q]+c;
}
}
void dfs(int x,int y) {
dfn[x]=++marisa; upd(val[x],1,root2[marisa],root2[marisa-1]);
// printf("dfs---%d %d %d %d\n",root2[marisa],root2[marisa-1],t2[root2[marisa]],t2[root2[marisa-1]]);
// printf("%d %d %d %d\n",t2[root2[1]],t2[root2[2]],t2[root2[3]],t2[root2[4]]);
int i; be[x]=++tot; update(val[x],1,root[tot],root[tot-1]); siz[x]=1; fa[x]=y; dep[x]=dep[y]+1;
for(i=head[x];i;i=nxt[i]) {
if(to[i]!=y) {
dfs(to[i],x); siz[x]+=siz[to[i]]; if(siz[son[x]]<siz[to[i]]) son[x]=to[i];
}
}
ed[x]=++tot; update(val[x],-1,root[tot],root[tot-1]);
}
void dfs2(int x,int t) {
top[x]=t;
if(son[x]) dfs2(son[x],t);
int i;
for(i=head[x];i;i=nxt[i])if(to[i]!=fa[x]&&to[i]!=son[x]) dfs2(to[i],to[i]);
}
int lca(int x,int y) {
while(top[x]!=top[y]) {
if(dep[top[x]]>dep[top[y]]) swap(x,y);
y=fa[top[y]];
}
return dep[x]<dep[y]?x:y;
}
int solve1(int x,int y,int v) {
// printf("%d %d %d %d\n",x,y,t2[x],t2[y]);
int i,re=0;
for(i=30;i>=0;i--) {
int k=!((v>>i)&1);
if(t2[ch2[y][k]]-t2[ch2[x][k]]) {
re+=(1<<i); x=ch2[x][k]; y=ch2[y][k];
}else x=ch2[x][!k],y=ch2[y][!k];
}
return re;
}
int solve2(int x,int y,int z,int w,int v) {
int i,re=0;
for(i=30;i>=0;i--) {
int k=!((v>>i)&1);
if(t[ch[x][k]]+t[ch[y][k]]-t[ch[z][k]]-t[ch[w][k]]>0) re+=(1<<i),x=ch[x][k],y=ch[y][k],z=ch[z][k],w=ch[w][k];
else x=ch[x][!k],y=ch[y][!k],z=ch[z][!k],w=ch[w][!k];
}
return re;
}
int main() {
scanf("%d%d",&n,&m);
int i,x,y,opt,z;
for(i=1;i<=n;i++) scanf("%d",&val[i]);
for(i=1;i<n;i++) scanf("%d%d",&x,&y),add(x,y),add(y,x);
dfs(1,0); dfs2(1,1);
// printf("%d %d %d %d\n",t2[root2[1]],t2[root2[2]],t2[root2[3]],t2[root2[4]]);
for(i=1;i<=m;i++) {
scanf("%d%d%d",&opt,&x,&y);
if(opt==1) {
// printf("%d\n",root2[dfn[x]-1]);
printf("%d\n",solve1(root2[dfn[x]-1],root2[dfn[x]+siz[x]-1],y));
}else {
scanf("%d",&z);
int l=lca(x,y);
printf("%d\n",solve2(root[be[x]],root[be[y]],root[be[l]],root[be[fa[l]]],z));
}
}
}
BZOJ_5338_ [TJOI2018]xor_可持久化trie的更多相关文章
- BZOJ.5338.[TJOI2018]xor(可持久化Trie)
BZOJ LOJ 洛谷 惊了,18年了还有省选出模板题吗= = 做这题就是练模板的,我就知道我忘的差不多了 询问一就用以DFS序为前缀得到的可持久化Trie做,询问二很经典的树上差分. 注意求询问二的 ...
- [BZOJ5338][TJOI2018]xor(可持久化Trie)
可持久化Trie模板题. 建两种可持久化Trie,每个点两棵,一棵对DFS求前缀和,一棵对祖先求前缀和. 或者树剖,不好写多少还多个log. #include<cstdio> #inclu ...
- BZOJ 5338: [TJOI2018]xor 可持久化trie+dfs序
强行把序列问题放树上,好无聊啊~ code: #include <bits/stdc++.h> #define N 200005 #define setIO(s) freopen(s&qu ...
- BZOJ5338 [TJOI2018] Xor 【可持久化Trie树】【dfs序】
题目分析: 很无聊的一道题目.首先区间内单点对应异或值的询问容易想到trie树.由于题目在树上进行,case1将路径分成两段,然后dfs的时候顺便可持久化trie树做询问.case2维护dfs序,对d ...
- 可持久化trie(BZOJ5338: [TJOI2018]xor)
题面 BZOJ Sol 显然是要维护一个区域的 \(trie\) 树,然后贪心 区间 \(trie\) 树??? 可持久化 \(trie\) 树??? 直接参考主席树表示出区间的方法建立 \(trie ...
- 洛谷P4592 [TJOI2018]异或 【可持久化trie树】
题目链接 BZOJ4592 题解 可持久化trie树裸题 写完就A了 #include<algorithm> #include<iostream> #include<cs ...
- [TJOI2018] Xor 异或 (可持久化Trie,树链剖分)
题目描述 现在有一颗以 1 为根节点的由 n 个节点组成的树,树上每个节点上都有一个权值 \(v_i\).现在有 Q 次操作,操作如下: 1 x y :查询节点 x 的子树中与 y 异或结果的最大值. ...
- HDU 4757 Tree(可持久化Trie+Tarjan离线LCA)
Tree Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others) Total Su ...
- 【BZOJ4260】 Codechef REBXOR 可持久化Trie
看到异或就去想前缀和(⊙o⊙) 这个就是正反做一遍最大异或和更新答案 最大异或就是很经典的可持久化Trie,从高到低贪心 WA: val&(1<<(base-1))得到的并不直接是 ...
随机推荐
- 敏捷DoD完毕定义的多种形态
作者:张克强 作者微博:张克强-敏捷307 关于Definition of Done 完毕的定义 在以往的说法中,常见用 退出标准 , 完毕条件.成功标准,等等 在敏捷软件开发中,存在多级的不同 ...
- C++成员不通过对象调用的直接调用写法
C++成员不通过对象调用(.或->方式)的另类(C式)调用写法 #include <iostream> using namespace std; /* 我们知道,成员函数和普通函数最 ...
- 织梦在广告(myad)中使用css样式
使用单引号,以及只有style这一个属性
- ios 推送证书没有密钥 解决方案【转载】
注意事项: 1.keychains选择Login 2.2.在创建完CertificateSigningRequest.certSigningRequest可以看到Keys中该有你的私有秘钥 3.按文档 ...
- C#高级编程八十一天----捕获异常
捕获异常 前面主要说了关于异常的一些基础和理论知识,没有进入到正真的异常案例,这一讲通过几个案例来描写叙述一下异常的捕获和处理. 案例代码: using System; using System.Co ...
- activemq 搭建--集群
linux activmemq 集群安装,配置和高可用测试 从 ActiveMQ 5.9 开始,ActiveMQ 的集群实现方式取消了传统的Master-Slave 方式,增加了基于Z ...
- TP框架---thinkphp修改删除数据
1.在控制器MainController里面写一个方法,调用Nation表中的数据. public function zhuyemian() { $n = D("Nation"); ...
- 【BZOJ4408】[Fjoi 2016]神秘数 主席树神题
[BZOJ4408][Fjoi 2016]神秘数 Description 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13},1 = 12 = 1 ...
- 怎么用cookie解决选项卡问题刷新后怎么保持原来的选项?
什么是cookie? Cookies虽然一般都以英文名呈现,但是它还是有一个可爱的中文名“小甜饼”.Cookies是指服务器暂存放在你的电脑里的txt格式的文本文件资料,主要用于网络服务器辨别电脑使用 ...
- android菜鸟学习笔记4----android项目结构
src: 应用程序源代码存放目录 gen: 自动生成的目录,目录中存放所有由Android开发工具自动生成的文件. 目录中最重要的就是R.java文件. 这个文件由Android开发工具自动产生的.A ...